Am Mittwoch, dem 02.08.2023 um 16:45 +0000 schrieb Qing Zhao: > > > On Aug 1, 2023, at 10:31 AM, Martin Uecker <uec...@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