public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Jakub Jelinek <jakub@redhat.com>, Martin Uecker <uecker@tugraz.at>
Cc: "Joseph S. Myers" <joseph@codesourcery.com>,
	Marek Polacek <polacek@redhat.com>,
	Richard Biener <rguenther@suse.de>,
	gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c: Split -Wcalloc-transposed-args warning from -Walloc-size, -Walloc-size fixes
Date: Tue, 19 Dec 2023 12:20:11 -0500	[thread overview]
Message-ID: <6a303c5f-8c7a-435f-b823-fa00f3376471@redhat.com> (raw)
In-Reply-To: <ZYFYiltexdf6gU4m@tucnak>

On 12/19/23 03:47, Jakub Jelinek wrote:
> On Tue, Dec 19, 2023 at 08:11:11AM +0100, Martin Uecker wrote:
>> Am Montag, dem 18.12.2023 um 20:14 +0100 schrieb Jakub Jelinek:
>>> Hi!
>>>
>>> The following patch changes -Walloc-size warning to no longer warn
>>> about int *p = calloc (1, sizeof (int));, because as discussed earlier,
>>> the size is IMNSHO sufficient in that case, for alloc_size with 2
>>> arguments warns if the product of the 2 arguments is insufficiently small.
>>>
>>> Also, it warns also for explicit casts of malloc/calloc etc. calls
>>> rather than just implicit, so not just
>>>    int *p = malloc (1);
>>> but also
>>>    int *p = (int *) malloc (1);
>>>
>>> It also fixes some ICEs where the code didn't verify the alloc_size
>>> arguments properly (Walloc-size-5.c testcase ICEs with vanilla trunk).
>>>
>>> And lastly, it introduces a coding style warning, -Wcalloc-transposed-args
>>> to warn for calloc (sizeof (struct S), 1) and similar calls (regardless
>>> of what they are cast to, warning whenever first argument is sizeof and
>>> the second is not).
>>
>> I would generally see function arguments that are swapped relative
>> to the documented ABI as more than a coding style issue even in
>> cases where it can be expected to make no difference.
> 
> If you have suggestions how to reword the documentation, would that be
> sufficient for you?  I still don't see why given correct alignment one can't
> store struct S into sizeof (struct S) sized heap char array,

Seems to me one can in C++, anyway.  An unsigned char array can provide 
storage for another type, and the call to calloc can be interpreted as 
creating such an array if that gives the program defined behavior.
https://eel.is/c++draft/intro.object#def:provides_storage
https://eel.is/c++draft/intro.object#def:object,implicit_creation

Jason


  parent reply	other threads:[~2023-12-19 17:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-18 19:14 Jakub Jelinek
2023-12-19  7:11 ` Martin Uecker
2023-12-19  8:47   ` Jakub Jelinek
2023-12-19 10:34     ` Martin Uecker
2023-12-19 17:20     ` Jason Merrill [this message]
2023-12-19 17:45       ` Martin Uecker
2023-12-19 19:32 ` Joseph Myers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a303c5f-8c7a-435f-b823-fa00f3376471@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=polacek@redhat.com \
    --cc=rguenther@suse.de \
    --cc=uecker@tugraz.at \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).