@@ -366,3 +366,36 @@ func TestPointersInDecoder(t *testing.T) {
366
366
})
367
367
}
368
368
}
369
+
370
+ // TestBoundsChecking verifies that buffer access is properly bounds-checked
371
+ // to prevent panics on malformed databases.
372
+ func TestBoundsChecking (t * testing.T ) {
373
+ // Create a very small buffer that would cause out-of-bounds access
374
+ // if bounds checking is not working
375
+ smallBuffer := []byte {0x44 , 0x41 } // Type string (0x4), size 4, but only 2 bytes total
376
+ dd := NewDataDecoder (smallBuffer )
377
+ decoder := & Decoder {d : dd , offset : 0 }
378
+
379
+ // This should fail gracefully with an error instead of panicking
380
+ _ , err := decoder .DecodeString ()
381
+ require .Error (t , err )
382
+ require .Contains (t , err .Error (), "exceeds buffer length" )
383
+
384
+ // Test DecodeBytes bounds checking with a separate buffer
385
+ bytesBuffer := []byte {0x84 , 0x41 } // Type bytes (4 << 5 = 0x80), size 4 (0x04), but only 2 bytes total
386
+ dd3 := NewDataDecoder (bytesBuffer )
387
+ decoder3 := & Decoder {d : dd3 , offset : 0 }
388
+
389
+ _ , err = decoder3 .DecodeBytes ()
390
+ require .Error (t , err )
391
+ require .Contains (t , err .Error (), "exceeds buffer length" )
392
+
393
+ // Test DecodeUInt128 bounds checking
394
+ uint128Buffer := []byte {0x0B , 0x03 } // Extended type (0x0), size 11, TypeUint128-7=3, but only 2 bytes total
395
+ dd2 := NewDataDecoder (uint128Buffer )
396
+ decoder2 := & Decoder {d : dd2 , offset : 0 }
397
+
398
+ _ , _ , err = decoder2 .DecodeUInt128 ()
399
+ require .Error (t , err )
400
+ require .Contains (t , err .Error (), "exceeds buffer length" )
401
+ }
0 commit comments