File tree Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Expand file tree Collapse file tree 2 files changed +13
-2
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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+ }
You can’t perform that action at this time.
0 commit comments