From 8057d567501d4f31faa5e44ec9e053346bce3a55 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Mon, 13 Jan 2025 16:41:55 +0100 Subject: [PATCH] fix: appease linter gosec G115: integer overflow conversion int -> uint (gosec) --- go.mod | 1 + go.sum | 2 ++ internal/arduino/builder/sketch.go | 23 ++++++++++++++++++----- internal/go-configmap/configuration.go | 4 +++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 2da7d99dded..d91627f08c8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 18131cd7a72..94b85fdd2a2 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/arduino/builder/sketch.go b/internal/arduino/builder/sketch.go index 1dda735138f..45612d9d313 100644 --- a/internal/arduino/builder/sketch.go +++ b/internal/arduino/builder/sketch.go @@ -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" @@ -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() { @@ -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 } } @@ -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") diff --git a/internal/go-configmap/configuration.go b/internal/go-configmap/configuration.go index e1a9dbb791e..165cb66fa4e 100644 --- a/internal/go-configmap/configuration.go +++ b/internal/go-configmap/configuration.go @@ -19,6 +19,8 @@ import ( "fmt" "reflect" "strings" + + "fortio.org/safecast" ) type Map struct { @@ -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