From: Martin Uecker <uecker@tugraz.at>
To: Qing Zhao <qing.zhao@oracle.com>
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, 02 Aug 2023 19:55:05 +0200 [thread overview]
Message-ID: <83b2a19a3459dcb57cb5fb905e02510468cd0142.camel@tugraz.at> (raw)
In-Reply-To: <C44AEAFB-9B4C-441D-9734-12611BD3EFC3@oracle.com>
Am Mittwoch, dem 02.08.2023 um 16:45 +0000 schrieb Qing Zhao:
>
> > 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?
Right, for a struct with FAM we could check that it is
sizeof () plus a multiple of the element size of the FAM.
Still special logic...
Martin
> Qing
> >
> > Martin
> >
> >
> >
> >
>
--
Univ.-Prof. Dr. rer. nat. Martin Uecker
Graz University of Technology
Institute of Biomedical Imaging
prev parent reply other threads:[~2023-08-02 17:55 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
2023-08-02 17:55 ` Martin Uecker [this message]
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=83b2a19a3459dcb57cb5fb905e02510468cd0142.camel@tugraz.at \
--to=uecker@tugraz.at \
--cc=gcc-patches@gcc.gnu.org \
--cc=joseph@codesourcery.com \
--cc=prathamesh.kulkarni@linaro.org \
--cc=qing.zhao@oracle.com \
/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).