Skip to content

Commit

Permalink
fix(lint): address new linter warnings surfaced by new version of gol…
Browse files Browse the repository at this point in the history
…angci-lint
  • Loading branch information
dylan-bourque committed Sep 5, 2024
1 parent c084aa0 commit d7687ca
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
28 changes: 24 additions & 4 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (d *Decoder) DecodeTag() (tag int, wireType WireType, err error) {
return 0, -1, fmt.Errorf("invalid tag value (%d) at byte %d: %w", v, d.offset, ErrInvalidFieldTag)
}
d.offset += n
//nolint: gosec // no overflow given the range check above
return int(v >> 3), WireType(v & 0x7), nil
}

Expand Down Expand Up @@ -201,6 +202,7 @@ func (d *Decoder) DecodeBytes() ([]byte, error) {
// length is good
}

//nolint: gosec // no overflow, all values <= maxFieldLen will fit in an int
nb := int(l)
if d.offset+n+nb > len(d.p) {
return nil, io.ErrUnexpectedEOF
Expand Down Expand Up @@ -228,6 +230,7 @@ func (d *Decoder) DecodeUInt32() (uint32, error) {
return 0, ErrValueOverflow
}
d.offset += n
//nolint: gosec // no overflow given the range check above
return uint32(v), nil
}

Expand Down Expand Up @@ -263,10 +266,13 @@ func (d *Decoder) DecodeInt32() (int32, error) {
if n == 0 {
return 0, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData)
}
if int64(v) > math.MaxInt32 {
// ensure the result is within [-math.MaxInt32, math.MaxInt32] when converted to a signed value
//nolint: gosec // overflow == error
if i64 := int64(v); i64 > math.MaxInt32 || i64 < math.MinInt32 {
return 0, ErrValueOverflow
}
d.offset += n
//nolint: gosec // no overflow given the range check above
return int32(v), nil
}

Expand All @@ -285,6 +291,7 @@ func (d *Decoder) DecodeInt64() (int64, error) {
return 0, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData)
}
d.offset += n
//nolint: gosec // no overflow, intentionally converting from uint64 to int64
return int64(v), nil
}

