-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix panic in color parsing #290
Conversation
Thanks!! Could you please update the CHANGELOG.md file, and while you're at it also add the other PR we forgot to add there? |
88916a4
to
6edacde
Compare
Thank you for all these fixes! It's really satisfying to close these after having implemented fuzzing. |
Happy to help! This was my first real experience with fuzzing. It'll be a part of my arsenal of tools/techniques from here on out lol |
@@ -27,7 +27,7 @@ impl FromStr for Color { | |||
s | |||
}; | |||
match s.len() { | |||
6 => { | |||
6 if s.is_ascii() => { |
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.
Does this really suffice? The docs for from_str_radix
say "This function panics if radix is not in the range from 2 to 36.". I think we want is_ascii_hexdigit
?
Edit: Erm, of course radix
is always 16, so it won't panic because of that... but I still think we'll want to check for the hexdigits instead?
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 radix in this case is the constant value 16
and from_str_radix
has its own graceful error handling for invalid inputs (first arg). The guard added in this commit protects against invalid string slicing &s[x..y]
. Honestly, it would be nice of there was a set of <int>::from_bstr_radix
functions.
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.
Oh alright, I was misunderstanding why this panicked. Apparently it's about invalid UTF-8?
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.
Yeah, String
s and string slices (str
) must be valid UTF-8 encoded strings. Iterating over the characters (char
; valid UTF-8 encoded character) of a string in Rust is typically done with an s.chars()
iterator.
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.
Alright, thanks for the explanation! So indeed a check on ascii suffices here.
Input:
Note how the background color here is composed of 6 bytes, but the code before this change was trying to slice in the middle of a char boundary.