Skip to content

Commit d274c9f

Browse files
committed
Fix underflow in recursion limit code
The recursion limiter didn't prevent underflow. Fixed properly and also switched to `Cell` because atomic support seems fairly variable.
1 parent 9a753ec commit d274c9f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/parser.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use core::fmt;
2424
#[cfg(feature = "std")]
2525
use std::rc::Rc;
2626
#[cfg(feature = "std")]
27-
use std::sync::atomic::{AtomicUsize, Ordering};
27+
use std::cell::Cell;
2828

2929
use log::debug;
3030
#[cfg(feature = "std")]
@@ -62,11 +62,12 @@ macro_rules! return_ok_if_some {
6262
macro_rules! check_recursion_depth {
6363
($this:ident) => {
6464
let remaining_depth = $this.remaining_depth.clone();
65-
if remaining_depth.fetch_sub(1, Ordering::SeqCst) <= 0 {
65+
remaining_depth.set(remaining_depth.get().saturating_sub(1));
66+
if remaining_depth.get() == 0 {
6667
return Err(ParserError::RecursionLimitExceeded);
6768
}
6869
defer! {
69-
remaining_depth.fetch_add(1, Ordering::SeqCst);
70+
remaining_depth.set(remaining_depth.get() + 1);
7071
}
7172
};
7273
}
@@ -136,7 +137,7 @@ pub struct Parser<'a> {
136137
index: usize,
137138
dialect: &'a dyn Dialect,
138139
#[cfg(feature = "std")]
139-
remaining_depth: Rc<AtomicUsize>,
140+
remaining_depth: Rc<Cell<usize>>,
140141
}
141142

142143
impl<'a> Parser<'a> {
@@ -147,7 +148,7 @@ impl<'a> Parser<'a> {
147148
index: 0,
148149
dialect,
149150
#[cfg(feature = "std")]
150-
remaining_depth: Rc::new(AtomicUsize::new(96)),
151+
remaining_depth: Rc::new(Cell::new(96)),
151152
}
152153
}
153154

0 commit comments

Comments
 (0)