Expand Down Expand Up @@ -462,11 +469,13 @@ func (d *Decoder) DecodePackedInt32() ([]int32, error) { //nolint: dupl // FALSE
if n == 0 {
return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData)
}
// ensure the result is within [-math.MaxInt32, math.MaxInt32] when converted to a signed value
if v > math.MaxInt32 {
return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrValueOverflow)
}
nRead += uint64(n)
d.offset += n
//nolint: gosec // no overflow given the range check above
res = append(res, int32(v))
}
if nRead != l {
Expand Down Expand Up @@ -513,6 +522,7 @@ func (d *Decoder) DecodePackedInt64() ([]int64, error) { //nolint: dupl // FALSE
}
nRead += uint64(n)
d.offset += n
//nolint: gosec // no overflow given the range check above
res = append(res, int64(v))
}
if nRead != l {
Expand Down Expand Up @@ -555,11 +565,13 @@ func (d *Decoder) DecodePackedUint32() ([]uint32, error) { //nolint: dupl // FAL
if n == 0 {
return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData)
}
// ensure the result is within [0, math.MaxUInt32]
if v > math.MaxUint32 {
return nil, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrValueOverflow)
}
nRead += uint64(n)
d.offset += n
//nolint: gosec // no overflow given the range check above
res = append(res, uint32(v))
}
if nRead != l {
Expand Down Expand Up @@ -886,6 +898,7 @@ func (d *Decoder) DecodeNested(m interface{}) error {
// length is good
}

//nolint: gosec // no overflow, all values <= maxFieldLen fit into an int
nb := int(l)
if nb < 0 {
return fmt.Errorf("csproto: bad byte length %d at byte %d", nb, d.offset)
Expand Down Expand Up @@ -934,12 +947,14 @@ func (d *Decoder) Skip(tag int, wt WireType) ([]byte, error) {
if n != sz {
return nil, fmt.Errorf("invalid data at byte %d: %w", bof, ErrInvalidVarintData)
}
if int(v>>3) != tag || WireType(v&0x7) != wt {
//nolint: gosec // no overflow, all valid tags fit into an int and *anything* & 0x7 fits into WireType
thisTag, thisWireType := int(v>>3), WireType(v&0x7)
if thisTag != tag || thisWireType != wt {
return nil, &DecoderSkipError{
ExpectedTag: tag,
ExpectedWireType: wt,
ActualTag: int(v >> 3),
ActualWireType: WireType(v & 0x7),
ActualTag: thisTag,
ActualWireType: thisWireType,
}
}
}
Expand All @@ -966,6 +981,7 @@ func (d *Decoder) Skip(tag int, wt WireType) ([]byte, error) {
// length is good
}

//nolint: gosec // no overflow, all values <= maxFieldLen fit into an int
skipped = n + int(l)

case WireTypeFixed32:
Expand Down Expand Up @@ -1033,7 +1049,9 @@ func DecodeZigZag32(p []byte) (v int32, n int, err error) {
if n == 0 {
return 0, 0, ErrInvalidVarintData
}
//nolint: gosec // no overflow, just undoing the zig-zag encoding
dv = uint64((uint32(dv) >> 1) ^ uint32((int32(dv&1)<<31)>>31))
//nolint: gosec // no overflow, see above
return int32(dv), n, nil
}

Expand All @@ -1050,7 +1068,9 @@ func DecodeZigZag64(p []byte) (v int64, n int, err error) {
if n == 0 {
return 0, 0, ErrInvalidVarintData
}
//nolint: gosec // no overflow, just undoing the zig-zag encoding
dv = (dv >> 1) ^ uint64((int64(dv&1)<<63)>>63)
//nolint: gosec // no overflow, see above
return int64(dv), n, nil
}

Expand Down
2 changes: 2 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,12 @@ func EncodeTag(dest []byte, tag int, wireType WireType) int {
func EncodeVarint(dest []byte, v uint64) int {
n := 0
for v >= 1<<7 {
//nolint: gosec // v & 0x7f cannot overflow uint8
dest[n] = uint8(v&0x7f | 0x80)
v >>= 7
n++
}
//nolint: gosec // no overflow, the loop above ensures that v <= 2^7
dest[n] = uint8(v)
return n + 1
}
Expand Down
1 change: 1 addition & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func Bool(v bool) *bool {

// Int returns a pointer to v as an int32 value (for use when assigning pointer fields on Protobuf message types)
func Int(v int) *int32 {
//nolint: gosec // intentionally converting from int to int32
p := int32(v)
return &p
}
Expand Down
5 changes: 5 additions & 0 deletions lazyproto/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lazyproto

import (
"fmt"
"math"

"google.golang.org/protobuf/encoding/protowire"
)
Expand Down Expand Up @@ -84,6 +85,10 @@ func (d Def) validate(path ...int) error {
if n < 0 {
n = -1 * n
}
if n > math.MaxInt32 {
return fmt.Errorf("invalid field tag (%v) at path %v", k, path)
}
//nolint: gosec // no overflow given the range check above
if !protowire.Number(n).IsValid() {
return fmt.Errorf("invalid field tag (%v) at path %v", k, path)
}
Expand Down
11 changes: 10 additions & 1 deletion lazyproto/fielddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (fd *FieldData) UInt32Value() (uint32, error) {
if value > math.MaxUint32 {
return 0, csproto.ErrValueOverflow
}
//nolint: gosec // no overflow given the range check above
return uint32(value), nil
})
}
Expand All @@ -140,6 +141,7 @@ func (fd *FieldData) UInt32Values() ([]uint32, error) {
if value > math.MaxUint32 {
return 0, 0, csproto.ErrValueOverflow
}
//nolint: gosec // no overflow given the range check above
return uint32(value), n, nil
})
}
Expand All @@ -159,9 +161,12 @@ func (fd *FieldData) Int32Value() (int32, error) {
if err != nil {
return 0, err
}
if int64(value) > math.MaxInt32 {
// ensure the result is within [-math.MaxInt32, math.MaxInt32] when converted to a signed value
//nolint: gosec // overflow == error
if i64 := int64(value); i64 > math.MaxInt32 || i64 < math.MinInt32 {
return 0, csproto.ErrValueOverflow
}
//nolint: gosec // no overflow given the range check above
return int32(value), nil
})
}
Expand All @@ -181,9 +186,11 @@ func (fd *FieldData) Int32Values() ([]int32, error) {
if err != nil {
return 0, 0, err
}
// ensure the result is within [-math.MaxInt32, math.MaxInt32] when converted to a signed value
if value > math.MaxUint32 {
return 0, 0, csproto.ErrValueOverflow
}
//nolint: gosec // no overflow given the range check above
return int32(value), n, nil
})
}
Expand Down Expand Up @@ -267,6 +274,7 @@ func (fd *FieldData) Int64Value() (int64, error) {
if err != nil {
return 0, err
}
//nolint: gosec // no overflow, intentionally converting from uint64 to int64
return int64(value), nil
})
}
Expand All @@ -286,6 +294,7 @@ func (fd *FieldData) Int64Values() ([]int64, error) {
if err != nil {
return 0, 0, err
}
//nolint: gosec // no overflow, intentionally converting from uint64 to int64
return int64(value), n, nil
})
}
Expand Down
1 change: 1 addition & 0 deletions sizeof.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func SizeOfVarint(v uint64) int {

// SizeOfZigZag returns the number of bytes required to hold the zig zag encoding of v.
func SizeOfZigZag(v uint64) int {
//nolint: gosec // overflow is only theoretical
return SizeOfVarint((v << 1) ^ uint64((int64(v) >> 63)))
}

Expand Down

0 comments on commit d7687ca

Please sign in to comment.