@@ -17,6 +17,10 @@ import (
1717 "github.com/chainguard-dev/malcontent/pkg/programkind"
1818)
1919
20+ // maxFuzzSize is the maximum input size for fuzz tests to stay well under
21+ // Go's 100MB fuzzer shared memory capacity and avoid OOM in parsers.
22+ const maxFuzzSize = 10 * 1024 * 1024
23+
2024// readTestFile reads a file using file.GetContents for consistency with production code.
2125func readTestFile (path string ) ([]byte , error ) {
2226 f , err := os .Open (path )
@@ -52,6 +56,9 @@ func FuzzExtractTar(f *testing.F) {
5256 f .Add ([]byte {0x1f , 0x8b , 0x08 , 0x00 }) // gzip magic bytes only
5357
5458 f .Fuzz (func (t * testing.T , data []byte ) {
59+ if len (data ) > maxFuzzSize {
60+ return
61+ }
5562 tmpFile , err := os .CreateTemp ("" , "fuzz-tar-*.tar.gz" )
5663 if err != nil {
5764 t .Skip ("failed to create temp file" )
@@ -107,6 +114,9 @@ func FuzzExtractZip(f *testing.F) {
107114 f .Add ([]byte {0x50 , 0x4b , 0x03 , 0x04 }) // full zip signature
108115
109116 f .Fuzz (func (t * testing.T , data []byte ) {
117+ if len (data ) > maxFuzzSize {
118+ return
119+ }
110120 tmpFile , err := os .CreateTemp ("" , "fuzz-zip-*.zip" )
111121 if err != nil {
112122 t .Skip ("failed to create temp file" )
@@ -196,6 +206,9 @@ func FuzzExtractArchive(f *testing.F) {
196206 f .Add ([]byte {0x1f , 0x8b , 0x08 , 0x00 }, ".gz" ) // gzip header
197207
198208 f .Fuzz (func (t * testing.T , data []byte , ext string ) {
209+ if len (data ) > maxFuzzSize {
210+ return
211+ }
199212 if _ , ok := programkind .ArchiveMap [ext ]; ! ok {
200213 return
201214 }
@@ -294,6 +307,9 @@ func FuzzExtractGzip(f *testing.F) {
294307 f .Add (make ([]byte , 1024 * 1024 )) // large zeros (compression bomb test)
295308
296309 f .Fuzz (func (t * testing.T , data []byte ) {
310+ if len (data ) > maxFuzzSize {
311+ return
312+ }
297313 tmpFile , err := os .CreateTemp ("" , "fuzz-gz-*.gz" )
298314 if err != nil {
299315 t .Skip ()
@@ -338,6 +354,9 @@ func FuzzExtractBz2(f *testing.F) {
338354 f .Add (make ([]byte , 1024 * 1024 )) // large zeros
339355
340356 f .Fuzz (func (t * testing.T , data []byte ) {
357+ if len (data ) > maxFuzzSize {
358+ return
359+ }
341360 tmpFile , err := os .CreateTemp ("" , "fuzz-bz2-*.bz2" )
342361 if err != nil {
343362 t .Skip ()
@@ -390,6 +409,9 @@ func FuzzExtractZstd(f *testing.F) {
390409 f .Add (make ([]byte , 1024 * 1024 )) // large zeros
391410
392411 f .Fuzz (func (t * testing.T , data []byte ) {
412+ if len (data ) > maxFuzzSize {
413+ return
414+ }
393415 tmpFile , err := os .CreateTemp ("" , "fuzz-zst-*.zst" )
394416 if err != nil {
395417 t .Skip ()
@@ -444,6 +466,9 @@ func FuzzExtractZlib(f *testing.F) {
444466 f .Add (make ([]byte , 1024 * 1024 )) // large zeros
445467
446468 f .Fuzz (func (t * testing.T , data []byte ) {
469+ if len (data ) > maxFuzzSize {
470+ return
471+ }
447472 tmpFile , err := os .CreateTemp ("" , "fuzz-zlib-*.zlib" )
448473 if err != nil {
449474 t .Skip ()
@@ -497,7 +522,7 @@ func FuzzExtractRPM(f *testing.F) {
497522 f .Add ([]byte ("not rpm" )) // invalid
498523
499524 f .Fuzz (func (t * testing.T , data []byte ) {
500- if len (data ) < 96 || ! bytes .Equal (data [:4 ], rpmMagic ) {
525+ if len (data ) < 96 || len ( data ) > maxFuzzSize || ! bytes .Equal (data [:4 ], rpmMagic ) {
501526 return
502527 }
503528
@@ -552,6 +577,9 @@ func FuzzExtractDeb(f *testing.F) {
552577 f .Add ([]byte ("not deb" )) // invalid
553578
554579 f .Fuzz (func (t * testing.T , data []byte ) {
580+ if len (data ) > maxFuzzSize {
581+ return
582+ }
555583 tmpFile , err := os .CreateTemp ("" , "fuzz-deb-*.deb" )
556584 if err != nil {
557585 t .Skip ()
@@ -593,6 +621,9 @@ func FuzzExtractUPX(f *testing.F) {
593621 f .Add ([]byte ("not upx" )) // invalid
594622
595623 f .Fuzz (func (t * testing.T , data []byte ) {
624+ if len (data ) > maxFuzzSize {
625+ return
626+ }
596627 tmpFile , err := os .CreateTemp ("" , "fuzz-upx-*" )
597628 if err != nil {
598629 t .Skip ()
0 commit comments