(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
analyzer/
imprecise-floating-point-1.c
       1  /* { dg-require-effective-target alloca } */
       2  
       3  #include <stdlib.h>
       4  
       5  /* Tests warn on use of floating-point operands inside the calculation
       6     of an allocation size.
       7  
       8     The test cases here only test for warnings.  The test cases inside
       9     allocation-size-X.c should be plently enough to test for false positives.  */
      10  
      11  void test_1 (float f)
      12  {
      13    int *ptr = malloc (sizeof (int) * f); /* { dg-line test_1 } */
      14    free (ptr);
      15  
      16    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_1 } */
      17    /* { dg-message "operand 'f' is of type 'float'" "note" { target *-*-* } test_1 } */
      18    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_1 } */
      19  }
      20  
      21  void test_2 (int n)
      22  {
      23    int *ptr = malloc (n * 3.1); /* { dg-line test_2 } */
      24    free (ptr);
      25  
      26    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_2 } */
      27    /* { dg-message "operand '\(\\d|e|f|\\.|\\+|\)+' is of type 'double'" "note" { target *-*-* } test_2 } */
      28    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_2 } */
      29  }
      30  
      31  void *alloc_me (size_t size)
      32  {
      33    return malloc (size); /* { dg-line test_3 } */
      34  
      35    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_3 } */
      36    /* { dg-message "operand 'f' is of type 'float'" "note" { target *-*-* } test_3 } */
      37    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_3 } */
      38  }
      39  
      40  void test_3 (float f)
      41  {
      42    void *ptr = alloc_me (f); /* { dg-message "calling 'alloc_me' from 'test_3'" } */
      43    free (ptr);
      44  }
      45  
      46  void test_4 (int n)
      47  {
      48    int *ptr = calloc(1.7 * n, sizeof (int)); /* { dg-line test_4 } */
      49    free (ptr);
      50  
      51    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_4 } */
      52    /* { dg-message "operand '\(\\d|e|f|\\.|\\+|\)+' is of type 'double'" "note" { target *-*-* } test_4 } */
      53    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_4 } */
      54  }
      55  
      56  int test_5 (float f)
      57  {
      58    int *ptr = __builtin_alloca (sizeof (int) * f); /* { dg-line test_5 } */
      59    *ptr = 4;
      60    return *ptr;
      61  
      62    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_5 } */
      63    /* { dg-message "operand 'f' is of type 'float'" "note" { target *-*-* } test_5 } */
      64    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_5 } */
      65  }
      66  
      67  int test_6 (float f)
      68  {
      69    int *ptr = __builtin_alloca (1.7f * f * 2.3f); /* { dg-line test_6 } */
      70    *ptr = 4;
      71    return *ptr;
      72  
      73    /* { dg-warning "use of floating-point arithmetic here might yield unexpected results" "warning" { target *-*-* } test_6 } */
      74    /* { dg-message "operand 'f' is of type 'float'" "note" { target *-*-* } test_6 } */
      75    /* { dg-message "only use operands of an integer type inside the size argument" "note" { target *-*-* } test_6 } */
      76  }