Skip to content

Commit

Permalink
fix: appease linter gosec
Browse files Browse the repository at this point in the history
G115: integer overflow conversion int -> uint (gosec)
  • Loading branch information
alessio-perugini committed Jan 13, 2025
1 parent 3319e93 commit 8057d56
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go 1.23.4
replace github.com/mailru/easyjson => github.com/cmaglie/easyjson v0.8.1

require (
fortio.org/safecast v1.0.0
github.com/ProtonMail/go-crypto v1.1.3
github.com/arduino/go-paths-helper v1.12.1
github.com/arduino/go-properties-orderedmap v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
Expand Down
23 changes: 18 additions & 5 deletions internal/arduino/builder/sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"

"fortio.org/safecast"
f "github.com/arduino/arduino-cli/internal/algorithms"
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
"github.com/arduino/arduino-cli/internal/i18n"
Expand Down Expand Up @@ -297,8 +298,12 @@ func merge(builtSketchPath, bootloaderPath, mergedSketchPath *paths.Path, maximu
if segment.Address < initialAddress {
initialAddress = segment.Address
}
if segment.Address+uint32(len(segment.Data)) > lastAddress {
lastAddress = segment.Address + uint32(len(segment.Data))
lenData, err := safecast.Convert[uint32](len(segment.Data))
if err != nil {
return err
}
if segment.Address+lenData > lastAddress {
lastAddress = segment.Address + lenData
}
}
for _, segment := range memSketch.GetDataSegments() {
Expand All @@ -308,8 +313,12 @@ func merge(builtSketchPath, bootloaderPath, mergedSketchPath *paths.Path, maximu
if segment.Address < initialAddress {
initialAddress = segment.Address
}
if segment.Address+uint32(len(segment.Data)) > lastAddress {
lastAddress = segment.Address + uint32(len(segment.Data))
lenData, err := safecast.Convert[uint32](len(segment.Data))
if err != nil {
return err
}
if segment.Address+lenData > lastAddress {
lastAddress = segment.Address + lenData
}
}

Expand All @@ -323,7 +332,11 @@ func merge(builtSketchPath, bootloaderPath, mergedSketchPath *paths.Path, maximu
// Write out a .bin if the addresses doesn't go too far away from origin
// (and consequently produce a very large bin)
size := lastAddress - initialAddress
if size > uint32(maximumBinSize) {
maximumBinSizeUint32, err := safecast.Convert[uint32](maximumBinSize)
if err != nil {
return nil
}
if size > maximumBinSizeUint32 {
return nil
}
mergedSketchPathBin := paths.New(strings.TrimSuffix(mergedSketchPath.String(), ".hex") + ".bin")
Expand Down
4 changes: 3 additions & 1 deletion internal/go-configmap/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"reflect"
"strings"

"fortio.org/safecast"
)

type Map struct {
Expand Down Expand Up @@ -88,7 +90,7 @@ func tryConversion(current any, desiredType reflect.Type) (any, error) {
return uint(currentFloat), nil
}
if currentInt, ok := current.(int); ok {
return uint(currentInt), nil
return safecast.Convert[uint](currentInt)
}
case reflect.Int:
// Exception for JSON decoder: json decoder will decode all numbers as float64
Expand Down

0 comments on commit 8057d56

Please sign in to comment.