Skip to content

Commit 825eb7f

Browse files
committed
Address unsafe int conversions
1 parent ef60998 commit 825eb7f

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

sql/enumtype.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,104 @@ func (t enumType) Convert(v interface{}) (interface{}, error) {
170170
return uint16(value), nil
171171
}
172172
case uint:
173+
var maxInt int
174+
if strconv.IntSize == 32 {
175+
maxInt = math.MaxInt32
176+
} else {
177+
maxInt = math.MaxInt64
178+
}
179+
if value > uint(maxInt) {
180+
return nil, ErrConvertingToEnum.New(v)
181+
}
173182
return t.Convert(int(value))
174183
case int8:
175184
return t.Convert(int(value))
176185
case uint8:
186+
var maxInt int
187+
if strconv.IntSize == 32 {
188+
maxInt = math.MaxInt32
189+
} else {
190+
maxInt = math.MaxInt64
191+
}
192+
if value > uint8(maxInt) {
193+
return nil, ErrConvertingToEnum.New(v)
194+
}
177195
return t.Convert(int(value))
178196
case int16:
179197
return t.Convert(int(value))
180198
case uint16:
199+
var maxInt int
200+
if strconv.IntSize == 32 {
201+
maxInt = math.MaxInt32
202+
} else {
203+
maxInt = math.MaxInt64
204+
}
205+
if value > uint16(maxInt) {
206+
return nil, ErrConvertingToEnum.New(v)
207+
}
181208
return t.Convert(int(value))
182209
case int32:
183210
return t.Convert(int(value))
184211
case uint32:
212+
var maxInt int
213+
if strconv.IntSize == 32 {
214+
maxInt = math.MaxInt32
215+
} else {
216+
maxInt = math.MaxInt64
217+
}
218+
if value > uint32(maxInt) {
219+
return nil, ErrConvertingToEnum.New(v)
220+
}
185221
return t.Convert(int(value))
186222
case int64:
223+
var minInt64, maxInt64 int64
224+
if strconv.IntSize == 32 {
225+
minInt64 = int64(math.MinInt32)
226+
maxInt64 = int64(math.MaxInt32)
227+
} else {
228+
minInt64 = int64(math.MinInt64)
229+
maxInt64 = int64(math.MaxInt64)
230+
}
231+
if value > maxInt64 || value < minInt64 {
232+
return nil, ErrConvertingToEnum.New(v)
233+
}
187234
return t.Convert(int(value))
188235
case uint64:
236+
var maxInt uint64
237+
if strconv.IntSize == 32 {
238+
maxInt = uint64(math.MaxInt32)
239+
} else {
240+
maxInt = uint64(math.MaxInt64)
241+
}
242+
if value > maxInt {
243+
return nil, ErrConvertingToEnum.New(v)
244+
}
189245
return t.Convert(int(value))
190246
case float32:
247+
if value < float32(int(^uint(0)>>1)*-1) || value > float32(int(^uint(0)>>1)) {
248+
return nil, ErrConvertingToEnum.New(v)
249+
}
191250
return t.Convert(int(value))
192251
case float64:
252+
if value < float64(int(^uint(0)>>1)*-1) || value > float64(int(^uint(0)>>1)) {
253+
return nil, ErrConvertingToEnum.New(v)
254+
}
193255
return t.Convert(int(value))
194256
case decimal.Decimal:
195-
return t.Convert(value.IntPart())
257+
decInt := value.IntPart()
258+
if decInt > int64(^int(0)) || decInt < int64(-1<<((strconv.IntSize)-1)) {
259+
return nil, ErrConvertingToEnum.New(v)
260+
}
261+
return t.Convert(int(decInt))
196262
case decimal.NullDecimal:
197263
if !value.Valid {
198264
return nil, nil
199265
}
200-
return t.Convert(value.Decimal.IntPart())
266+
decInt := value.Decimal.IntPart()
267+
if decInt > int64(^int(0)) || decInt < int64(-1<<((strconv.IntSize)-1)) {
268+
return nil, ErrConvertingToEnum.New(v)
269+
}
270+
return t.Convert(int(decInt))
201271
case string:
202272
if index := t.IndexOf(value); index != -1 {
203273
if index > math.MaxUint16 {

0 commit comments

Comments
 (0)