public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Qing Zhao <qing.zhao@oracle.com>
To: Martin Uecker <uecker@tugraz.at>
Cc: Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>,
	"gcc-patches@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	Joseph Myers <joseph@codesourcery.com>
Subject: Re: [C PATCH]: Add Walloc-type to warn about insufficient size in allocations
Date: Wed, 2 Aug 2023 16:45:56 +0000	[thread overview]
Message-ID: <C44AEAFB-9B4C-441D-9734-12611BD3EFC3@oracle.com> (raw)
In-Reply-To: <09c7e37362800bf5eacffc8b442d3a84c3f7f817.camel@tugraz.at>



> On Aug 1, 2023, at 10:31 AM, Martin Uecker <uecker@tugraz.at> wrote:
> 
> Am Dienstag, dem 01.08.2023 um 13:27 +0000 schrieb Qing Zhao:
>> 
>>> On Aug 1, 2023, at 3:51 AM, Martin Uecker via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
>>> 
> 
> ....
>>>> Hi Martin,
>>>> Just wondering if it'd be a good idea perhaps to warn if alloc size is
>>>> not a multiple of TYPE_SIZE_UNIT instead of just less-than ?
>>>> So it can catch cases like:
>>>> int *p = malloc (sizeof (int) + 2); // probably intended malloc
>>>> (sizeof (int) * 2)
>>>> 
>>>> FWIW, this is caught using -fanalyzer:
>>>> f.c: In function 'f':
>>>> f.c:3:12: warning: allocated buffer size is not a multiple of the
>>>> pointee's size [CWE-131] [-Wanalyzer-allocation-size]
>>>>    3 |   int *p = __builtin_malloc (sizeof(int) + 2);
>>>>      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>> 
>>>> Thanks,
>>>> Prathamesh
>>> 
>>> Yes, this is probably a good idea.  It might need special
>>> logic for flexible array members then...
>> 
>> Why special logic for FAM on such warning? (Not a multiple of TYPE_SIZE_UNIT for the element).
>> 
> 
> For
> 
> struct { int n; char buf[]; } *p = malloc(sizeof *p + n);
> p->n = n;
> 
> the size would not be a multiple.

But n is still a multiple of sizeof (char), right? Do I miss anything here?

Qing
> 
> Martin
> 
> 
> 
> 


  reply	other threads:[~2023-08-02 16:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 11:21 Martin Uecker
2023-07-21 20:55 ` Qing Zhao
2023-07-31 19:39 ` Siddhesh Poyarekar
2023-07-31 20:06   ` Martin Uecker
2023-07-31 20:41 ` Prathamesh Kulkarni
2023-08-01  7:51   ` Martin Uecker
2023-08-01 13:27     ` Qing Zhao
2023-08-01 14:31       ` Martin Uecker
2023-08-02 16:45         ` Qing Zhao [this message]
2023-08-02 17:55           ` Martin Uecker

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=C44AEAFB-9B4C-441D-9734-12611BD3EFC3@oracle.com \
    --to=qing.zhao@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=prathamesh.kulkarni@linaro.org \
    --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).