Skip to content

Commit d965129

Browse files
authored
Merge pull request #29 from trimble-oss/alert-autofix-274
Potential fix for code scanning alert no. 274: Incorrect conversion between integer types
2 parents 65a127c + 157e5d5 commit d965129

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

sql/expression/bit_ops.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func convertUintFromInt(n int64) uint64 {
189189
if err != nil {
190190
return 0
191191
}
192+
if uintVal > math.MaxInt64 {
193+
return 0 // Out of range for int64
194+
}
192195
return uintVal
193196
}
194197

sql/system_booltype.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ func (t systemBoolType) Convert(v interface{}) (interface{}, error) {
103103
// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
104104
if value == float64(int64(value)) {
105105
if value >= float64(math.MinInt64) && value <= float64(math.MaxInt64) {
106-
if intVal := int64(value); intVal >= math.MinInt8 && intVal <= math.MaxInt8 {
107-
return t.Convert(intVal)
106+
intVal := int64(value)
107+
if intVal >= math.MinInt8 && intVal <= math.MaxInt8 {
108+
return int8(intVal), nil
108109
}
109-
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
110110
}
111111
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
112112
}

sql/system_inttype.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package sql
1616

1717
import (
18+
"math"
1819
"reflect"
1920
"strconv"
2021

@@ -98,7 +99,13 @@ func (t systemIntType) Convert(v interface{}) (interface{}, error) {
9899
// Float values aren't truly accepted, but the engine will give them when it should give ints.
99100
// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
100101
if value == float64(int64(value)) {
101-
return t.Convert(int64(value))
102+
if value >= float64(t.lowerbound) && value <= float64(t.upperbound) {
103+
if value >= float64(math.MinInt64) && value <= float64(math.MaxInt64) {
104+
intVal := int64(value)
105+
return t.Convert(intVal)
106+
}
107+
}
108+
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
102109
}
103110
case decimal.Decimal:
104111
f, _ := value.Float64()

sql/system_uinttype.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ func (t systemUintType) Convert(v interface{}) (interface{}, error) {
8888
if value >= t.lowerbound && value <= t.upperbound {
8989
return value, nil
9090
}
91-
case float32:
92-
return t.Convert(float64(value))
9391
case float64:
9492
// Float values aren't truly accepted, but the engine will give them when it should give ints.
9593
// Therefore, if the float doesn't have a fractional portion, we treat it as an int.
9694
if value == float64(uint64(value)) {
97-
return t.Convert(uint64(value))
95+
if value >= float64(t.lowerbound) && value <= float64(t.upperbound) {
96+
uintVal := uint64(value)
97+
return t.Convert(uintVal)
98+
}
99+
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
98100
}
99101
case decimal.Decimal:
100102
f, _ := value.Float64()

0 commit comments

Comments
 (0)