Skip to content

Commit

Permalink
Proper handling of some from_utf8 failures.
Browse files Browse the repository at this point in the history
Improve handling of some cases there a str::from_utf8 call might fail.
Thanks to @killercup and rust-fuzz/targets#119 for pointing out the
problem.
  • Loading branch information
kaj committed May 4, 2018
1 parent 4bb4b3c commit ea4e823
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/parser/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ named!(pub sass_string_dq<SassString>,
simple_qstring_part |
string_part_interpolation |
map!(hash_no_interpolation,
|s| StringPart::Raw(from_utf8(s)
.unwrap()
.to_string())) |
|s| StringPart::Raw(s.to_string())) |
value!(StringPart::Raw("\"".to_string()),
tag!("\\\"")) |
value!(StringPart::Raw("'".to_string()),
Expand All @@ -42,9 +40,7 @@ named!(pub sass_string_sq<SassString>,
simple_qstring_part |
string_part_interpolation |
map!(hash_no_interpolation,
|s| StringPart::Raw(from_utf8(s)
.unwrap()
.to_string())) |
|s| StringPart::Raw(s.to_string())) |
value!(StringPart::Raw("'".to_string()),
tag!("\\'")) |
value!(StringPart::Raw("\"".to_string()),
Expand Down Expand Up @@ -77,22 +73,26 @@ named!(
| hash_no_interpolation
),
String::new(),
|mut acc: String, item: &[u8]| {
acc.push_str(from_utf8(item).unwrap());
|mut acc: String, item: &str| {
acc.push_str(item);
acc
}
)
);
named!(
selector_plain_part<&[u8]>,
is_not!("\n\t >$\"'\\#+*/()[]{}:;,=!&@")
selector_plain_part<&str>,
map_res!(is_not!("\n\t >$\"'\\#+*/()[]{}:;,=!&@"),
|s| from_utf8(s))
);
named!(
selector_escaped_part<&[u8]>,
recognize!(preceded!(
tag!("\\"),
alt!(value!((), many_m_n!(1, 3, hexpair)) | value!((), take!(1)))
))
selector_escaped_part<&str>,
map_res!(
recognize!(preceded!(
tag!("\\"),
alt!(value!((), many_m_n!(1, 3, hexpair)) | value!((), take!(1)))
)),
|s| from_utf8(s)
)
);
named!(
hexpair,
Expand All @@ -102,20 +102,21 @@ named!(
))
);
named!(
hash_no_interpolation<&[u8]>,
terminated!(tag!("#"), peek!(not!(tag!("{"))))
hash_no_interpolation<&str>,
map_res!(terminated!(tag!("#"), peek!(not!(tag!("{")))),
|s| from_utf8(s))
);
named!(
extra_escape<StringPart>,
map!(
map_res!(
preceded!(
tag!("\\"),
alt!(
alphanumeric | tag!(" ") | tag!("'") | tag!("\"") | tag!("\\")
| tag!("#")
)
),
|s| StringPart::Raw(format!("\\{}", from_utf8(s).unwrap()))
|s| StringPart::Raw(format!("\\{}", from_utf8(s)))
)
);

Expand Down

0 comments on commit ea4e823

Please sign in to comment.