@@ -20,6 +20,7 @@ import (
20
20
"bytes"
21
21
"encoding/hex"
22
22
"fmt"
23
+ "math"
23
24
"math/big"
24
25
"reflect"
25
26
"strconv"
@@ -943,3 +944,164 @@ func TestOOMMaliciousInput(t *testing.T) {
943
944
}
944
945
}
945
946
}
947
+
948
+ func TestPackAndUnpackIncompatibleNumber (t * testing.T ) {
949
+ var encodeABI Arguments
950
+ uint256Ty , err := NewType ("uint256" , "" , nil )
951
+ if err != nil {
952
+ panic (err )
953
+ }
954
+ encodeABI = Arguments {
955
+ {Type : uint256Ty },
956
+ }
957
+
958
+ maxU64 , ok := new (big.Int ).SetString (strconv .FormatUint (math .MaxUint64 , 10 ), 10 )
959
+ if ! ok {
960
+ panic ("bug" )
961
+ }
962
+ maxU64Plus1 := new (big.Int ).Add (maxU64 , big .NewInt (1 ))
963
+ cases := []struct {
964
+ decodeType string
965
+ inputValue * big.Int
966
+ err error
967
+ expectValue interface {}
968
+ }{
969
+ {
970
+ decodeType : "uint8" ,
971
+ inputValue : big .NewInt (math .MaxUint8 + 1 ),
972
+ err : errBadUint8 ,
973
+ },
974
+ {
975
+ decodeType : "uint8" ,
976
+ inputValue : big .NewInt (math .MaxUint8 ),
977
+ err : nil ,
978
+ expectValue : uint8 (math .MaxUint8 ),
979
+ },
980
+ {
981
+ decodeType : "uint16" ,
982
+ inputValue : big .NewInt (math .MaxUint16 + 1 ),
983
+ err : errBadUint16 ,
984
+ },
985
+ {
986
+ decodeType : "uint16" ,
987
+ inputValue : big .NewInt (math .MaxUint16 ),
988
+ err : nil ,
989
+ expectValue : uint16 (math .MaxUint16 ),
990
+ },
991
+ {
992
+ decodeType : "uint32" ,
993
+ inputValue : big .NewInt (math .MaxUint32 + 1 ),
994
+ err : errBadUint32 ,
995
+ },
996
+ {
997
+ decodeType : "uint32" ,
998
+ inputValue : big .NewInt (math .MaxUint32 ),
999
+ err : nil ,
1000
+ expectValue : uint32 (math .MaxUint32 ),
1001
+ },
1002
+ {
1003
+ decodeType : "uint64" ,
1004
+ inputValue : maxU64Plus1 ,
1005
+ err : errBadUint64 ,
1006
+ },
1007
+ {
1008
+ decodeType : "uint64" ,
1009
+ inputValue : maxU64 ,
1010
+ err : nil ,
1011
+ expectValue : uint64 (math .MaxUint64 ),
1012
+ },
1013
+ {
1014
+ decodeType : "uint256" ,
1015
+ inputValue : maxU64Plus1 ,
1016
+ err : nil ,
1017
+ expectValue : maxU64Plus1 ,
1018
+ },
1019
+ {
1020
+ decodeType : "int8" ,
1021
+ inputValue : big .NewInt (math .MaxInt8 + 1 ),
1022
+ err : errBadInt8 ,
1023
+ },
1024
+ {
1025
+ decodeType : "int8" ,
1026
+ inputValue : big .NewInt (math .MinInt8 - 1 ),
1027
+ err : errBadInt8 ,
1028
+ },
1029
+ {
1030
+ decodeType : "int8" ,
1031
+ inputValue : big .NewInt (math .MaxInt8 ),
1032
+ err : nil ,
1033
+ expectValue : int8 (math .MaxInt8 ),
1034
+ },
1035
+ {
1036
+ decodeType : "int16" ,
1037
+ inputValue : big .NewInt (math .MaxInt16 + 1 ),
1038
+ err : errBadInt16 ,
1039
+ },
1040
+ {
1041
+ decodeType : "int16" ,
1042
+ inputValue : big .NewInt (math .MinInt16 - 1 ),
1043
+ err : errBadInt16 ,
1044
+ },
1045
+ {
1046
+ decodeType : "int16" ,
1047
+ inputValue : big .NewInt (math .MaxInt16 ),
1048
+ err : nil ,
1049
+ expectValue : int16 (math .MaxInt16 ),
1050
+ },
1051
+ {
1052
+ decodeType : "int32" ,
1053
+ inputValue : big .NewInt (math .MaxInt32 + 1 ),
1054
+ err : errBadInt32 ,
1055
+ },
1056
+ {
1057
+ decodeType : "int32" ,
1058
+ inputValue : big .NewInt (math .MinInt32 - 1 ),
1059
+ err : errBadInt32 ,
1060
+ },
1061
+ {
1062
+ decodeType : "int32" ,
1063
+ inputValue : big .NewInt (math .MaxInt32 ),
1064
+ err : nil ,
1065
+ expectValue : int32 (math .MaxInt32 ),
1066
+ },
1067
+ {
1068
+ decodeType : "int64" ,
1069
+ inputValue : new (big.Int ).Add (big .NewInt (math .MaxInt64 ), big .NewInt (1 )),
1070
+ err : errBadInt64 ,
1071
+ },
1072
+ {
1073
+ decodeType : "int64" ,
1074
+ inputValue : new (big.Int ).Sub (big .NewInt (math .MinInt64 ), big .NewInt (1 )),
1075
+ err : errBadInt64 ,
1076
+ },
1077
+ {
1078
+ decodeType : "int64" ,
1079
+ inputValue : big .NewInt (math .MaxInt64 ),
1080
+ err : nil ,
1081
+ expectValue : int64 (math .MaxInt64 ),
1082
+ },
1083
+ }
1084
+ for i , testCase := range cases {
1085
+ packed , err := encodeABI .Pack (testCase .inputValue )
1086
+ if err != nil {
1087
+ panic (err )
1088
+ }
1089
+ ty , err := NewType (testCase .decodeType , "" , nil )
1090
+ if err != nil {
1091
+ panic (err )
1092
+ }
1093
+ decodeABI := Arguments {
1094
+ {Type : ty },
1095
+ }
1096
+ decoded , err := decodeABI .Unpack (packed )
1097
+ if err != testCase .err {
1098
+ t .Fatalf ("Expected error %v, actual error %v. case %d" , testCase .err , err , i )
1099
+ }
1100
+ if err != nil {
1101
+ continue
1102
+ }
1103
+ if ! reflect .DeepEqual (decoded [0 ], testCase .expectValue ) {
1104
+ t .Fatalf ("Expected value %v, actual value %v" , testCase .expectValue , decoded [0 ])
1105
+ }
1106
+ }
1107
+ }
0 commit comments