Skip to content

Commit ed2d4f8

Browse files
committed
fix: corrupted jsonb panic
1 parent fe81859 commit ed2d4f8

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/de.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ impl<'a> Decoder<'a> {
116116
FALSE_TAG => Ok(Value::Bool(false)),
117117
STRING_TAG => {
118118
let offset = jentry.length as usize;
119-
let s = unsafe { std::str::from_utf8_unchecked(&self.buf[..offset]) };
119+
let string = &self.buf.get(..offset).ok_or(Error::InvalidUtf8)?;
120+
let s = unsafe { std::str::from_utf8_unchecked(string) };
120121
self.buf = &self.buf[offset..];
121122
Ok(Value::String(Cow::Borrowed(s)))
122123
}
123124
NUMBER_TAG => {
124125
let offset = jentry.length as usize;
125-
let n = Number::decode(&self.buf[..offset])?;
126+
let number = &self.buf.get(..offset).ok_or(Error::InvalidJsonbNumber)?;
127+
let n = Number::decode(number)?;
126128
self.buf = &self.buf[offset..];
127129
Ok(Value::Number(n))
128130
}

tests/it/decode.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,12 @@ fn test_decode_object() {
176176
}
177177
}
178178
}
179+
180+
#[test]
181+
fn test_decode_corrupted() {
182+
let json = "{\"a\": 1, \"b\": \"123\"}";
183+
let jsonb = jsonb::parse_value(json.as_bytes()).unwrap().to_vec();
184+
let corrupted = jsonb[0..jsonb.len() - 1].to_vec();
185+
let value = from_slice(corrupted.as_slice());
186+
assert!(value.is_err());
187+
}

0 commit comments

Comments
 (0)