From c1f08b2bb1f4057573c1d03ee998cb87fbc9e159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Thu, 12 Jul 2018 11:23:49 +0200 Subject: [PATCH] tests/NullInt.Scan: fix integer conversion from int{8,16,32,64} --- stubs_test.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/stubs_test.go b/stubs_test.go index d3c6003..1515adb 100644 --- a/stubs_test.go +++ b/stubs_test.go @@ -2,6 +2,7 @@ package sqlmock import ( "database/sql/driver" + "errors" "fmt" "strconv" "time" @@ -25,8 +26,28 @@ func (ni *NullInt) Scan(value interface{}) (err error) { } switch v := value.(type) { - case int, int8, int16, int32, int64: - ni.Integer, ni.Valid = v.(int), true + case int: + ni.Integer, ni.Valid = v, true + return + case int8: + ni.Integer, ni.Valid = int(v), true + return + case int16: + ni.Integer, ni.Valid = int(v), true + return + case int32: + ni.Integer, ni.Valid = int(v), true + return + case int64: + const maxUint = ^uint(0) + const minUint = 0 + const maxInt = int(maxUint >> 1) + const minInt = -maxInt - 1 + + if v > int64(maxInt) || v < int64(minInt) { + return errors.New("value out of int range") + } + ni.Integer, ni.Valid = int(v), true return case []byte: ni.Integer, err = strconv.Atoi(string(v))