Repository navigation
Fix undefined behavior reported by UBSan - #513
Merged
Merged
Conversation
The entries of the scanned bracket list are compared with uc_bracket_cmp(), which treats both of its arguments as uc_bracket_t pointers. The search key of uc_compiler_literal_follow() was the plain size_t position however, so the comparison read the follow member past the end of the key, which the undefined behaviour sanitizer reports while compiling a destructuring assignment of an array pattern: compiler.c:2639:40: runtime error: member access within address 0x7f8660f0bf40 with insufficient space for an object of type 'const uc_bracket_t' Pass a bracket holding the position as key instead, which is what the comparator expects and what the searched array holds. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
A debug protocol buffer starts out empty, with no data allocated for it yet, and the first read may deliver nothing. Searching an empty buffer for the newline of a complete line passed a null pointer to memchr(), which is declared nonnull, which is undefined even though such a search cannot match anything: runtime error: null pointer passed as argument 1, which is declared to never be null Only look for a line when something has been buffered. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
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.
Fix two undefined behavior (UB) instances that UBSan reports at runtime, each found while executing
the respective code path:
1. debug_proto: skip the line search of an empty buffer (lib/debug_proto.c)
A debug protocol buffer starts out empty with no data allocated, and the first read may deliver nothing. Searching
such an empty buffer for the newline of a complete line passed a null pointer to memchr(), which is declared nonnull
— undefined even though the search cannot match anything:
The fix only performs the line search when something has actually been buffered.
2. compiler: look up scanned brackets with a bracket key (compiler.c)
Entries of the scanned bracket list are compared with uc_bracket_cmp(), which treats both arguments as uc_bracket_t
pointers. The search key of uc_compiler_literal_follow() was, however, a plain size_t position, so the comparator
read the follow member past the end of the key — reported by UBSan while compiling a destructuring assignment of an
array pattern:
The fix passes a uc_bracket_t holding the position as the search key, which is what the comparator expects and what
the searched array holds.