Skip to content

Commit 1ed3ce5

Browse files
authored
Merge pull request #384 from tri-adam/overflow-fix
fix: correct the range check for descriptor IDs
2 parents fd8a090 + 6f00aba commit 1ed3ce5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pkg/sif/create.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro
9797
}
9898

9999
// We derive the ID from i, so make sure the ID will not overflow.
100-
if i >= math.MaxInt32 {
100+
if int64(i) >= math.MaxUint32 {
101101
return errObjectIDOverflow
102102
}
103103

@@ -233,8 +233,16 @@ func OptCreateWithCloseOnUnload(b bool) CreateOpt {
233233
}
234234
}
235235

236+
var errDescriptorCapacityNotSupported = errors.New("descriptor capacity not supported")
237+
236238
// createContainer creates a new SIF container file in rw, according to opts.
237239
func createContainer(rw ReadWriter, co createOpts) (*FileImage, error) {
240+
// The supported number of descriptors is limited by the unsigned 32-bit ID field in each
241+
// rawDescriptor.
242+
if co.descriptorCapacity >= math.MaxUint32 {
243+
return nil, errDescriptorCapacityNotSupported
244+
}
245+
238246
rds := make([]rawDescriptor, co.descriptorCapacity)
239247
rdsSize := int64(binary.Size(rds))
240248

pkg/sif/create_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ func TestCreateContainerAtPath(t *testing.T) {
186186
opts []CreateOpt
187187
wantErr error
188188
}{
189+
{
190+
name: "ErrDescriptorCapacityNotSupported",
191+
opts: []CreateOpt{
192+
OptCreateWithDescriptorCapacity(math.MaxUint32),
193+
},
194+
wantErr: errDescriptorCapacityNotSupported,
195+
},
189196
{
190197
name: "ErrInsufficientCapacity",
191198
opts: []CreateOpt{

0 commit comments

Comments
 (0)