https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544

            Bug ID: 87544
           Summary: alloc-size-larger-than incorrectly triggered
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gael.guennebaud at gmail dot com
  Target Milestone: ---

Created attachment 44800
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44800&action=edit
self-contained test case

The attached example incorrectly trigger the alloc-size-larger-than= warning
with either gcc 7, 8 or trunk. (-O2 -Wall). It is also reproduced on godbolt
for convenience:
https://godbolt.org/z/KXsyZP

The weirdest thing is that if I remove the condition line 15:

if(size>16 && (std::size_t(result) & 15)!=0)

or only remove one of the condition like:

if(size>16) { ... }
if((std::size_t(result) & 15)!=0) { ... }

then the warning is gone. I don't really see how a test on the pointer returned
by malloc can change anything... If I replace this test by an assert (instead
of freeing and returning 0) then I also get the alloc-size-larger-than=
warning.

Of course I can workaround with:

  void *result =  0;
  if(size<std::numeric_limits<std::ptrdiff_t>::max())
    result = std::malloc(size);

and this is what I'm going to do in the original code.

Reply via email to