Tor sure does shadow a lot of local and global variables with its block-level variable declarations.
I spot-checked a few of these and they look harmless.
Still, it would be great if we removed them by renaming the variables in the innermost scope. This would avoid confusion, and remove the possibility of bugs where the declaration is removed, and the identifier references the outer scope.
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Child items ...
Show closed items
Linked items 0
Link issues together to show that they're related.
Learn more.
Working on patches. A quick look at the result of enabling -Wshadow showed tons of warnings so it's going to take some time to fix every instance without breaking other things.
Trac: Owner: N/Ato cypherpunks Status: new to assigned
I have noticed several distinct cases of variable shadowing. Each case has its own solution. To make reviewing easier i was thinking of splitting these cases into separate commits.
The cases i have found so far are
redeclaring variables for the same purpose (solution: use the existing variables)
declaring index variables outside for loops (this usually conflicts with subsequent smartlist iterations that use the same index variable names) (solution: declare index variables inside for loops)
using local variable names that match static global variable names (solution: append _local to the local variable names unless renaming them to something more descriptive is appropriate)
I'm unsure how to generate a script that can detect variable shadowing and fix the issues according to the proposed solutions so I'm doing it manually for now.
Please be aware that we might take a long time to review, or reject, a patch that takes a lot of time to manually verify. It's too easy to introduce bugs this way.
Why not try tools like coccinelle, or a scripting language?
Most compilers will give you line numbers of shadowing and shadowed variables.
One strategy could be to temporarily change outer and inner names, then check using a compiler.
There is a risk that inner variables will accidentally be changed to the outer variable name, but that's the only risk I can see.
One strategy could be to temporarily change outer and inner names, then check using a compiler.
There is a risk that inner variables will accidentally be changed to the outer variable name, but that's the only risk I can see.
Here is a detailed strategy I thought of, it might not work:
Change outer variable names by appending _o:
run compiler with -Wshadow and parse the log to create a patch that renames all shadowed declarations to _o, handling SMARTLIST_FOREACH as a special case
run compiler and rename all undeclared identifiers with _o
Change inner variable names by appending _j:
run compiler with -Wshadow and rename all shadowed declarations with _j
run compiler and rename all undeclared identifiers with _j
Revert _o patch
Check for and fix whitespace manually
Remove unnecessary redeclarations manually, if you want to
1135405 Fix a variable-shadowing bug in check_private_dir
Assigns to pw_uid, but doesn't use the value after that.
(Some compilers will warn about this.)
I think pw_uid might need to be used in the log message, or the line pw_uid = tor_getpwuid(st.st_uid); should be deleted.
const struct passwd *pw_uid = NULL; char *process_ownername = NULL; pw_uid = tor_getpwuid(running_uid); process_ownername = pw_uid ? tor_strdup(pw_uid->pw_name) : tor_strdup("<unknown>"); pw_uid = tor_getpwuid(st.st_uid); log_warn(LD_FS, "%s is not owned by this user (%s, %d) but by " "%s (%d). Perhaps you are running Tor as the wrong user?", dirname, process_ownername, (int)running_uid, pw ? pw->pw_name : "<unknown>", (int)st.st_uid);