You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The macro ADD_ROOT works only if the first parameter of the functions is named root.
This is especially not good, because the root parameter shadows the root global variable !
extern void *root; // Global variable
void some_function(void *root) { // Parameter shadows global
// Creates local array and links it
ADD_ROOT(2);
// ... uses local 'root' parameter
}
The thing is, if we rename the global variable into say gc_root, or the parameter root into say *input, the code may still compile (due to shadowing) and even work for some time and start to crash in random ways.
And yet, the compiler will NOT warn about the shadowing with -Wall (you need -Wshadow).
And it will NOT warn about unused parameter input either.
And the current code works not despite the shadowing but BECAUSE of the shadowing !
This has caused memory corruption and many hours of debugging.
The macro ADD_ROOT works only if the first parameter of the functions is named root.
This is especially not good, because the root parameter shadows the root global variable !
The thing is, if we rename the global variable into say gc_root, or the parameter root into say *input, the code may still compile (due to shadowing) and even work for some time and start to crash in random ways.
And yet, the compiler will NOT warn about the shadowing with -Wall (you need -Wshadow).
And it will NOT warn about unused parameter input either.
And the current code works not despite the shadowing but BECAUSE of the shadowing !
This has caused memory corruption and many hours of debugging.
Here is a fix:
Now we have to add root to every DEFINE, but at least there is no bad surprise.
static void add_variable(void *root, Obj **env, Obj **sym, Obj **val) {
DEFINE2(root, vars, tmp);
...
}
The dependency of DEFINE2 on root is explicit. Now we can rename the global variable gc_root without issue.
The text was updated successfully, but these errors were encountered: