| 1 |
/* PR c/17308 - nonnull attribute not as useful as it could be |
| 2 |
PR c/78673 - sprintf missing attribute nonnull on destination argument |
| 3 |
{ dg-do "compile" } |
| 4 |
{ dg-additional-options "-O2 -Wnonnull -ftrack-macro-expansion=0 -std=c99" } */ |
| 5 |
|
| 6 |
#define va_list __builtin_va_list |
| 7 |
|
| 8 |
typedef struct FILE FILE; |
| 9 |
|
| 10 |
static char* null (void) |
| 11 |
{ |
| 12 |
return 0; |
| 13 |
} |
| 14 |
|
| 15 |
void sink (int, ...); |
| 16 |
#define T(arg) sink (0, arg) |
| 17 |
|
| 18 |
|
| 19 |
#define bzero __builtin_bzero |
| 20 |
#define memcpy __builtin_memcpy |
| 21 |
#define memmove __builtin_memmove |
| 22 |
#define mempcpy __builtin_mempcpy |
| 23 |
#define memset __builtin_memset |
| 24 |
|
| 25 |
void test_memfuncs (void *s, unsigned n) |
| 26 |
{ |
| 27 |
if (n == 0) |
| 28 |
return; |
| 29 |
|
| 30 |
/* Bzero is not declared attribute nonnull (maybe it should be?) |
| 31 |
but it's transformed into a call to memset() which is. */ |
| 32 |
bzero (null (), n); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 33 |
|
| 34 |
T (memcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 35 |
T (memcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 36 |
|
| 37 |
T (memmove (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 38 |
T (memmove (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 39 |
|
| 40 |
T (mempcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 41 |
T (mempcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 42 |
|
| 43 |
T (memset (null (), 0, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 44 |
} |
| 45 |
|
| 46 |
#undef memcpy |
| 47 |
#undef memmove |
| 48 |
#undef mempcpy |
| 49 |
#undef memset |
| 50 |
#define memcpy(d, s, n) __builtin___memcpy_chk (d, s, n, n) |
| 51 |
#define memmove(d, s, n) __builtin___memmove_chk (d, s, n, n) |
| 52 |
#define mempcpy(d, s, n) __builtin___mempcpy_chk (d, s, n, n) |
| 53 |
#define memset(d, x, n) __builtin___memset_chk (d, x, n, n) |
| 54 |
|
| 55 |
void test_memfuncs_chk (void *s, unsigned n) |
| 56 |
{ |
| 57 |
if (n == 0) |
| 58 |
return; |
| 59 |
|
| 60 |
T (memcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 61 |
T (memcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 62 |
|
| 63 |
T (memmove (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 64 |
T (memmove (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 65 |
|
| 66 |
T (mempcpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 67 |
T (mempcpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 68 |
|
| 69 |
T (memset (null (), 0, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
#define strcat __builtin_strcat |
| 74 |
#define strchr __builtin_strchr |
| 75 |
#define stpcpy __builtin_stpcpy |
| 76 |
#define stpncpy __builtin_stpncpy |
| 77 |
#define strcpy __builtin_strcpy |
| 78 |
#define strncpy __builtin_strncpy |
| 79 |
#define strlen __builtin_strlen |
| 80 |
#define strncat __builtin_strncat |
| 81 |
#define strstr __builtin_strstr |
| 82 |
|
| 83 |
void test_strfuncs (char *s, unsigned n) |
| 84 |
{ |
| 85 |
if (n == 0) |
| 86 |
return; |
| 87 |
|
| 88 |
T (strcat (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 89 |
T (strcat (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 90 |
|
| 91 |
T (strchr (null (), 'x')); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 92 |
|
| 93 |
T (stpcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 94 |
T (stpcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 95 |
|
| 96 |
T (stpncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 97 |
T (stpncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 98 |
|
| 99 |
T (strcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 100 |
T (strcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 101 |
|
| 102 |
T (strncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 103 |
T (strncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 104 |
|
| 105 |
T (strlen (null ())); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 106 |
|
| 107 |
T (strncat (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 108 |
T (strncat (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 109 |
|
| 110 |
T (strstr (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 111 |
T (strstr (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
#undef strcat |
| 116 |
#undef stpcpy |
| 117 |
#undef stpncpy |
| 118 |
#undef strcpy |
| 119 |
#undef strncpy |
| 120 |
#undef strncat |
| 121 |
|
| 122 |
#define strcat(d, s) __builtin___strcat_chk (d, s, n) |
| 123 |
#define stpcpy(d, s) __builtin___stpcpy_chk (d, s, n) |
| 124 |
#define stpncpy(d, s, n) __builtin___stpncpy_chk (d, s, n, n) |
| 125 |
#define strcpy(d, s) __builtin___strcpy_chk (d, s, n) |
| 126 |
#define strncpy(d, s, n) __builtin___strncpy_chk (d, s, n, n) |
| 127 |
#define strncat(d, s, n) __builtin___strncat_chk (d, s, n, n) |
| 128 |
|
| 129 |
void test_strfuncs_chk (char *s, unsigned n) |
| 130 |
{ |
| 131 |
if (n == 0) |
| 132 |
return; |
| 133 |
|
| 134 |
T (strcat (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 135 |
T (strcat (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 136 |
|
| 137 |
T (strchr (null (), 'x')); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 138 |
|
| 139 |
T (stpcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 140 |
T (stpcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 141 |
|
| 142 |
T (stpncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 143 |
T (stpncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 144 |
|
| 145 |
T (strcpy (null (), s)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 146 |
T (strcpy (s, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 147 |
|
| 148 |
T (strncpy (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 149 |
T (strncpy (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 150 |
|
| 151 |
T (strncat (s, null (), n)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 152 |
T (strncat (null (), s, n)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
#define fprintf __builtin_fprintf |
| 157 |
#define fprintf_unlocked __builtin_fprintf_unlocked |
| 158 |
#define vfprintf __builtin_vfprintf |
| 159 |
#define printf __builtin_printf |
| 160 |
#define printf_unlocked __builtin_printf_unlocked |
| 161 |
#define vprintf __builtin_vprintf |
| 162 |
#define sprintf __builtin_sprintf |
| 163 |
#define snprintf __builtin_snprintf |
| 164 |
#define vsprintf __builtin_vsprintf |
| 165 |
#define vsnprintf __builtin_vsnprintf |
| 166 |
|
| 167 |
void test_stdio_funcs (FILE *f, char *d, unsigned n, va_list va) |
| 168 |
{ |
| 169 |
T (fprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 170 |
T (fprintf (f, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 171 |
|
| 172 |
T (fprintf_unlocked (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 173 |
T (fprintf_unlocked (f, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 174 |
|
| 175 |
T (vfprintf (null (), "%i", va));/* { dg-warning "argument 1 null where non-null expected" } */ |
| 176 |
T (vfprintf (f, null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 177 |
|
| 178 |
T (vprintf (null (), va)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 179 |
|
| 180 |
T (printf (null ())); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 181 |
T (printf_unlocked (null ())); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 182 |
|
| 183 |
T (vprintf (null (), va)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 184 |
|
| 185 |
T (sprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 186 |
T (sprintf (d, null ())); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 187 |
|
| 188 |
T (snprintf (null (), n, "%i", 0)); |
| 189 |
T (snprintf (d, n, null ())); /* { dg-warning "argument 3 null where non-null expected" } */ |
| 190 |
|
| 191 |
T (vsprintf (null (), "%i", va)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 192 |
T (vsprintf (d, null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 193 |
|
| 194 |
T (vsnprintf (null (), n, "%i", va)); |
| 195 |
T (vsnprintf (d, n, null (), va)); /* { dg-warning "argument 3 null where non-null expected" } */ |
| 196 |
} |
| 197 |
|
| 198 |
#undef fprintf |
| 199 |
#undef fprintf_unlocked |
| 200 |
#undef vfprintf |
| 201 |
#undef printf |
| 202 |
#undef printf_unlocked |
| 203 |
#undef vprintf |
| 204 |
#undef sprintf |
| 205 |
#undef snprintf |
| 206 |
#undef vsprintf |
| 207 |
#undef vsnprintf |
| 208 |
|
| 209 |
#define fprintf(f, fmt, ...) \ |
| 210 |
__builtin___fprintf_chk (f, 0, fmt, __VA_ARGS__) |
| 211 |
#define vfprintf(f, fmt, va) \ |
| 212 |
__builtin___vfprintf_chk (f, 0, fmt, va) |
| 213 |
#define printf(fmt, ...) \ |
| 214 |
__builtin___printf_chk (0, fmt, __VA_ARGS__) |
| 215 |
#define vprintf(fmt, va) \ |
| 216 |
__builtin___vprintf_chk (0, fmt, va) |
| 217 |
#define sprintf(d, fmt, ... ) \ |
| 218 |
__builtin___sprintf_chk (d, 0, n, fmt, __VA_ARGS__) |
| 219 |
#define snprintf(d, n, fmt, ...) \ |
| 220 |
__builtin___snprintf_chk (d, n, 0, n, fmt, __VA_ARGS__) |
| 221 |
#define vsprintf(d, fmt, va) \ |
| 222 |
__builtin___vsprintf_chk (d, 0, n, fmt, va) |
| 223 |
#define vsnprintf(d, n, fmt, va) \ |
| 224 |
__builtin___vsnprintf_chk (d, n, 0, n, fmt, va) |
| 225 |
|
| 226 |
void test_stdio_funcs_chk (FILE *f, char *d, const char *fmt, |
| 227 |
unsigned n, va_list va) |
| 228 |
{ |
| 229 |
T (fprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 230 |
T (fprintf (f, null (), 0)); /* { dg-warning "argument 3 null where non-null expected" } */ |
| 231 |
|
| 232 |
T (vfprintf (null (), "%i", va));/* { dg-warning "argument 1 null where non-null expected" } */ |
| 233 |
T (vfprintf (f, null (), va)); /* { dg-warning "argument 3 null where non-null expected" } */ |
| 234 |
|
| 235 |
T (vprintf (null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 236 |
|
| 237 |
T (printf (null (), 0)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 238 |
|
| 239 |
T (vprintf (null (), va)); /* { dg-warning "argument 2 null where non-null expected" } */ |
| 240 |
|
| 241 |
T (sprintf (null (), "%i", 0)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 242 |
T (sprintf (d, null (), 0)); /* { dg-warning "argument 4 null where non-null expected" } */ |
| 243 |
|
| 244 |
T (snprintf (null (), n, "%i", 0)); |
| 245 |
T (snprintf (d, n, null (), 0)); /* { dg-warning "argument 5 null where non-null expected" } */ |
| 246 |
|
| 247 |
T (vsprintf (null (), "%i", va)); /* { dg-warning "argument 1 null where non-null expected" } */ |
| 248 |
T (vsprintf (d, null (), va)); /* { dg-warning "argument 4 null where non-null expected" } */ |
| 249 |
|
| 250 |
T (vsnprintf (null (), n, "%i", va)); |
| 251 |
T (vsnprintf (d, n, null (), va)); /* { dg-warning "argument 5 null where non-null expected" } */ |
| 252 |
} |