File tree 2 files changed +21
-2
lines changed
2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ const (
36
36
// decompressed, err := zstd.Decompress(dst, src)
37
37
decompressSizeBufferLimit = 1000 * 1000
38
38
39
- zstdFrameHeaderSizeMax = 18 // From zstd.h. Since it's experimental API, hardcoding it
39
+ zstdFrameHeaderSizeMin = 2 // From zstd.h. Since it's experimental API, hardcoding it
40
40
)
41
41
42
42
// CompressBound returns the worst case size needed for a destination buffer,
@@ -67,7 +67,7 @@ func decompressSizeHint(src []byte) int {
67
67
}
68
68
69
69
hint := upperBound
70
- if len (src ) >= zstdFrameHeaderSizeMax {
70
+ if len (src ) >= zstdFrameHeaderSizeMin {
71
71
hint = int (C .ZSTD_getFrameContentSize (unsafe .Pointer (& src [0 ]), C .size_t (len (src ))))
72
72
if hint < 0 { // On error, just use upperBound
73
73
hint = upperBound
Original file line number Diff line number Diff line change @@ -293,6 +293,25 @@ func TestBadPayloadZipBomb(t *testing.T) {
293
293
}
294
294
}
295
295
296
+ func TestSmallPayload (t * testing.T ) {
297
+ // Test that we can compress really small payloads and this doesn't generate a huge output buffer
298
+ compressed , err := Compress (nil , []byte ("a" ))
299
+ if err != nil {
300
+ t .Fatalf ("failed to compress: %s" , err )
301
+ }
302
+
303
+ preAllocated := make ([]byte , 1 , 64 ) // Don't use more than that
304
+ decompressed , err := Decompress (preAllocated , compressed )
305
+ if err != nil {
306
+ t .Fatalf ("failed to compress: %s" , err )
307
+ }
308
+
309
+ if & (preAllocated [0 ]) != & (decompressed [0 ]) { // They should point to the same spot (no realloc)
310
+ t .Fatal ("Compression buffer was changed" )
311
+ }
312
+
313
+ }
314
+
296
315
func BenchmarkCompression (b * testing.B ) {
297
316
if raw == nil {
298
317
b .Fatal (ErrNoPayloadEnv )
You can’t perform that action at this time.
0 commit comments