Closed tueda closed 1 year ago
The following patch makes this problem manifest and gives a segfault.
diff --git a/sources/tools.c b/sources/tools.c
index 3279d07..f392291 100644
--- a/sources/tools.c
+++ b/sources/tools.c
@@ -2788,7 +2788,15 @@ VOID *FromList(LIST *L)
i = ( L->num * L->size ) / sizeof(int);
old = (int *)L->lijst; newL = (int *)newlist;
while ( --i >= 0 ) *newL++ = *old++;
- if ( L->lijst ) M_free(L->lijst,"L->lijst FromList");
+ if ( L->lijst ) {
+ // Before freeing the memory block, we mess up its content.
+ // This must not be a problem.
+ char *p = (char *)L->lijst;
+ for (int i = 0; i < L->num * L->size; i++) {
+ *p++ = 'x'; // non-zero
+ }
+ M_free(L->lijst,"L->lijst FromList");
+ }
}
L->lijst = newlist;
}
When PutPreVar
at line 372 or 375 extends the list of the preprocessor variables, the pointer p
to a preprocessor variable in the current list will be invalid.
This is found during the discussion of #433. The following code leads to a Valgrind error with the current master branch:
The point is that the
var
macro has the index 23, which is $12 \times 2^n - 1$ with $n \in \mathbb{Z}_{\ge0}$. The magic number 12 comes from how theFromList
function extends the list (the initial capacity is 12; the comment says 10 but that is wrong).