[2.0.x] Fix crash upon repeated calls to process_subcommands_now_P#10509
Merged
thinkyhead merged 2 commits intoMarlinFirmware:bugfix-2.0.xfrom Apr 24, 2018
Merged
Conversation
- The previous implementation would call parser.parse() on a buffer that was allocated on the stack. Parser.parse would in turn store this pointer in parser.command_ptr, but this pointer would become invalid upon the return of the first call to process_subcommands_now_P() - A subsequent call to process_subcommands_now_P() would thus call strlen() on parser.command_ptr, now invalid, and thus crash Marlin. This fix stores the value parser.command_ptr itself and restores it upon exit, rather than replacing it to a pointer to automatic data.
4c75e7f to
0775ac9
Compare
Member
|
Hmm, yes, of course! This should work well. On invocation the command pointer points to a slot in the command queue which won't get overwritten until later on. (The queue slot is blocked from being re-used until the command exits.) If sub-commands call this function recursively then their strings will be on the stack. So, that's good. The stack will be smaller and this will run faster. |
Contributor
Author
|
@thinkyhead : Yes, this was a counter-intuitive fix. Generally copying the data is a safe thing to do, but right here, the exact opposite was necessary. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The first call to
process_subcommands_now_P()would callparser.parse()on a buffer that was allocated on the stack. The parser would in turn store this pointer inparser.command_ptr, but this pointer would immediately become invalid when this call toprocess_subcommands_now_P()exited. A subsequent call toprocess_subcommands_now_P()would thus callstrlen()onparser.command_ptr, now invalid, and thus crash Marlin.This fix stores the value
parser.command_ptritself and restores it by value upon exit. This ensures thatparser.command_ptrwill not be left pointing to an invalid buffer.This PR fixes a bug that was introduced in #10450, due to an attempt to make
process_subcommands_now_P()safer (it did, but only once!)