Skip to content

Commit

Permalink
Cast results of len to uint32 to fix compilation in i386 arch. (#961)
Browse files Browse the repository at this point in the history
GOARCH=386 go build ./ passes with this change and fails without it due
to integer overflow.

Fixes #953
  • Loading branch information
martinmr authored Aug 6, 2019
1 parent e627d49 commit 8783134
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions table/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (b *Builder) addHelper(key []byte, v y.ValueStruct) {
}

// store current entry's offset
y.AssertTrue(b.buf.Len() < math.MaxUint32)
y.AssertTrue(uint32(b.buf.Len()) < math.MaxUint32)
b.entryOffsets = append(b.entryOffsets, uint32(b.buf.Len())-b.baseOffset)

// Layout: header, diffKey, value.
Expand Down Expand Up @@ -173,7 +173,8 @@ func (b *Builder) shouldFinishBlock(key []byte, value y.ValueStruct) bool {
return false
}

y.AssertTrue((len(b.entryOffsets)+1)*4+4+8+4 < math.MaxUint32) // check for below statements
// Integer overflow check for statements below.
y.AssertTrue((uint32(len(b.entryOffsets))+1)*4+4+8+4 < math.MaxUint32)
// We should include current entry also in size, that's why +1 to len(b.entryOffsets).
entriesOffsetsSize := uint32((len(b.entryOffsets)+1)*4 +
4 + // size of list
Expand All @@ -191,7 +192,7 @@ func (b *Builder) Add(key []byte, value y.ValueStruct) {
b.finishBlock()
// Start a new block. Initialize the block.
b.baseKey = []byte{}
y.AssertTrue(b.buf.Len() < math.MaxUint32)
y.AssertTrue(uint32(b.buf.Len()) < math.MaxUint32)
b.baseOffset = uint32(b.buf.Len())
b.entryOffsets = b.entryOffsets[:0]
}
Expand Down Expand Up @@ -245,7 +246,7 @@ func (b *Builder) Finish() []byte {
n, err := b.buf.Write(index)
y.Check(err)

y.AssertTrue(n < math.MaxUint32)
y.AssertTrue(uint32(n) < math.MaxUint32)
// Write index size.
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(n))
Expand Down Expand Up @@ -277,7 +278,7 @@ func (b *Builder) writeChecksum(data []byte) {
n, err := b.buf.Write(chksum)
y.Check(err)

y.AssertTrue(n < math.MaxUint32)
y.AssertTrue(uint32(n) < math.MaxUint32)
// Write checksum size.
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(n))
Expand Down

0 comments on commit 8783134

Please sign in to comment.