@@ -301,7 +301,7 @@ func BatchDirDecode(buf []byte, itemCount uint32, packedAreaLen uint32) ([]Batch
301301// Checks alignment and that each entry falls within packedAreaLen.
302302func BatchDirValidate (buf []byte , itemCount uint32 , packedAreaLen uint32 ) error {
303303 dirSize64 := uint64 (itemCount ) * 8
304- if dirSize64 > uint64 ( maxIntValue () ) {
304+ if dirSize64 > maxIntUint64 ( ) {
305305 return ErrBadItemCount
306306 }
307307 dirSize := int (dirSize64 ) // #nosec G115 -- bounded by maxIntValue above.
@@ -331,7 +331,7 @@ func BatchItemGet(payload []byte, itemCount uint32, index uint32) ([]byte, error
331331 }
332332
333333 dirSize64 := uint64 (itemCount ) * 8
334- if dirSize64 > uint64 ( maxIntValue () ) {
334+ if dirSize64 > maxIntUint64 ( ) {
335335 return nil , ErrBadItemCount
336336 }
337337 dirSize := int (dirSize64 ) // #nosec G115 -- bounded by maxIntValue above.
@@ -348,7 +348,7 @@ func BatchItemGet(payload []byte, itemCount uint32, index uint32) ([]byte, error
348348 if off % uint32 (Alignment ) != 0 {
349349 return nil , ErrBadAlignment
350350 }
351- if uint64 (off ) + uint64 ( length ) > uint64 ( packedAreaLen ) {
351+ if ! uint32RangeWithinInt (off , length , packedAreaLen ) {
352352 return nil , ErrOutOfBounds
353353 }
354354
@@ -404,30 +404,31 @@ func NewBatchBuilder(buf []byte, maxItems uint32) *BatchBuilder {
404404// Add appends an item payload. Handles alignment padding.
405405func (b * BatchBuilder ) Add (item []byte ) error {
406406 maxInt := maxIntValue ()
407+ itemLen32 , itemLenFitsU32 := checkedU32Int (len (item ))
407408 // Inline the common case; addSlow preserves the precise error returns for
408409 // overflow and unusual bounds.
409410 if b .itemCount < b .maxItems &&
410411 b .dirEnd >= 0 &&
411412 b .dataOffset >= 0 &&
412413 b .dataOffset <= maxInt - 7 &&
413- uint64 ( len ( item )) <= uint64 ( ^ uint32 ( 0 )) {
414+ itemLenFitsU32 {
414415 alignedOff := Align8 (b .dataOffset )
415- if uint64 (alignedOff ) <= uint64 (^ uint32 (0 )) &&
416+ alignedOff32 , alignedOffFitsU32 := checkedU32Int (alignedOff )
417+ if alignedOffFitsU32 &&
416418 alignedOff <= maxInt - b .dirEnd &&
417419 len (item ) <= maxInt - alignedOff {
418420 absPos := b .dirEnd + alignedOff
419421 if len (item ) <= maxInt - absPos {
420422 itemEnd := absPos + len (item )
421- idx64 := uint64 (b .itemCount ) * 8
422- if idx64 <= uint64 (maxInt ) {
423- idx := int (idx64 ) // #nosec G115 -- bounded by maxInt above.
423+ idx , ok := checkedInt (uint64 (b .itemCount ) * 8 )
424+ if ok {
424425 if itemEnd <= len (b .buf ) && idx <= len (b .buf )- 8 {
425426 if alignedOff > b .dataOffset {
426427 clear (b .buf [b .dirEnd + b .dataOffset : b .dirEnd + alignedOff ])
427428 }
428429 copy (b .buf [absPos :], item )
429- ne .PutUint32 (b .buf [idx :idx + 4 ], uint32 ( alignedOff )) // #nosec G115 -- bounded above.
430- ne .PutUint32 (b .buf [idx + 4 :idx + 8 ], uint32 ( len ( item ))) // #nosec G115 -- bounded above.
430+ ne .PutUint32 (b .buf [idx :idx + 4 ], alignedOff32 )
431+ ne .PutUint32 (b .buf [idx + 4 :idx + 8 ], itemLen32 )
431432 b .dataOffset = alignedOff + len (item )
432433 b .itemCount ++
433434 return nil
@@ -447,10 +448,12 @@ func (b *BatchBuilder) addSlow(item []byte) error {
447448 return ErrOverflow
448449 }
449450 alignedOff := Align8 (b .dataOffset )
450- if uint64 (alignedOff ) > uint64 (^ uint32 (0 )) {
451+ alignedOff32 , ok := checkedU32Int (alignedOff )
452+ if ! ok {
451453 return ErrOverflow
452454 }
453- if uint64 (len (item )) > uint64 (^ uint32 (0 )) {
455+ itemLen32 , ok := checkedU32Int (len (item ))
456+ if ! ok {
454457 return ErrOverflow
455458 }
456459 if alignedOff > maxIntValue ()- b .dirEnd {
@@ -481,16 +484,13 @@ func (b *BatchBuilder) addSlow(item []byte) error {
481484 copy (b .buf [absPos :], item )
482485
483486 // Write directory entry.
484- idx64 := uint64 (b .itemCount ) * 8
485- if idx64 > uint64 ( maxIntValue ()) {
487+ idx , ok := checkedInt ( uint64 (b .itemCount ) * 8 )
488+ if ! ok {
486489 return ErrOverflow
487490 }
488- idx := int (idx64 ) // #nosec G115 -- bounded by maxIntValue above.
489491 if idx > len (b .buf )- 8 {
490492 return ErrOverflow
491493 }
492- alignedOff32 := uint32 (alignedOff ) // #nosec G115 -- bounded by uint32 max above.
493- itemLen32 := uint32 (len (item )) // #nosec G115 -- bounded by uint32 max above.
494494 ne .PutUint32 (b .buf [idx :idx + 4 ], alignedOff32 )
495495 ne .PutUint32 (b .buf [idx + 4 :idx + 8 ], itemLen32 )
496496
0 commit comments