1 /* PR tree-optimization/90879 - fold zero-equality of strcmp between
2 a longer string and a smaller array
4 { dg-options "-O2 -Wall -Wextra -ftrack-macro-expansion=0" } */
8 #define T(a, b) sink (0 == strcmp (a, b), a, b)
12 struct S
{ char a4
[4], c
; };
18 /* Verify that comparison of string literals with arrays with unknown
19 content but size that prevents them from comparing equal is diagnosed. */
21 void strcmp_array_lit (void)
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 }
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" }
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 " }
41 void strcmp_array_pstr (void)
43 const char *s4
= "1234";
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 }
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" }
62 const char *t4
= "4321";
63 const char *s5
= "12345";
64 const char *s6
= "123456";
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 " }
75 void strcmp_array_cond_pstr (int i
)
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 " }
82 void strcmp_array_copy (void)
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 }
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" }
107 T (a4
, s
); // { dg-warning "'strcmp' of a string of length 4 and an array of size 4 evaluates to nonzero " }
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 " }
113 T (s
, a4
); // { dg-warning "length 4 and an array of size 4 " }
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 " }
121 void strcmp_member_array_lit (const struct S
*p
)
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 *-*-* } }
129 #define T(a, b, n) sink (0 == strncmp (a, b, n), a, b)
131 void strncmp_array_lit (void)
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 }
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" }
142 // Verify no warning when the bound is the same as the array size.
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" }
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 " }
157 void strncmp_strarray_copy (void)
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 }
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" }
179 T (a4
, "54321", 5); // { dg-warning "'strncmp' of strings of length 3 and 5 and bound of 5 evaluates to nonzero " }