| 1 |
/* PR middle-end/81384 - built-in form of strnlen missing |
| 2 |
|
| 3 |
Since the strnlen patch affects the handling for strncmp and other |
| 4 |
bounded functions, verify that a bound in excess of the maximum |
| 5 |
object size specified for strncmp is diagnosed regardless of |
| 6 |
attribute nonstring. Also verify that a bound that's greater than |
| 7 |
the size of a non-string array is diagnosed, even if it's not in |
| 8 |
excess of the maximum object size. |
| 9 |
|
| 10 |
{ dg-do compile } |
| 11 |
{ dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */ |
| 12 |
|
| 13 |
#include "range.h" |
| 14 |
|
| 15 |
extern int strncmp (const char*, const char*, size_t); |
| 16 |
|
| 17 |
#define STR /* not non-string */ |
| 18 |
#define NS __attribute__ ((nonstring)) |
| 19 |
|
| 20 |
#define _CAT(s, n) s ## n |
| 21 |
#define CAT(s, n) _CAT (s, n) |
| 22 |
#define UNIQ(n) CAT (n, __LINE__) |
| 23 |
|
| 24 |
void sink (int); |
| 25 |
|
| 26 |
#define T(at1, N1, at2, N2, bound) \ |
| 27 |
do { \ |
| 28 |
extern at1 char UNIQ (a)[N1]; \ |
| 29 |
extern at2 char UNIQ (b)[N2]; \ |
| 30 |
sink (strncmp (UNIQ (a), UNIQ (b), bound)); \ |
| 31 |
} while (0) |
| 32 |
|
| 33 |
void strncmp_cst (void) |
| 34 |
{ |
| 35 |
size_t n = DIFF_MAX; |
| 36 |
|
| 37 |
T (STR, /* [] */, STR, /* [] */, n); |
| 38 |
T (STR, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 39 |
|
| 40 |
T (STR, 1, STR, /* [] */, 1); |
| 41 |
T (STR, 1, STR, /* [] */, n); |
| 42 |
T (STR, 2, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 43 |
|
| 44 |
T (STR, /* [] */, STR, 3, 3); |
| 45 |
T (STR, /* [] */, STR, 3, n); |
| 46 |
T (STR, /* [] */, STR, 4, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 47 |
|
| 48 |
T (STR, /* [] */, NS, /* [] */, 3); |
| 49 |
T (STR, /* [] */, NS, /* [] */, n); |
| 50 |
T (STR, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 51 |
|
| 52 |
T (STR, 5, NS, /* [] */, 4); |
| 53 |
T (STR, 5, NS, /* [] */, 5); |
| 54 |
T (STR, 5, NS, /* [] */, 6); |
| 55 |
T (STR, 5, NS, /* [] */, n); |
| 56 |
T (STR, 6, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 57 |
|
| 58 |
T (STR, /* [] */, NS, 7, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */ |
| 59 |
|
| 60 |
T (STR, /* [] */, NS, 8, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 61 |
|
| 62 |
T (NS, /* [] */, STR, /* [] */, n); |
| 63 |
T (NS, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 64 |
|
| 65 |
T (NS, 9, STR, /* [] */, n); /* { dg-warning "argument 1 declared attribute 'nonstring' is smaller than the specified bound \[0-9\]+" } */ |
| 66 |
T (NS, 10, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 67 |
|
| 68 |
T (NS, /* [] */, STR, 11, 11); |
| 69 |
T (NS, /* [] */, STR, 11, n); |
| 70 |
T (NS, /* [] */, STR, 12, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 71 |
|
| 72 |
T (NS, /* [] */, NS, /* [] */, n); |
| 73 |
T (NS, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 74 |
|
| 75 |
T (NS, 13, NS, /* [] */, 13); |
| 76 |
T (NS, 13, NS, /* [] */, n); /* { dg-warning "argument 1 declared attribute 'nonstring' is smaller than the specified bound \[0-9\]+" } */ |
| 77 |
T (NS, 14, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 78 |
|
| 79 |
T (NS, /* [] */, NS, 15, 15); |
| 80 |
T (NS, /* [] */, NS, 15, 16); /* { dg-warning "argument 2 declared attribute 'nonstring' is smaller than the specified bound 16" } */ |
| 81 |
T (NS, /* [] */, NS, 16, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */ |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
void strncmp_range (void) |
| 86 |
{ |
| 87 |
size_t n = DIFF_MAX; |
| 88 |
n = UR (n, n + 1); |
| 89 |
|
| 90 |
T (STR, /* [] */, STR, /* [] */, n); |
| 91 |
T (STR, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 92 |
|
| 93 |
T (STR, 1, STR, /* [] */, 1); |
| 94 |
T (STR, 1, STR, /* [] */, n); |
| 95 |
T (STR, 2, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 96 |
|
| 97 |
T (STR, /* [] */, STR, 3, n); |
| 98 |
T (STR, /* [] */, STR, 4, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 99 |
|
| 100 |
T (STR, /* [] */, NS, /* [] */, n); |
| 101 |
T (STR, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 102 |
|
| 103 |
T (STR, 5, NS, /* [] */, n); |
| 104 |
T (STR, 6, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 105 |
|
| 106 |
T (STR, /* [] */, NS, 7, n); /* { dg-warning "argument 2 declared attribute 'nonstring' is smaller than the specified bound \\\[\[0-9\]+, \[0-9\]+]" } */ |
| 107 |
|
| 108 |
T (STR, /* [] */, NS, 8, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 109 |
|
| 110 |
T (NS, /* [] */, STR, /* [] */, n); |
| 111 |
T (NS, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 112 |
|
| 113 |
T (NS, 9, STR, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */ |
| 114 |
T (NS, 10, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 115 |
|
| 116 |
T (NS, /* [] */, STR, 11, n); |
| 117 |
T (NS, /* [] */, STR, 12, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 118 |
|
| 119 |
T (NS, /* [] */, NS, /* [] */, n); |
| 120 |
T (NS, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 121 |
|
| 122 |
T (NS, 13, NS, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */ |
| 123 |
T (NS, 14, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 124 |
|
| 125 |
T (NS, /* [] */, NS, 15, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */ |
| 126 |
T (NS, /* [] */, NS, 16, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */ |
| 127 |
} |