Skip to content

Commit

Permalink
Merge pull request #408 from epage/key
Browse files Browse the repository at this point in the history
fix(parser): Avoid stackoverflow with long dotted keys
  • Loading branch information
epage committed Dec 28, 2022
2 parents 9174af0 + b084184 commit ce27cd7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/toml_edit/src/parser/key.rs
Expand Up @@ -6,6 +6,7 @@ use nom8::combinator::peek;
use nom8::multi::separated_list1;

use crate::key::Key;
use crate::parser::errors::CustomError;
use crate::parser::prelude::*;
use crate::parser::strings::{basic_string, literal_string};
use crate::parser::trivia::{from_utf8_unchecked, ws};
Expand All @@ -24,6 +25,11 @@ pub(crate) fn key(input: Input<'_>) -> IResult<Input<'_>, Vec<Key>, ParserError<
}),
)
.context(Context::Expression("key"))
.map_res(|k| {
// Inserting the key will require recursion down the line
RecursionCheck::check_depth(k.len())?;
Ok::<_, CustomError>(k)
})
.parse(input)
}

Expand Down
12 changes: 12 additions & 0 deletions crates/toml_edit/src/parser/mod.rs
Expand Up @@ -70,6 +70,14 @@ pub(crate) mod prelude {

#[cfg(not(feature = "unbounded"))]
impl RecursionCheck {
pub(crate) fn check_depth(depth: usize) -> Result<(), super::errors::CustomError> {
if depth < 128 {
Ok(())
} else {
Err(super::errors::CustomError::RecursionLimitExceeded)
}
}

pub(crate) fn recursing(
mut self,
input: Input<'_>,
Expand All @@ -95,6 +103,10 @@ pub(crate) mod prelude {

#[cfg(feature = "unbounded")]
impl RecursionCheck {
pub(crate) fn check_depth(_depth: usize) -> Result<(), super::errors::CustomError> {
Ok(())
}

pub(crate) fn recursing(
self,
_input: Input<'_>,
Expand Down

0 comments on commit ce27cd7

Please sign in to comment.