Skip to content

Commit

Permalink
More numeric type conversion lint fixes (#146)
Browse files Browse the repository at this point in the history
Newer versions of gosec implement more checks and guard detection for
numeric type conversions. This allows some suppressions to be removed
and more thorough guards to be used.

Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday authored Sep 16, 2024
1 parent dbd0ca7 commit 9ffb918
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Scheduled build
on:
schedule:
- cron: "23 03 * * 0"
workflow_dispatch:

jobs:
main:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,14 @@ func (c *ComplexContract) UpdateValue(ctx utils.CustomTransactionContextInterfac
return fmt.Errorf("data retrieved from world state for key %s was not of type BasicObject", id)
}

if ba.Value > math.MaxInt {
return errors.New("%d overflows an int")
newValue := float64(ba.Value) + float64(valueAdd)
if newValue > math.MaxUint {
return fmt.Errorf("%f overflows an unsigned int", newValue)
} else if newValue <= 0 {
ba.Value = 0
} else {
ba.Value = uint(newValue)
}
newValue := int(ba.Value) + valueAdd // #nosec G115

if newValue < 0 {
newValue = 0
}

ba.Value = uint(newValue)

baBytes, _ := json.Marshal(ba)

Expand Down
12 changes: 6 additions & 6 deletions internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (it *int8Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to int8", value)
}

intVal = int8(int64val) // #nosec G115
intVal = int8(int64val)
}

return reflect.ValueOf(intVal), nil
Expand All @@ -100,7 +100,7 @@ func (it *int16Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to int16", value)
}

intVal = int16(int64val) // #nosec G115
intVal = int16(int64val)
}

return reflect.ValueOf(intVal), nil
Expand All @@ -121,7 +121,7 @@ func (it *int32Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to int32", value)
}

intVal = int32(int64val) // #nosec G115
intVal = int32(int64val)
}

return reflect.ValueOf(intVal), nil
Expand Down Expand Up @@ -191,7 +191,7 @@ func (ut *uint8Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to uint8", value)
}

uintVal = uint8(uint64Val) // #nosec G115
uintVal = uint8(uint64Val)
}

return reflect.ValueOf(uintVal), nil
Expand All @@ -217,7 +217,7 @@ func (ut *uint16Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to uint16", value)
}

uintVal = uint16(uint64Val) // #nosec G115
uintVal = uint16(uint64Val)
}

return reflect.ValueOf(uintVal), nil
Expand All @@ -243,7 +243,7 @@ func (ut *uint32Type) Convert(value string) (reflect.Value, error) {
return reflect.Value{}, fmt.Errorf("cannot convert passed value %s to uint32", value)
}

uintVal = uint32(uint64Val) // #nosec G115
uintVal = uint32(uint64Val)
}

return reflect.ValueOf(uintVal), nil
Expand Down

0 comments on commit 9ffb918

Please sign in to comment.