Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think, did tests/fuzz/fuzz-handle_onion_message.c (also using jmp_buf fuzz_env and setjmp(fuzz_env) at line 78) has the same ordering issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of
setjmp/longjmpintests/fuzz/fuzz-handle_onion_message.cshouldn't cause a crash since the only local variable (daemon) accessed after thelongjmpis initialized (toNULL) before thesetjmp. However, the call total_free(daemon->master)may or may not happen as intended, depending on whether the compiler emits instructions to reload the register holding the value ofdaemonafter thelongjmp. If the register is reloaded (from the stack), then it may hold the non-null address to whichdaemonwas set from the return value ofnew_daemon()(depending on whether the compiler emitted instructions after the call tonew_daemon()to flush the new value ofdaemonback onto the stack), andtal_free(daemon->master)will be called if this has occurred. On the other hand, if the register is not reloaded, then it will still hold the valueNULL(the value to whichdaemonwas initialized before thesetjmpcall), andtal_free(daemon->master)will not be called. I would argue that it's not a good idea to have control flow vary depending upon compiler optimizations. You can prevent the compiler from caching thedaemonlocal variable in a register by declaring it (i.e., the pointer itself, not the pointed-to object)volatile, but I generally wouldn't recommend that, asvolatileis detrimental to compiler optimizations. A nicer fix would be to insert a second call tosetjmpafterdaemonis set to the return value fromnew_daemon(). That would ensure that the code at thecleanuplabel will always see the latest value ofdaemon, even in the case thatcleanupis reached via alongjmp.