Commit b633a92
authored
Do not save/restore a pointer across an in-place realloc. (#1925)
For `p = realloc(p, sz)`, VisitBinaryOperator saved the pre-realloc
pointer -- both the primal p and its adjoint _d_p -- to restore it in
the reverse sweep. realloc frees the old block, so the restore installed
a dangling pointer that the sweep then read and wrote, and the cleanup
free() double-freed it. Valgrind reports the Invalid read/write and
Invalid free in Gradient/Pointers.C once the clang false positives are
suppressed.
A growing or same-size realloc preserves the reused contents, so the
reallocated pointer stays valid for every reverse access. Detect an
in-place realloc -- the LHS is realloc's own pointer argument -- and skip
both the primal and the derivative store/restore, leaving p and _d_p in
place. A shrinking in-place realloc stays unhandled (its reverse sweep
may read the freed tail out of bounds, as before); no test exercises it.1 parent 959f0be commit b633a92
2 files changed
Lines changed: 28 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3146 | 3146 | | |
3147 | 3147 | | |
3148 | 3148 | | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
| 3152 | + | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
| 3161 | + | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | + | |
| 3167 | + | |
| 3168 | + | |
| 3169 | + | |
3149 | 3170 | | |
3150 | 3171 | | |
3151 | | - | |
| 3172 | + | |
3152 | 3173 | | |
3153 | 3174 | | |
3154 | 3175 | | |
| |||
3167 | 3188 | | |
3168 | 3189 | | |
3169 | 3190 | | |
3170 | | - | |
3171 | | - | |
| 3191 | + | |
| 3192 | + | |
3172 | 3193 | | |
3173 | 3194 | | |
3174 | 3195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
| |||
338 | 336 | | |
339 | 337 | | |
340 | 338 | | |
341 | | - | |
342 | | - | |
343 | 339 | | |
344 | 340 | | |
345 | 341 | | |
346 | | - | |
| 342 | + | |
347 | 343 | | |
348 | | - | |
| 344 | + | |
349 | 345 | | |
350 | 346 | | |
351 | 347 | | |
352 | | - | |
| 348 | + | |
353 | 349 | | |
354 | 350 | | |
355 | 351 | | |
356 | 352 | | |
357 | | - | |
| 353 | + | |
358 | 354 | | |
359 | 355 | | |
360 | 356 | | |
361 | 357 | | |
362 | 358 | | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | 359 | | |
368 | 360 | | |
369 | 361 | | |
| |||
0 commit comments