From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by sourceware.org (Postfix) with ESMTPS id 22BFD385840B for ; Wed, 6 Dec 2023 18:00:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 22BFD385840B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gwmail.gwu.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gwmail.gwu.edu ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 22BFD385840B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::132 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701885657; cv=none; b=Gz/UW2fzFz1ZgMyw7/I3x3PqgwmnhhlpDub2WJp1EXN5NJBFw84BBYhf1CALsoX8pWTxp/3VMYTEa6wV108IhB1hO5sE+M58gUdbW9LT8ZOtb/UYQFpHe4puBdkrAPoPDa+FoeJdyU64J6hyInikVoCjfdCSxOvoSgVDvuANPok= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701885657; c=relaxed/simple; bh=6M86Nt1TwTxmoLY24jQQPXQt3hW50o+cCUws6gJNvdI=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=KMp5UXeb5KCyL41zO/KrWn5oEOW22iQ6uy+PJm5AswDN1G/ububN7mVFeA3Ji4GP3px6gI0htyuhsIgd1kOOXFUgsbXM3QwmcLHr6APf9885zhv+YD0vwUjNLFxw0aVXqaCy2NLTU170aosk2mIm6qwcSTY7/34ljm4Y+Mfj+HE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x132.google.com with SMTP id e9e14a558f8ab-35d9d0f4478so727265ab.1 for ; Wed, 06 Dec 2023 10:00:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gwmail.gwu.edu; s=google; t=1701885655; x=1702490455; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Yb4N/F1NUVypKEBnlweGr9JPzsZUe7LlcjLhyVV10vE=; b=Pwon2dAH8Kkwg/YNcz1ero2I2kiNiaysWO2gks00P+5noaNHYMMKxqZhkYSU3aM6RN 8KOqQtknARq6UdRWd1IsMk48lVynqVZus/73A+m+oZ2ncxOjq9gxaAWAHtRzfPhtFyOc cocXIIhTrKv/5Hv/O48NH1KAj9/1jnLKN/v9kSZMYncNSIumVXcDjuqS1nI5KM/Arypr klxi4J3DDMCtCKL8c1EZK4lPSwxA71Tkd5BhpPv6bWMvNsU0k8M5BNCRZtKlXHyBzVJX P7cxiVLvXw2Z9haD29fOsDf1/INb5e7KhNvyuY8wEhGRFnyBA3SrO7iQbs/w7XhooriI 4zEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701885655; x=1702490455; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Yb4N/F1NUVypKEBnlweGr9JPzsZUe7LlcjLhyVV10vE=; b=bi37ZNhK+/TVGki8M741u6lmtc8QDoABn7Hd+VUSoIOVW5o+V3yOVLN65V2T6N3Nv6 CdDTJrKlp9Pg+kCLKCzsFHGWKlhCycROXrPeCx+2j1mhF6dPUxi7VhbdJz04raJDDbPT N0M3DDDwyczvgxs9yd63C4sZpW+aGPR4HKqREbY6+V8qFOXcHUxVj6tLSKC1ll8uFpo0 CCw+hXzQD0oaJ8gplx3iVhyKw5fyrjzK/zE8fozGh3lAQ33FptONdxslcJ51iFJUgIhp U3xbbqabAdadTfnGxye2wosPHFrs6mOetm1M078XSxQ/2Gf3RX1BQ2NelZQGQyZQtFQh R7hw== X-Gm-Message-State: AOJu0YzFIk8rHgffoFpn1fMj53LnFdB4ktwpAHQUwCUYe0mXRMX3fo6f aepTBHtU04CFjBEe8Hiqv3ZnBa7zy+H5kB2/rKfvjQ== X-Google-Smtp-Source: AGHT+IG0DJ3sr56C2rLoBhez4HsaXX4piGRuHds04k2gWxF3bb2A3EIm4aXQeSKBWjfe7M9h4NFO0THlhCNsDWKxe8I= X-Received: by 2002:a05:6e02:10a:b0:35d:59a2:a334 with SMTP id t10-20020a056e02010a00b0035d59a2a334mr1558384ilm.54.1701885655329; Wed, 06 Dec 2023 10:00:55 -0800 (PST) MIME-Version: 1.0 References: <90ccd1f0c61d39d1a3f88007c160bc0983eadf9c.camel@tugraz.at> <1d48016d1dc1d2280c3b36b4d2f0d1bda8d9c593.camel@tugraz.at> In-Reply-To: From: Eric Gallager Date: Wed, 6 Dec 2023 13:00:43 -0500 Message-ID: Subject: Re: [C PATCH, v2] Add Walloc-size to warn about insufficient size in allocations [PR71219] To: Martin Uecker Cc: Jakub Jelinek , Xi Ruoyao , gcc-patches@gcc.gnu.org, Joseph Myers , Marek Polacek Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, Dec 6, 2023 at 10:13=E2=80=AFAM Martin Uecker wr= ote: > > Am Mittwoch, dem 06.12.2023 um 16:01 +0100 schrieb Jakub Jelinek: > > On Wed, Dec 06, 2023 at 03:56:10PM +0100, Martin Uecker wrote: > > > > That would be my preference because then the allocation size is > > > > correct and it is purely a style warning. > > > > It doesn't follow how the warning is described: > > > > "Warn about calls to allocation functions decorated with attribute > > > > @code{alloc_size} that specify insufficient size for the target typ= e of > > > > the pointer the result is assigned to" > > > > when the size is certainly sufficient. > > > > > > The C standard defines the semantics of to allocate space > > > of 'nmemb' objects of size 'size', so I would say > > > the warning and its description are correct because > > > if you call calloc with '1' as size argument but > > > the object size is larger then you specify an > > > insufficient size for the object given the semantical > > > description of calloc in the standard. > > > > 1 is sizeof (char), so you ask for an array of sizeof (struct ...) > > chars and store the struct into it. > > If you use > > char *p =3D calloc(sizeof(struct foo), 1); > > it does not warn. > > > > > > > We have the -Wmemset-transposed-args warning, couldn't we > > > > have a similar one for calloc, and perhaps do it solely in > > > > the case where one uses sizeof of the type used in the cast > > > > pointer? > > > > So warn for > > > > (struct S *) calloc (sizeof (struct S), 1) > > > > or > > > > (struct S *) calloc (sizeof (struct S), n) > > > > but not for > > > > (struct S *) calloc (4, 15) > > > > or > > > > (struct S *) calloc (sizeof (struct T), 1) > > > > or similar? Of course check for compatible types of TYPE_MAIN_VARI= ANTs. > > > > > > Yes, although in contrast to -Wmeset-transposed-args > > > this would be considered a "style" option which then > > > nobody would activate. And if we put it into -Wextra > > > then we have the same situation as today. > > > > Well, the significant difference would be that users would > > know that they got the size for the allocation right, just > > that a coding style says it is better to put the type's size > > as the second argument rather than first, and they could disable > > that warning separately from -Walloc-size and still get warnings > > on (struct S *) calloc (1, 1) or (struct S *) malloc (3) if > > sizeof (struct S) is 24... > > Ok. > > Note that another limitation of the current version is that it > does not warn for > > ... =3D (struct S*) calloc (...) > > with the cast (which is non-idiomatic in C). Note that -Wc++-compat encourages the cast, for people who are trying to make their code compilable as both C and C++. > This is also > something I would like to address in the future and would be > more important for the C++ version. But for this case it > should probably use the type of the cast and the warning > needs to be added somewhere else in the FE. > > > Martin >