mirror of
https://github.com/securego/gosec.git
synced 2024-12-25 03:55:54 +00:00
fix: revive.redefines-builtin-id lint warnings (#1257)
Co-authored-by: Cosmin Cojocar <cosmin@cojocar.ch>
This commit is contained in:
parent
399e835157
commit
8c602d0bc4
4 changed files with 28 additions and 36 deletions
|
@ -44,6 +44,7 @@ linters-settings:
|
||||||
rules:
|
rules:
|
||||||
- name: dot-imports
|
- name: dot-imports
|
||||||
disabled: true
|
disabled: true
|
||||||
|
- name: redefines-builtin-id
|
||||||
|
|
||||||
run:
|
run:
|
||||||
timeout: 5m
|
timeout: 5m
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
package analyzers
|
package analyzers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/token"
|
"go/token"
|
||||||
"math"
|
"math"
|
||||||
|
@ -22,7 +23,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/exp/constraints"
|
|
||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
"golang.org/x/tools/go/analysis/passes/buildssa"
|
"golang.org/x/tools/go/analysis/passes/buildssa"
|
||||||
"golang.org/x/tools/go/ssa"
|
"golang.org/x/tools/go/ssa"
|
||||||
|
@ -141,8 +141,8 @@ func parseIntType(intType string) (integer, error) {
|
||||||
return integer{}, fmt.Errorf("invalid bit size: %d", intSize)
|
return integer{}, fmt.Errorf("invalid bit size: %d", intSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
var min int
|
var minVal int
|
||||||
var max uint
|
var maxVal uint
|
||||||
|
|
||||||
if signed {
|
if signed {
|
||||||
shiftAmount := intSize - 1
|
shiftAmount := intSize - 1
|
||||||
|
@ -152,19 +152,19 @@ func parseIntType(intType string) (integer, error) {
|
||||||
return integer{}, fmt.Errorf("invalid shift amount: %d", shiftAmount)
|
return integer{}, fmt.Errorf("invalid shift amount: %d", shiftAmount)
|
||||||
}
|
}
|
||||||
|
|
||||||
max = (1 << uint(shiftAmount)) - 1
|
maxVal = (1 << uint(shiftAmount)) - 1
|
||||||
min = -1 << (intSize - 1)
|
minVal = -1 << (intSize - 1)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
max = (1 << uint(intSize)) - 1
|
maxVal = (1 << uint(intSize)) - 1
|
||||||
min = 0
|
minVal = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return integer{
|
return integer{
|
||||||
signed: signed,
|
signed: signed,
|
||||||
size: intSize,
|
size: intSize,
|
||||||
min: min,
|
min: minVal,
|
||||||
max: max,
|
max: maxVal,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,8 +274,8 @@ func hasExplicitRangeCheck(instr *ssa.Convert, dstType string) bool {
|
||||||
case *ssa.If:
|
case *ssa.If:
|
||||||
result := getResultRange(v, instr, visitedIfs)
|
result := getResultRange(v, instr, visitedIfs)
|
||||||
if result.isRangeCheck {
|
if result.isRangeCheck {
|
||||||
minValue = max(minValue, &result.minValue)
|
minValue = max(minValue, result.minValue)
|
||||||
maxValue = min(maxValue, &result.maxValue)
|
maxValue = min(maxValue, result.maxValue)
|
||||||
explicitPositiveVals = append(explicitPositiveVals, result.explicitPositiveVals...)
|
explicitPositiveVals = append(explicitPositiveVals, result.explicitPositiveVals...)
|
||||||
explicitNegativeVals = append(explicitNegativeVals, result.explicitNegativeVals...)
|
explicitNegativeVals = append(explicitNegativeVals, result.explicitNegativeVals...)
|
||||||
}
|
}
|
||||||
|
@ -328,12 +328,12 @@ func getResultRange(ifInstr *ssa.If, instr *ssa.Convert, visitedIfs map[*ssa.If]
|
||||||
|
|
||||||
if thenBounds.convertFound {
|
if thenBounds.convertFound {
|
||||||
result.convertFound = true
|
result.convertFound = true
|
||||||
result.minValue = max(result.minValue, thenBounds.minValue)
|
result.minValue = maxWithPtr(result.minValue, thenBounds.minValue)
|
||||||
result.maxValue = min(result.maxValue, thenBounds.maxValue)
|
result.maxValue = minWithPtr(result.maxValue, thenBounds.maxValue)
|
||||||
} else if elseBounds.convertFound {
|
} else if elseBounds.convertFound {
|
||||||
result.convertFound = true
|
result.convertFound = true
|
||||||
result.minValue = max(result.minValue, elseBounds.minValue)
|
result.minValue = maxWithPtr(result.minValue, elseBounds.minValue)
|
||||||
result.maxValue = min(result.maxValue, elseBounds.maxValue)
|
result.maxValue = minWithPtr(result.maxValue, elseBounds.maxValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.explicitPositiveVals = append(result.explicitPositiveVals, thenBounds.explicitPositiveVals...)
|
result.explicitPositiveVals = append(result.explicitPositiveVals, thenBounds.explicitPositiveVals...)
|
||||||
|
@ -388,14 +388,14 @@ func updateResultFromBinOp(result *rangeResult, binOp *ssa.BinOp, instr *ssa.Con
|
||||||
}
|
}
|
||||||
|
|
||||||
if op == "neg" {
|
if op == "neg" {
|
||||||
min := result.minValue
|
minVal := result.minValue
|
||||||
max := result.maxValue
|
maxVal := result.maxValue
|
||||||
|
|
||||||
if min >= 0 {
|
if minVal >= 0 {
|
||||||
result.maxValue = uint(min)
|
result.maxValue = uint(minVal)
|
||||||
}
|
}
|
||||||
if max <= math.MaxInt {
|
if maxVal <= math.MaxInt {
|
||||||
result.minValue = int(max)
|
result.minValue = int(maxVal)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -449,8 +449,8 @@ func walkBranchForConvert(block *ssa.BasicBlock, instr *ssa.Convert, visitedIfs
|
||||||
bounds.convertFound = bounds.convertFound || result.convertFound
|
bounds.convertFound = bounds.convertFound || result.convertFound
|
||||||
|
|
||||||
if result.isRangeCheck {
|
if result.isRangeCheck {
|
||||||
bounds.minValue = toPtr(max(result.minValue, bounds.minValue))
|
bounds.minValue = toPtr(maxWithPtr(result.minValue, bounds.minValue))
|
||||||
bounds.maxValue = toPtr(min(result.maxValue, bounds.maxValue))
|
bounds.maxValue = toPtr(minWithPtr(result.maxValue, bounds.maxValue))
|
||||||
bounds.explicitPositiveVals = append(bounds.explicitPositiveVals, result.explicitPositiveVals...)
|
bounds.explicitPositiveVals = append(bounds.explicitPositiveVals, result.explicitPositiveVals...)
|
||||||
bounds.explicitNegativeVals = append(bounds.explicitNegativeVals, result.explicitNegativeVals...)
|
bounds.explicitNegativeVals = append(bounds.explicitNegativeVals, result.explicitNegativeVals...)
|
||||||
}
|
}
|
||||||
|
@ -540,24 +540,18 @@ func explicitValsInRange(explicitPosVals []uint, explicitNegVals []int, dstInt i
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func min[T constraints.Integer](a T, b *T) T {
|
func minWithPtr[T cmp.Ordered](a T, b *T) T {
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
if a < *b {
|
return min(a, *b)
|
||||||
return a
|
|
||||||
}
|
|
||||||
return *b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func max[T constraints.Integer](a T, b *T) T {
|
func maxWithPtr[T cmp.Ordered](a T, b *T) T {
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
if a > *b {
|
return max(a, *b)
|
||||||
return a
|
|
||||||
}
|
|
||||||
return *b
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func toPtr[T any](a T) *T {
|
func toPtr[T any](a T) *T {
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -11,7 +11,6 @@ require (
|
||||||
github.com/onsi/gomega v1.35.1
|
github.com/onsi/gomega v1.35.1
|
||||||
github.com/stretchr/testify v1.10.0
|
github.com/stretchr/testify v1.10.0
|
||||||
golang.org/x/crypto v0.29.0
|
golang.org/x/crypto v0.29.0
|
||||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
|
|
||||||
golang.org/x/lint v0.0.0-20241112194109-818c5a804067
|
golang.org/x/lint v0.0.0-20241112194109-818c5a804067
|
||||||
golang.org/x/text v0.20.0
|
golang.org/x/text v0.20.0
|
||||||
golang.org/x/tools v0.27.0
|
golang.org/x/tools v0.27.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -428,8 +428,6 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0
|
||||||
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
|
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
|
||||||
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
|
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
|
||||||
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
|
golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw=
|
||||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
|
|
||||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
|
|
||||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
|
Loading…
Reference in a new issue