| 1 |
/* PR tree-optimization/90879 - fold zero-equality of strcmp between |
| 2 |
a longer string and a smaller array |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wall -Wextra -ftrack-macro-expansion=0" } */ |
| 5 |
|
| 6 |
#include "strlenopt.h" |
| 7 |
|
| 8 |
#define T(a, b) sink (0 == strcmp (a, b), a, b) |
| 9 |
|
| 10 |
void sink (int, ...); |
| 11 |
|
| 12 |
struct S { char a4[4], c; }; |
| 13 |
|
| 14 |
extern char a4[4]; |
| 15 |
extern char a5[5]; |
| 16 |
extern char b4[4]; |
| 17 |
|
| 18 |
/* Verify that comparison of string literals with arrays with unknown |
| 19 |
content but size that prevents them from comparing equal is diagnosed. */ |
| 20 |
|
| 21 |
void strcmp_array_lit (void) |
| 22 |
{ |
| 23 |
if (strcmp (a4, "1234")) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 24 |
// { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 } |
| 25 |
sink (0, a4); |
| 26 |
|
| 27 |
int cmp; |
| 28 |
cmp = strcmp (a4, "1234"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 29 |
if (cmp) // { dg-message "in this expression" } |
| 30 |
sink (0, a4); |
| 31 |
|
| 32 |
T (a4, "4321"); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " } |
| 33 |
T (a4, "12345"); // { dg-warning "length 5 and an array of size 4 " } |
| 34 |
T (a4, "123456"); // { dg-warning "length 6 and an array of size 4 " } |
| 35 |
T ("1234", a4); // { dg-warning "length 4 and an array of size 4 " } |
| 36 |
T ("12345", a4); // { dg-warning "length 5 and an array of size 4 " } |
| 37 |
T ("123456", a4); // { dg-warning "length 6 and an array of size 4 " } |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
void strcmp_array_pstr (void) |
| 42 |
{ |
| 43 |
const char *s4 = "1234"; |
| 44 |
|
| 45 |
{ |
| 46 |
if (strcmp (a4, s4)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 47 |
// { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 } |
| 48 |
sink (1, a4); |
| 49 |
else |
| 50 |
sink (0, a4); |
| 51 |
} |
| 52 |
|
| 53 |
{ |
| 54 |
int c; |
| 55 |
c = strcmp (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 56 |
if (c) // { dg-message "in this expression" } |
| 57 |
sink (1, a4); |
| 58 |
else |
| 59 |
sink (0, a4); |
| 60 |
} |
| 61 |
|
| 62 |
const char *t4 = "4321"; |
| 63 |
const char *s5 = "12345"; |
| 64 |
const char *s6 = "123456"; |
| 65 |
|
| 66 |
T (a4, t4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " } |
| 67 |
T (a4, s5); // { dg-warning "length 5 and an array of size 4 " } |
| 68 |
T (a4, s6); // { dg-warning "length 6 and an array of size 4 " } |
| 69 |
T (s4, a4); // { dg-warning "length 4 and an array of size 4 " } |
| 70 |
T (s5, a4); // { dg-warning "length 5 and an array of size 4 " } |
| 71 |
T (s6, a4); // { dg-warning "length 6 and an array of size 4 " } |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
void strcmp_array_cond_pstr (int i) |
| 76 |
{ |
| 77 |
const char *s4 = i ? "1234" : "4321"; |
| 78 |
T (a4, s4); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " } |
| 79 |
T (a5, s4); |
| 80 |
} |
| 81 |
|
| 82 |
void strcmp_array_copy (void) |
| 83 |
{ |
| 84 |
char s[8]; |
| 85 |
|
| 86 |
{ |
| 87 |
strcpy (s, "1234"); |
| 88 |
if (strcmp (a4, s)) // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 89 |
// { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 } |
| 90 |
sink (1, a4); |
| 91 |
else |
| 92 |
sink (0, a4); |
| 93 |
} |
| 94 |
|
| 95 |
{ |
| 96 |
strcpy (s, "1234"); |
| 97 |
|
| 98 |
int c; |
| 99 |
c = strcmp (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero" } |
| 100 |
if (c) // { dg-message "in this expression" } |
| 101 |
sink (1, a4); |
| 102 |
else |
| 103 |
sink (0, a4); |
| 104 |
} |
| 105 |
|
| 106 |
strcpy (s, "4321"); |
| 107 |
T (a4, s); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " } |
| 108 |
strcpy (s, "12345"); |
| 109 |
T (a4, s); // { dg-warning "length 5 and an array of size 4 " } |
| 110 |
strcpy (s, "123456"); |
| 111 |
T (a4, s); // { dg-warning "length 6 and an array of size 4 " } |
| 112 |
strcpy (s, "4321"); |
| 113 |
T (s, a4); // { dg-warning "length 4 and an array of size 4 " } |
| 114 |
strcpy (s, "54321"); |
| 115 |
T (s, a4); // { dg-warning "length 5 and an array of size 4 " } |
| 116 |
strcpy (s, "654321"); |
| 117 |
T (s, a4); // { dg-warning "length 6 and an array of size 4 " } |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
void strcmp_member_array_lit (const struct S *p) |
| 122 |
{ |
| 123 |
// Not handled due to the fix for PR 92756. |
| 124 |
T (p->a4, "1234"); // { dg-warning "length 4 and an array of size 4 " "pr92765" { xfail *-*-* } } |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
#undef T |
| 129 |
#define T(a, b, n) sink (0 == strncmp (a, b, n), a, b) |
| 130 |
|
| 131 |
void strncmp_array_lit (void) |
| 132 |
{ |
| 133 |
if (strncmp (a4, "12345", 5)) // { dg-warning "'strncmp' of a string of length 5, an array of size 4 and bound of 5 evaluates to nonzero" } |
| 134 |
// { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 } |
| 135 |
sink (0, a4); |
| 136 |
|
| 137 |
int cmp; |
| 138 |
cmp = strncmp (a4, "54321", 5); // { dg-warning "'strncmp' of a string of length 5, an array of size 4 and bound of 5 evaluates to nonzero" } |
| 139 |
if (cmp) // { dg-message "in this expression" } |
| 140 |
sink (0, a4); |
| 141 |
|
| 142 |
// Verify no warning when the bound is the same as the array size. |
| 143 |
T (a4, "4321", 4); |
| 144 |
T (a4, "654321", 4); |
| 145 |
|
| 146 |
T (a4, "12345", 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " } |
| 147 |
T (a4, "123456", 6); // { dg-warning "length 6, an array of size 4 and bound of 6" } |
| 148 |
|
| 149 |
T ("1234", a4, 4); |
| 150 |
T ("12345", a4, 4); |
| 151 |
|
| 152 |
T ("12345", a4, 5); // { dg-warning "length 5, an array of size 4 and bound of 5 " } |
| 153 |
T ("123456", a4, 6); // { dg-warning "length 6, an array of size 4 and bound of 6 " } |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void strncmp_strarray_copy (void) |
| 158 |
{ |
| 159 |
{ |
| 160 |
char a[] = "1234"; |
| 161 |
char b[6]; |
| 162 |
strcpy (b, "12345"); |
| 163 |
if (strncmp (a, b, 5)) // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" } |
| 164 |
// { dg-bogus "in this expression" "unwanted note" { target *-*-* } .-1 } |
| 165 |
sink (0, a, b); |
| 166 |
} |
| 167 |
|
| 168 |
{ |
| 169 |
char a[] = "4321"; |
| 170 |
char b[6]; |
| 171 |
strcpy (b, "54321"); |
| 172 |
int cmp; |
| 173 |
cmp = strncmp (a, b, 5); // { dg-warning "'strncmp' of strings of length 4 and 5 and bound of 5 evaluates to nonzero" } |
| 174 |
if (cmp) // { dg-message "in this expression" } |
| 175 |
sink (0, a, b); |
| 176 |
} |
| 177 |
|
| 178 |
strcpy (a4, "abc"); |
| 179 |
T (a4, "54321", 5); // { dg-warning "'strncmp' of strings of length 3 and 5 and bound of 5 evaluates to nonzero " } |
| 180 |
} |
| 181 |
|
| 182 |
|