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 type 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. > > 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_VARIANTs. > > 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... Jakub