(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
out-of-bounds-zero.c
       1  /* { dg-additional-options "-Wno-stringop-overflow"} */
       2  /* -Wstringop-overflow= triggers on test5.  */
       3  
       4  #include <stdint.h>
       5  #include <stdlib.h>
       6  
       7  void test1 (void)
       8  {
       9    int32_t buf[1];
      10    /* Zero bytes written on non-zero allocation.  */
      11    __builtin_memset (buf, 0, 0);
      12  }
      13  
      14  void test2 (void)
      15  {
      16    /* ISO C forbids zero-size arrays but GCC compiles this to an
      17       zero-sized array without -Wpedantic.  */
      18    int32_t buf[0];
      19    /* Write on zero capacity.  */
      20    __builtin_memset (buf, 0, sizeof (int32_t)); /* { dg-line test2 } */
      21  
      22    /* { dg-warning "overflow" "warning" { target *-*-* } test2 } */
      23    /* { dg-message "from byte 0 till byte 3" "final event" { target *-*-* } test2 } */
      24  }
      25  
      26  void test3 (void)
      27  {
      28    int32_t buf[0];
      29    /* Zero bytes written on zero capacity.  */
      30    __builtin_memset (buf, 0, 0);
      31  }
      32  
      33  void test4 (void)
      34  {
      35    int32_t *buf = malloc (sizeof (int32_t));
      36    if (!buf)
      37      return;
      38  
      39    /* Zero bytes written on non-zero allocation.  */
      40    __builtin_memset (buf, 0, 0);
      41    free (buf);
      42  }
      43  
      44  void test5 (void)
      45  {
      46    int32_t *buf = malloc (0);
      47    if (!buf)
      48      return;
      49  
      50    /* Write on zero capacity.  */
      51    __builtin_memset (buf, 0, sizeof (int32_t)); /* { dg-line test5 } */
      52    free (buf);
      53  
      54    /* { dg-warning "overflow" "warning" { target *-*-* } test5 } */
      55    /* { dg-message "from byte 0 till byte 3" "final event" { target *-*-* } test5 } */
      56  }
      57  
      58  void test6 (void)
      59  {
      60    int32_t *buf = malloc (0);
      61    if (!buf)
      62      return;
      63  
      64    /* Zero bytes written on zero capacity.  */
      65    __builtin_memset (buf, 0, 0);
      66    free (buf);
      67  }