@@ -30,6 +30,7 @@ import (
30
30
31
31
"go.uber.org/multierr"
32
32
"github.com/google/osv-scalibr/extractor"
33
+ "github.com/google/osv-scalibr/extractor/internal/units"
33
34
"github.com/google/osv-scalibr/log"
34
35
"github.com/google/osv-scalibr/purl"
35
36
)
@@ -43,7 +44,7 @@ const (
43
44
defaultMaxZipDepth = 16
44
45
// defaultMaxZipBytes in the maximum number of bytes recursively read from an archive file.
45
46
// If this limit is reached, the default extractor is halted and results so far are returned.
46
- defaultMaxZipBytes = 4 << 30 // 4GiB
47
+ defaultMaxZipBytes = 4 * units . GiB
47
48
// defaultMinZipBytes is slightly larger than an empty zip file which is 22 bytes.
48
49
// https://en.wikipedia.org/wiki/ZIP_(file_format)#:~:text=Viewed%20as%20an%20ASCII%20string,file%20are%20usually%20%22PK%22.
49
50
defaultMinZipBytes = 30
@@ -60,7 +61,7 @@ type Config struct {
60
61
MaxZipDepth int
61
62
// MaxOpenedBytes is the maximum number of bytes recursively read from an archive file.
62
63
// If this limit is reached, extraction is halted and results so far are returned.
63
- MaxOpenedBytes int
64
+ MaxOpenedBytes int64
64
65
// MinZipBytes is use to ignore empty zip files during extraction.
65
66
// Zip files smaller than minZipBytes are ignored.
66
67
MinZipBytes int
@@ -73,7 +74,7 @@ type Config struct {
73
74
// Extractor extracts Java packages from archive files.
74
75
type Extractor struct {
75
76
maxZipDepth int
76
- maxOpenedBytes int
77
+ maxOpenedBytes int64
77
78
minZipBytes int
78
79
extractFromFilename bool
79
80
hashJars bool
@@ -128,12 +129,12 @@ func (e Extractor) Extract(ctx context.Context, input *extractor.ScanInput) ([]*
128
129
//
129
130
// It returns early with an error if max depth or max opened bytes is reached.
130
131
// Extracted packages are returned even if an error has occurred.
131
- func (e Extractor ) extractWithMax (ctx context.Context , input * extractor.ScanInput , depth , openedBytes int ) ([]* extractor.Inventory , error ) {
132
+ func (e Extractor ) extractWithMax (ctx context.Context , input * extractor.ScanInput , depth int , openedBytes int64 ) ([]* extractor.Inventory , error ) {
132
133
// Return early if any max/min thresholds are hit.
133
134
if depth > e .maxZipDepth {
134
135
return nil , fmt .Errorf ("%s reached max zip depth %d at %q" , e .Name (), depth , input .Path )
135
136
}
136
- if oBytes := openedBytes + int ( input .Info .Size () ); oBytes > e .maxOpenedBytes {
137
+ if oBytes := openedBytes + input .Info .Size (); oBytes > e .maxOpenedBytes {
137
138
return nil , fmt .Errorf ("%s reached max opened bytes of %d at %q" , e .Name (), oBytes , input .Path )
138
139
}
139
140
if int (input .Info .Size ()) < e .minZipBytes {
@@ -151,7 +152,7 @@ func (e Extractor) extractWithMax(ctx context.Context, input *extractor.ScanInpu
151
152
if err != nil {
152
153
return nil , fmt .Errorf ("%s failed to read file at %q: %w" , e .Name (), input .Path , err )
153
154
}
154
- openedBytes += len (b )
155
+ openedBytes += int64 ( len (b ) )
155
156
// Check size again in case input.Info.Size() was not accurate. Return early if hit max.
156
157
if openedBytes > e .maxOpenedBytes {
157
158
return nil , fmt .Errorf ("%s reached max opened bytes of %d at %q" , e .Name (), openedBytes , input .Path )
0 commit comments