Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More numeric type conversion lint fixes #146

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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