(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
Wstringop-overflow-76-novec.c
       1  /* Verify warnings and notes for MAX_EXPRs involving either pointers
       2     to distinct objects or one to a known object and the other to
       3     an unknown one.  Unlike for the same object, for unrelated objects
       4     the expected warnings and notes are the same as for MIN_EXPR: when
       5     the order of the objects in the address space cannot be determined
       6     the larger of them is assumed to be used.  (This is different for
       7     distinct struct members where the order is given.)
       8     The relational expressions are strictly invalid but that should be
       9     diagnosed by a separate warning.
      10     { dg-do compile }
      11     { dg-options "-O2 -Wno-array-bounds -fno-tree-vectorize" } */
      12  
      13  #define MAX(p, q) ((p) > (q) ? (p) : (q))
      14  
      15  /* Verify that even for MAX_EXPR and like for MIN_EXPR, the note points
      16     to the larger of the two objects and mentions the offset into it
      17     (although the offset might be better included in the warning).  */
      18  extern char a3[3];
      19  extern char a5[5];  // { dg-message "at offset 5 into destination object 'a5' of size 5" "note" }
      20  
      21  void max_a3_a5 (int i)
      22  {
      23    char *p = a3 + i;
      24    char *q = a5 + i;
      25  
      26    /* The relational expression below is invalid and should be diagnosed
      27       by its own warning independently of -Wstringop-overflow.  */
      28    char *d = MAX (p, q);
      29  
      30    d[2] = 0;
      31    d[3] = 0;
      32    d[4] = 0;
      33    d[5] = 0;         // { dg-warning "writing 1 byte into a region of size 0" }
      34  }
      35  
      36  
      37  // Same as above but with the larger array as the first MAX_EXPR operand.
      38  extern char b4[4];
      39  extern char b6[6];  // { dg-message "at offset 6 into destination object 'b6' of size 6" "note" }
      40  
      41  void max_b6_b4 (int i)
      42  {
      43    char *p = b6 + i;
      44    char *q = b4 + i;
      45    char *d = MAX (p, q);
      46  
      47    d[3] = 0;
      48    d[4] = 0;
      49    d[5] = 0;
      50    d[6] = 0;         // { dg-warning "writing 1 byte into a region of size 0" }
      51  }
      52  
      53  struct A3_5
      54  {
      55    char a3[3];  // { dg-message "at offset 3 into destination object 'a3' of size 3" "pr??????" { xfail *-*-* } }
      56    char a5[5];  // { dg-message "at offset 5 into destination object 'a5' of size 5" "note" }
      57  };
      58  
      59  void max_A3_A5 (int i, struct A3_5 *pa3_5)
      60  {
      61    char *p = pa3_5->a3 + i;
      62    char *q = pa3_5->a5 + i;
      63  
      64    char *d = MAX (p, q);
      65    d[2] = 0;
      66    d[3] = 0;         // { dg-warning "writing 1 byte into a region of size 0" "pr??????" { xfail *-*-* } }
      67    d[4] = 0;
      68    d[5] = 0;         // { dg-warning "writing 1 byte into a region of size 0" }
      69  }
      70  
      71  
      72  struct B4_B6
      73  {
      74    char b4[4];
      75    char b6[6];       // { dg-message "at offset 6 into destination object 'b6' of size 6" "note" }
      76  };
      77  
      78  void max_B6_B4 (int i, struct B4_B6 *pb4_b6)
      79  {
      80    char *p = pb4_b6->b6 + i;
      81    char *q = pb4_b6->b4 + i;
      82    char *d = MAX (p, q);
      83  
      84    d[3] = 0;
      85    d[4] = 0;
      86    d[5] = 0;
      87    d[6] = 0;         // { dg-warning "writing 1 byte into a region of size 0" }
      88  }