Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 4fceb09

Browse files
committed
Merge pull request #67 from oli-obk/fix_66
change panic to failure when parser-stack depleted
2 parents faeac0c + 50fe8ad commit 4fceb09

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/json.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ pub enum DecoderError {
316316
ExpectedError(string::String, string::String),
317317
MissingFieldError(string::String),
318318
UnknownVariantError(string::String),
319-
ApplicationError(string::String)
319+
ApplicationError(string::String),
320+
EOF,
320321
}
321322

322323
#[derive(Copy, Debug)]
@@ -1995,21 +1996,21 @@ impl Decoder {
19951996
}
19961997

19971998
impl Decoder {
1998-
fn pop(&mut self) -> Json {
1999-
self.stack.pop().unwrap()
1999+
fn pop(&mut self) -> DecodeResult<Json> {
2000+
self.stack.pop().ok_or(EOF)
20002001
}
20012002
}
20022003

20032004
macro_rules! expect {
20042005
($e:expr, Null) => ({
2005-
match $e {
2006+
match try!($e) {
20062007
Json::Null => Ok(()),
20072008
other => Err(ExpectedError("Null".to_string(),
20082009
format!("{}", other)))
20092010
}
20102011
});
20112012
($e:expr, $t:ident) => ({
2012-
match $e {
2013+
match try!($e) {
20132014
Json::$t(v) => Ok(v),
20142015
other => {
20152016
Err(ExpectedError(stringify!($t).to_string(),
@@ -2022,7 +2023,7 @@ macro_rules! expect {
20222023
macro_rules! read_primitive {
20232024
($name:ident, $ty:ty) => {
20242025
fn $name(&mut self) -> DecodeResult<$ty> {
2025-
match self.pop() {
2026+
match try!(self.pop()) {
20262027
Json::I64(f) => match num::cast(f) {
20272028
Some(f) => Ok(f),
20282029
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
@@ -2065,7 +2066,7 @@ impl ::Decoder for Decoder {
20652066
fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) }
20662067

20672068
fn read_f64(&mut self) -> DecodeResult<f64> {
2068-
match self.pop() {
2069+
match try!(self.pop()) {
20692070
Json::I64(f) => Ok(f as f64),
20702071
Json::U64(f) => Ok(f as f64),
20712072
Json::F64(f) => Ok(f),
@@ -2113,7 +2114,7 @@ impl ::Decoder for Decoder {
21132114
mut f: F) -> DecodeResult<T>
21142115
where F: FnMut(&mut Decoder, usize) -> DecodeResult<T>,
21152116
{
2116-
let name = match self.pop() {
2117+
let name = match try!(self.pop()) {
21172118
Json::String(s) => s,
21182119
Json::Object(mut o) => {
21192120
let n = match o.remove(&"variant".to_string()) {
@@ -2178,7 +2179,7 @@ impl ::Decoder for Decoder {
21782179
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
21792180
{
21802181
let value = try!(f(self));
2181-
self.pop();
2182+
try!(self.pop());
21822183
Ok(value)
21832184
}
21842185

@@ -2250,7 +2251,7 @@ impl ::Decoder for Decoder {
22502251
fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
22512252
F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
22522253
{
2253-
match self.pop() {
2254+
match try!(self.pop()) {
22542255
Json::Null => f(self, false),
22552256
value => { self.stack.push(value); f(self, true) }
22562257
}
@@ -3847,6 +3848,18 @@ mod tests {
38473848
}
38483849
}
38493850

3851+
#[test]
3852+
fn test_bad_json_stack_depleted() {
3853+
use json;
3854+
#[derive(Debug, RustcDecodable)]
3855+
enum ChatEvent {
3856+
Variant(i32)
3857+
}
3858+
let serialized = "{\"variant\": \"Variant\", \"fields\": []}";
3859+
let r: Result<ChatEvent, _> = json::decode(serialized);
3860+
assert!(r.unwrap_err() == EOF);
3861+
}
3862+
38503863
#[bench]
38513864
fn bench_streaming_small(b: &mut Bencher) {
38523865
b.iter( || {

0 commit comments

Comments
 (0)