(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Walloc-size-larger-than-18.c
       1  /* PR tree-optimization/88372 - alloc_size attribute is ignored
       2     on function pointers
       3     Verify that calls via function pointers declared alloc_size
       4     with zero or excessive size trigger either -Walloc-zero or
       5     -Walloc-size-larger-than warnings.
       6     { dg-do compile }
       7     { dg-require-effective-target indirect_calls }
       8     { dg-options "-O2 -Wall -Walloc-zero -ftrack-macro-expansion=0" } */
       9  
      10  #define ATTR(...) __attribute__ ((__VA_ARGS__))
      11  
      12  typedef __SIZE_TYPE__ size_t;
      13  
      14  
      15  void sink (void*);
      16  
      17  #define T(call) sink (call)
      18  
      19  ATTR (alloc_size (1)) void* (*ai1)(int, int);
      20  ATTR (alloc_size (2)) void* (*ai2)(int, int);
      21  ATTR (alloc_size (1, 2)) void* (*ai1_2)(int, int);
      22  
      23  ATTR (alloc_size (1)) void* (*asz1)(size_t, size_t);
      24  ATTR (alloc_size (2)) void* (*asz2)(size_t, size_t);
      25  ATTR (alloc_size (1, 2)) void* (*asz1_2)(size_t, size_t);
      26  
      27  
      28  void test_alloc_ptr_zero (void)
      29  {
      30    T (asz1 (0, 0));      /* { dg-warning "argument 1 value is zero" } */
      31    T (asz1 (0, 1));      /* { dg-warning "argument 1 value is zero" } */
      32    T (asz1 (1, 0));
      33    T (asz1 (1, 1));
      34  
      35    T (asz2 (0, 0));      /* { dg-warning "argument 2 value is zero" } */
      36    T (asz2 (0, 1));
      37    T (asz2 (1, 0));      /* { dg-warning "argument 2 value is zero" } */
      38    T (asz2 (1, 1));
      39  
      40    T (asz1_2 (0, 0));    /* { dg-warning "argument \[12\] value is zero" } */
      41    T (asz1_2 (1, 0));    /* { dg-warning "argument 2 value is zero" } */
      42    T (asz1_2 (0, 1));    /* { dg-warning "argument 1 value is zero" } */
      43    T (asz1_2 (1, 1));
      44  }
      45  
      46  
      47  void test_alloc_ptr_negative (int n)
      48  {
      49    T (ai1 (-1, -1));     /* { dg-warning "argument 1 value .-1. is negative" } */
      50    T (ai1 (-2,  1));     /* { dg-warning "argument 1 value .-2. is negative" } */
      51    T (ai1 ( 1, -1));
      52    T (ai1 ( 1,  1));
      53  
      54    T (ai2 (-1, -3));     /* { dg-warning "argument 2 value .-3. is negative" } */
      55    T (ai2 (-1,  1));
      56    T (ai2 ( 1, -4));     /* { dg-warning "argument 2 value .-4. is negative" } */
      57    T (ai2 ( 1,  1));
      58  
      59    T (ai1_2 (-5, -6));   /* { dg-warning "argument \[12\] value .-\[56\]. is negative" } */
      60    T (ai1_2 ( 1, -7));   /* { dg-warning "argument 2 value .-7. is negative" } */
      61    T (ai1_2 (-8,  1));   /* { dg-warning "argument 1 value .-8. is negative" } */
      62    T (ai1_2 ( 1,  1));
      63  
      64    if (n > -1)
      65      n = -1;
      66  
      67    /* Also verify a simple range.  */
      68    T (ai1_2 ( 1,  n));   /* { dg-warning "argument 2 range \\\[-\[0-9\]+, -1] is negative" } */
      69    T (ai1_2 ( n,  1));   /* { dg-warning "argument 1 range \\\[-\[0-9\]+, -1] is negative" } */
      70  }
      71  
      72  void test_alloc_ptr_too_big (void)
      73  {
      74    size_t x = (__SIZE_MAX__ >> 1) + 1;
      75    size_t y = __SIZE_MAX__ / 5;
      76  
      77    T (asz1 (x, x));     /* { dg-warning "argument 1 value .\[0-9\]+. exceeds" } */
      78    T (asz1 (x, 1));     /* { dg-warning "argument 1 value .\[0-9\]+. exceeds" } */
      79    T (asz1 (1, x));
      80    T (asz1 (1, 1));
      81  
      82    T (asz2 (x, x));     /* { dg-warning "argument 2 value .\[0-9\]+. exceeds" } */
      83    T (asz2 (x, 1));
      84    T (asz2 (1, x));     /* { dg-warning "argument 2 value .\[0-9\]+. exceeds" } */
      85    T (asz2 (1, 1));
      86  
      87    T (asz1_2 (x, x));   /* { dg-warning "argument \[12\] value .\[0-9\]+. exceeds" } */
      88    T (asz1_2 (y, 3));   /* { dg-warning "product .\[0-9\]+ \\\* 3. of arguments 1 and 2 exceeds" } */
      89    T (asz1_2 (y, y));   /* { dg-warning "product .\[0-9\]+ \\\* \[0-9\]+. of arguments 1 and 2 exceeds" } */
      90    T (asz1_2 (1, x));   /* { dg-warning "argument 2 value .\[0-9\]+. exceeds" } */
      91    T (asz1_2 (x, 1));   /* { dg-warning "argument 1 value .\[0-9\]+. exceeds" } */
      92    T (asz1_2 (1, 1));
      93  
      94  }