Skip to content

Commit 63376e0

Browse files
authored
Merge pull request #774 from epage/invalid
fix(parser): Resolve stackoverflow on lots of blank lines
2 parents a73c512 + 57f7bb8 commit 63376e0

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/toml_edit/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unbounded = []
4141

4242
[dependencies]
4343
indexmap = { version = "2.0.0", features = ["std"] }
44-
winnow = { version = "0.6.16", optional = true }
44+
winnow = { version = "0.6.17", optional = true }
4545
serde = { version = "1.0.145", optional = true }
4646
kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
4747
toml_datetime = { version = "0.6.8", path = "../toml_datetime" }

crates/toml_edit/src/parser/trivia.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use winnow::combinator::peek;
99
use winnow::combinator::repeat;
1010
use winnow::combinator::terminated;
1111
use winnow::prelude::*;
12+
use winnow::stream::Stream as _;
1213
use winnow::token::any;
1314
use winnow::token::one_of;
1415
use winnow::token::take_while;
@@ -91,15 +92,26 @@ pub(crate) fn ws_newlines(input: &mut Input<'_>) -> PResult<()> {
9192
// note: this rule is not present in the original grammar
9293
// ws-comment-newline = *( ws-newline-nonempty / comment )
9394
pub(crate) fn ws_comment_newline(input: &mut Input<'_>) -> PResult<()> {
94-
let _ = ws.parse_next(input)?;
95+
let mut start = input.checkpoint();
96+
loop {
97+
let _ = ws.parse_next(input)?;
98+
99+
dispatch! {opt(peek(any));
100+
Some(b'#') => (comment, newline).void(),
101+
Some(b'\n') => (newline).void(),
102+
Some(b'\r') => (newline).void(),
103+
_ => empty,
104+
}
105+
.parse_next(input)?;
95106

96-
dispatch! {opt(peek(any));
97-
Some(b'#') => (comment, newline, ws_comment_newline).void(),
98-
Some(b'\n') => (newline, ws_comment_newline).void(),
99-
Some(b'\r') => (newline, ws_comment_newline).void(),
100-
_ => empty,
107+
let end = input.checkpoint();
108+
if start == end {
109+
break;
110+
}
111+
start = end;
101112
}
102-
.parse_next(input)
113+
114+
Ok(())
103115
}
104116

105117
// note: this rule is not present in the original grammar

0 commit comments

Comments
 (0)