| 1 |
/* PR ????? - No warning on attempts to access free object |
| 2 |
Verify that attempting to reallocate unallocated objects referenced |
| 3 |
either directly or through pointers is diagnosed. |
| 4 |
{ dg-do compile } |
| 5 |
{ dg-options "-O2 -Wall -Wfree-nonheap-object" } */ |
| 6 |
|
| 7 |
typedef __SIZE_TYPE__ size_t; |
| 8 |
|
| 9 |
extern void free (void*); |
| 10 |
extern void* alloca (size_t); |
| 11 |
extern void* realloc (void*, size_t); |
| 12 |
|
| 13 |
void sink (void*, ...); |
| 14 |
|
| 15 |
extern void* eparr[]; |
| 16 |
extern char *eptr; |
| 17 |
|
| 18 |
extern size_t n; |
| 19 |
|
| 20 |
|
| 21 |
void nowarn_realloc (void *p, size_t n) |
| 22 |
{ |
| 23 |
char *q = realloc (p, n); |
| 24 |
sink (q); |
| 25 |
|
| 26 |
q = realloc (0, n); |
| 27 |
sink (q); |
| 28 |
|
| 29 |
q = realloc (q, n * 2); |
| 30 |
sink (q); |
| 31 |
} |
| 32 |
|
| 33 |
/* Verify that calling realloc on a pointer to an unknown object minus |
| 34 |
some nonzero offset isn't diagnosed, but a pointer plus a positive |
| 35 |
offset is (a positive offset cannot point at the beginning). */ |
| 36 |
|
| 37 |
void test_realloc_offset (char *p1, char *p2, char *p3, size_t n, int i) |
| 38 |
{ |
| 39 |
char *q; |
| 40 |
q = realloc (p1 - 1, n); |
| 41 |
sink (q); |
| 42 |
|
| 43 |
q = realloc (p2 + 1, n); // { dg-warning "'realloc' called on pointer 'p2' with nonzero offset 1" } |
| 44 |
sink (q); |
| 45 |
|
| 46 |
q = realloc (p3 + i, n); |
| 47 |
sink (q); |
| 48 |
} |
| 49 |
|
| 50 |
void warn_realloc_extern_arr (void) |
| 51 |
{ |
| 52 |
extern char ecarr[]; // { gg-message "declared here" } |
| 53 |
char *p = ecarr; |
| 54 |
char *q = realloc (p, n); // { dg-warning "'realloc' called on unallocated object 'ecarr'" } |
| 55 |
sink (q); |
| 56 |
} |
| 57 |
|
| 58 |
void warn_realloc_extern_arr_offset (int i) |
| 59 |
{ |
| 60 |
extern char ecarr[]; |
| 61 |
char *p = ecarr + i; |
| 62 |
char *q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 63 |
sink (q); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
void warn_realloc_string (int i) |
| 68 |
{ |
| 69 |
char *p, *q; |
| 70 |
{ |
| 71 |
p = "123"; |
| 72 |
sink (p); |
| 73 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 74 |
sink (q); |
| 75 |
} |
| 76 |
{ |
| 77 |
p = "234" + 1; |
| 78 |
sink (p); |
| 79 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 80 |
sink (q); |
| 81 |
} |
| 82 |
{ |
| 83 |
p = "123" + i; |
| 84 |
sink (p); |
| 85 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 86 |
sink (q); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
void warn_realloc_alloca (int n, int i) |
| 92 |
{ |
| 93 |
char *p, *q; |
| 94 |
{ |
| 95 |
p = alloca (n); |
| 96 |
sink (p); |
| 97 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 98 |
sink (q); |
| 99 |
} |
| 100 |
{ |
| 101 |
p = (char*)alloca (n + 1); |
| 102 |
sink (p); |
| 103 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 104 |
sink (q); |
| 105 |
} |
| 106 |
{ |
| 107 |
p = (char*)alloca (n + 2) + i; |
| 108 |
sink (p); |
| 109 |
q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 110 |
sink (q); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void warn_realloc_local_arr (int i) |
| 116 |
{ |
| 117 |
char *q; |
| 118 |
{ |
| 119 |
char a[4]; |
| 120 |
sink (a); |
| 121 |
q = realloc (a, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 122 |
sink (q); |
| 123 |
} |
| 124 |
|
| 125 |
{ |
| 126 |
char b[5]; |
| 127 |
sink (b); |
| 128 |
q = realloc (b + 1, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 129 |
sink (q); |
| 130 |
} |
| 131 |
|
| 132 |
{ |
| 133 |
char c[6]; |
| 134 |
sink (c); |
| 135 |
q = realloc (&c[2], n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 136 |
sink (q); |
| 137 |
} |
| 138 |
|
| 139 |
{ |
| 140 |
char d[7]; |
| 141 |
sink (d); |
| 142 |
q = realloc (&d[i], n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 143 |
sink (q); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
void warn_realloc_vla (int n1, int n2, int i) |
| 148 |
{ |
| 149 |
char *q; |
| 150 |
{ |
| 151 |
char vla[n1]; |
| 152 |
sink (vla); |
| 153 |
q = realloc (vla, n2); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 154 |
sink (q); |
| 155 |
} |
| 156 |
|
| 157 |
{ |
| 158 |
char vlb[n1 + 1]; |
| 159 |
sink (vlb); |
| 160 |
q = realloc (vlb + 1, n2);// { dg-warning "\\\[-Wfree-nonheap-object" } |
| 161 |
sink (q); |
| 162 |
} |
| 163 |
|
| 164 |
{ |
| 165 |
char vlc[n1 + 2]; |
| 166 |
sink (vlc); |
| 167 |
q = realloc (&vlc[2], n2);// { dg-warning "\\\[-Wfree-nonheap-object" } |
| 168 |
sink (q); |
| 169 |
} |
| 170 |
|
| 171 |
{ |
| 172 |
char vld[7]; |
| 173 |
sink (vld); |
| 174 |
q = realloc (&vld[i], n2);// { dg-warning "\\\[-Wfree-nonheap-object" } |
| 175 |
sink (q); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void nowarn_realloc_extern_ptrarr (void) |
| 180 |
{ |
| 181 |
char *q = realloc (*eparr, n); |
| 182 |
sink (q); |
| 183 |
} |
| 184 |
|
| 185 |
void nowarn_realloc_extern_ptrarr_offset (int i) |
| 186 |
{ |
| 187 |
char *p = eparr[i]; |
| 188 |
char *q = realloc (p, n); |
| 189 |
sink (q); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
void warn_realloc_extern_ptrarr (void) |
| 194 |
{ |
| 195 |
char *q = realloc (eparr, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 196 |
sink (q); |
| 197 |
} |
| 198 |
|
| 199 |
void warn_realloc_extern_ptrarr_offset (int i) |
| 200 |
{ |
| 201 |
void *p = eparr + i; |
| 202 |
void *q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 203 |
sink (q); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
void nowarn_realloc_extern_ptr (void) |
| 208 |
{ |
| 209 |
char *q = realloc (eptr, n); |
| 210 |
sink (q); |
| 211 |
} |
| 212 |
|
| 213 |
void nowarn_realloc_extern_ptr_offset (int i) |
| 214 |
{ |
| 215 |
char *p = eptr + i; |
| 216 |
char *q = realloc (p, n); |
| 217 |
sink (q); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
void warn_realloc_extern_ptr_pos_offset (int i) |
| 222 |
{ |
| 223 |
if (i <= 0) |
| 224 |
i = 1; |
| 225 |
|
| 226 |
char *p = eptr + i; |
| 227 |
char *q = realloc (p, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 228 |
sink (q); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
void nowarn_realloc_parm_offset (char *p, int i) |
| 233 |
{ |
| 234 |
char *q = p + i; |
| 235 |
q = realloc (q, n); |
| 236 |
sink (q); |
| 237 |
} |
| 238 |
|
| 239 |
void nowarn_realloc_parm_neg_offset (char *p, int i) |
| 240 |
{ |
| 241 |
if (i >= 0) |
| 242 |
i = -1; |
| 243 |
|
| 244 |
char *q = p + i; |
| 245 |
q = realloc (q, n); |
| 246 |
sink (q); |
| 247 |
} |
| 248 |
|
| 249 |
void warn_realloc_parm_pos_offset (char *p, int i) |
| 250 |
{ |
| 251 |
if (i <= 0) |
| 252 |
i = 1; |
| 253 |
|
| 254 |
char *q = p + i; |
| 255 |
q = realloc (q, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 256 |
sink (q); |
| 257 |
} |
| 258 |
|
| 259 |
void nowarn_realloc_deref_parm_pos_offset (void **p, int i) |
| 260 |
{ |
| 261 |
if (i <= 0) |
| 262 |
i = 1; |
| 263 |
|
| 264 |
// The offset is from p, not *p. |
| 265 |
void *q = *(p + i); |
| 266 |
q = realloc (q, n); |
| 267 |
sink (q); |
| 268 |
} |
| 269 |
|
| 270 |
void warn_realloc_deref_parm_pos_offset (void **p, int i) |
| 271 |
{ |
| 272 |
if (i <= 0) |
| 273 |
i = 1; |
| 274 |
|
| 275 |
// Unlike in the function above the offset is from *p. |
| 276 |
void *q = *p + i; |
| 277 |
q = realloc (q, n); // { dg-warning "\\\[-Wfree-nonheap-object" } |
| 278 |
sink (q); |
| 279 |
} |