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

            Bug ID: 128314
           Summary: wrong code at -O1 and above: an overflowing
                    __builtin___memcpy_chk into an unused local array is
                    removed instead of aborting
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <stdio.h>

int main(void)
{
  char d[1];
  volatile __SIZE_TYPE__ n = 4;
  __builtin___memcpy_chk(d, "abc", n, sizeof d);
  puts("no abort");
  return 0;
}

godbolt: https://godbolt.org/z/vh1YKdzPf

The call copies 4 bytes into the 1-byte d and passes its true size, so it must
abort with "*** buffer overflow detected ***: terminated", as it does at -O0
and with -O1 -fno-tree-dse or -O2 -fno-tree-dse. At -O1, -O2, -O3 and -Os no
__memcpy_chk call is emitted and the program prints "no abort".

Reply via email to