Skip to content

Commit 91e4bdd

Browse files
committed
Add overflow checks when unmarshaling int/uint types
1 parent 8e69c81 commit 91e4bdd

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

decoder.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,18 @@ func (d *decoder) unmarshalInt32(size uint, offset uint, result reflect.Value) (
208208
default:
209209
return newOffset, newUnmarshalTypeError(value, result.Type())
210210
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
211-
result.SetInt(int64(value))
211+
n := int64(value)
212+
if result.OverflowInt(n) {
213+
return 0, newUnmarshalTypeError(value, result.Type())
214+
}
215+
result.SetInt(n)
212216
return newOffset, nil
213217
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
214-
if value < 0 {
218+
n := uint64(value)
219+
if result.OverflowUint(n) {
215220
return 0, newUnmarshalTypeError(value, result.Type())
216221
}
217-
result.SetUint(uint64(value))
222+
result.SetUint(n)
218223
return newOffset, nil
219224
case reflect.Interface:
220225
result.Set(reflect.ValueOf(value))
@@ -293,9 +298,16 @@ func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, ui
293298
default:
294299
return newOffset, newUnmarshalTypeError(value, result.Type())
295300
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
296-
result.SetInt(int64(value))
301+
n := int64(value)
302+
if result.OverflowInt(n) {
303+
return 0, newUnmarshalTypeError(value, result.Type())
304+
}
305+
result.SetInt(n)
297306
return newOffset, nil
298307
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
308+
if result.OverflowUint(value) {
309+
return 0, newUnmarshalTypeError(value, result.Type())
310+
}
299311
result.SetUint(value)
300312
return newOffset, nil
301313
case reflect.Interface:

0 commit comments

Comments
 (0)