Skip to content

Commit 41ad493

Browse files
committed
fix(parser): Don't panic on bad hex characters
1 parent d4e6437 commit 41ad493

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

crates/toml_parser/src/decoder/scalar.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ fn decode_float_or_integer<'i>(
561561

562562
if 0 < part_start {
563563
let first = part.as_bytes().first().copied().unwrap_or(b'0');
564-
if !is_any_digit(first, kind) {
564+
if first == b'_' {
565565
let start = part_start - 1;
566566
let end = part_start;
567567
debug_assert_eq!(&raw.as_str()[start..end], underscore);
@@ -574,7 +574,7 @@ fn decode_float_or_integer<'i>(
574574
}
575575
if 1 < part.len() && part_end < raw.len() {
576576
let last = part.as_bytes().last().copied().unwrap_or(b'0');
577-
if !is_any_digit(last, kind) {
577+
if last == b'_' {
578578
let start = part_end;
579579
let end = start + underscore.len();
580580
debug_assert_eq!(&raw.as_str()[start..end], underscore);
@@ -615,22 +615,6 @@ fn decode_float_or_integer<'i>(
615615
kind
616616
}
617617

618-
fn is_any_digit(b: u8, kind: ScalarKind) -> bool {
619-
if kind == ScalarKind::Float {
620-
is_dec_integer_digit(b)
621-
} else {
622-
is_any_integer_digit(b)
623-
}
624-
}
625-
626-
fn is_any_integer_digit(b: u8) -> bool {
627-
(b'0'..=b'9', b'a'..=b'f', b'A'..=b'F').contains_token(b)
628-
}
629-
630-
fn is_dec_integer_digit(b: u8) -> bool {
631-
(b'0'..=b'9').contains_token(b)
632-
}
633-
634618
fn has_underscore(raw: &str) -> bool {
635619
raw.as_bytes().find_slice(b'_').is_some()
636620
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
EventResults {
2+
input: "\nv=0xz_\n",
3+
events: [
4+
Event {
5+
kind: Newline,
6+
encoding: None,
7+
span: 0..1,
8+
},
9+
Event {
10+
kind: SimpleKey,
11+
encoding: None,
12+
span: 1..2,
13+
},
14+
Event {
15+
kind: KeyValSep,
16+
encoding: None,
17+
span: 2..3,
18+
},
19+
Event {
20+
kind: Scalar,
21+
encoding: None,
22+
span: 3..7,
23+
},
24+
Event {
25+
kind: Newline,
26+
encoding: None,
27+
span: 7..8,
28+
},
29+
],
30+
errors: [
31+
ParseError {
32+
context: Some(
33+
3..7,
34+
),
35+
description: "invalid hexadecimal number",
36+
expected: None,
37+
unexpected: Some(
38+
5..5,
39+
),
40+
},
41+
ParseError {
42+
context: Some(
43+
3..7,
44+
),
45+
description: "`_` may only go between digits",
46+
expected: Some(
47+
[],
48+
),
49+
unexpected: Some(
50+
6..7,
51+
),
52+
},
53+
ParseError {
54+
context: Some(
55+
0..4,
56+
),
57+
description: "failed to parse i64",
58+
expected: None,
59+
unexpected: Some(
60+
0..2,
61+
),
62+
},
63+
],
64+
}

crates/toml_parser/tests/testsuite/parse_document.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ v=0x _
494494
}
495495

496496
#[test]
497-
#[should_panic]
498497
fn hex_with_bad_chars() {
499498
t(
500499
"

0 commit comments

Comments
 (0)