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

            Bug ID: 118474
           Summary: -Wanalyzer-allocation-size false positive with -O0
                    -fsanitize=integer-divide-by-zero -fanalyzer
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: xry111 at gcc dot gnu.org
  Target Milestone: ---

void *realloc (void *, unsigned long)
  __attribute__((__nothrow__, __leaf__))
  __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__ (2)));

long *
slurp1 (long *buffer, unsigned long file_size)
{
  return (long *) realloc (buffer, file_size - file_size % sizeof (long));
}

With -fanalyzer -O0 -fsanitize=integer-divide-by-zero:

ana.c: In function 'slurp1':
ana.c:8:19: warning: allocated buffer size is not a multiple of the pointee's
size [CWE-131] [-Wanalyzer-allocation-size]
    8 |   return (long *) realloc (buffer, file_size - file_size % sizeof
(long));
      |                  
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  'slurp1': event 1
    |
    |    8 |   return (long *) realloc (buffer, file_size - file_size % sizeof
(long));
    |      |                  
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |                   |
    |      |                   (1) allocated 'file_size - (file_size & 7)'
bytes and assigned to 'long int *' here; 'sizeof (long int)' is '8'
    |

TL;DR the background is: I'm doing an experiment to change LoongArch
-mcheck-zero-division to an alias of -fsanitize=integer-divide-by-zero
-fsanitize-trap=integer-divide-by-zero instead of some magic in the machine
description.  I attempted to keep -mcheck-zero-division the default at -O0/-Og
for "backward compatibility" but then my attempt effectively turned
-fsanitize=integer-divide-by-zero -fsanitize-trap=integer-divide-by-zero on by
default and caused a bunch of test failures not expecting the sanitizer.  Maybe
I'll just abandon my attempt and make -mno-check-zero-division the default for
all optimization levels, but I think this bug report is valid on its own...

Reply via email to