(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
attr-alloc_size-6.c
       1  /* PR c/78284 - warn on malloc with very large arguments
       2     Test exercising the ability of the built-in allocation functions
       3     to detect and diagnose, without optimization, calls that attemnpt
       4     to allocate objects in excess of the number of bytes specified by
       5     -Walloc-larger-than=maximum.  */
       6  /* { dg-do compile } */
       7  /* { dg-require-effective-target alloca } */
       8  /* { dg-options "-O0 -Wall -Walloc-size-larger-than=12345 -Wno-use-after-free" } */
       9  
      10  #define MAXOBJSZ  12345
      11  
      12  typedef __SIZE_TYPE__ size_t;
      13  
      14  void sink (void*);
      15  
      16  
      17  void test_lit (char *p, char *q)
      18  {
      19    sink (__builtin_aligned_alloc (1, MAXOBJSZ));
      20    sink (__builtin_aligned_alloc (1, MAXOBJSZ + 1));   /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
      21  
      22    sink (__builtin_alloca (MAXOBJSZ));
      23    sink (__builtin_alloca (MAXOBJSZ + 2));   /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
      24  
      25    sink (__builtin_calloc (MAXOBJSZ, 1));
      26    sink (__builtin_calloc (1, MAXOBJSZ));
      27  
      28    /* Verify that the signed to unsigned conversion below doesn't cause
      29       a warning.  */
      30    sink (__builtin_calloc (p - q, 1));
      31    sink (__builtin_calloc (1, q - p));
      32    sink (__builtin_calloc (p - q, MAXOBJSZ));
      33    sink (__builtin_calloc (MAXOBJSZ, q - p));
      34  
      35    sink (__builtin_calloc (MAXOBJSZ / 2, 3));   /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
      36    sink (__builtin_calloc (4, MAXOBJSZ / 3));   /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
      37  
      38    sink (__builtin_malloc (MAXOBJSZ));
      39    sink (__builtin_malloc (MAXOBJSZ + 3));   /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
      40  
      41    sink (__builtin_realloc (p, MAXOBJSZ));
      42    sink (__builtin_realloc (p, MAXOBJSZ + 4));  /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */
      43  }
      44  
      45  
      46  enum { max = MAXOBJSZ };
      47  
      48  void test_cst (char *p, char *q)
      49  {
      50    sink (__builtin_aligned_alloc (1, max));
      51    sink (__builtin_aligned_alloc (1, max + 1));   /* { dg-warning "argument 2 value .12346\[lu\]*. exceeds maximum object size 12345" } */
      52  
      53    sink (__builtin_alloca (max));
      54    sink (__builtin_alloca (max + 2));   /* { dg-warning "argument 1 value .12347\[lu\]*. exceeds maximum object size 12345" } */
      55  
      56    sink (__builtin_calloc (max, 1));
      57    sink (__builtin_calloc (1, max));
      58  
      59    /* Verify that the signed to unsigned conversion below doesn't cause
      60       a warning.  */
      61    sink (__builtin_calloc (p - q, 1));
      62    sink (__builtin_calloc (1, q - p));
      63    sink (__builtin_calloc (p - q, max));
      64    sink (__builtin_calloc (max, q - p));
      65  
      66    sink (__builtin_calloc (max / 2, 3));   /* { dg-warning "product .6172\[lu\]* \\* 3\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
      67    sink (__builtin_calloc (4, max / 3));   /* { dg-warning "product .4\[lu\]* \\* 4115\[lu\]*. of arguments 1 and 2 exceeds maximum object size 12345" } */
      68  
      69    sink (__builtin_malloc (max));
      70    sink (__builtin_malloc (max + 3));   /* { dg-warning "argument 1 value .12348\[lu\]*. exceeds maximum object size 12345" } */
      71  
      72    sink (__builtin_realloc (p, max));
      73    sink (__builtin_realloc (p, max + 4));  /* { dg-warning "argument 2 value .12349\[lu\]*. exceeds maximum object size 12345" } */
      74  }