Skip to content

Commit

Permalink
Merge pull request #67 from CookiePieWw/fix_panic
Browse files Browse the repository at this point in the history
fix: panic when facing corrupted jsonb
  • Loading branch information
b41sh authored Nov 13, 2024
2 parents 126b1d4 + 7fa10a3 commit 8c8d2fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ impl<'a> Decoder<'a> {
FALSE_TAG => Ok(Value::Bool(false)),
STRING_TAG => {
let offset = jentry.length as usize;
let s = unsafe { std::str::from_utf8_unchecked(&self.buf[..offset]) };
let string = &self.buf.get(..offset).ok_or(Error::InvalidUtf8)?;
let s = unsafe { std::str::from_utf8_unchecked(string) };
self.buf = &self.buf[offset..];
Ok(Value::String(Cow::Borrowed(s)))
}
NUMBER_TAG => {
let offset = jentry.length as usize;
let n = Number::decode(&self.buf[..offset])?;
let number = &self.buf.get(..offset).ok_or(Error::InvalidJsonbNumber)?;
let n = Number::decode(number)?;
self.buf = &self.buf[offset..];
Ok(Value::Number(n))
}
Expand Down
9 changes: 9 additions & 0 deletions tests/it/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,12 @@ fn test_decode_object() {
}
}
}

#[test]
fn test_decode_corrupted() {
let json = "{\"a\": 1, \"b\": \"123\"}";
let jsonb = jsonb::parse_value(json.as_bytes()).unwrap().to_vec();
let corrupted = jsonb[0..jsonb.len() - 1].to_vec();
let value = from_slice(corrupted.as_slice());
assert!(value.is_err());
}

0 comments on commit 8c8d2fc

Please sign in to comment.