common/hexutil: allow empty strings when decoding JSON (#3559)

This commit is contained in:
Felix Lange 2017-01-13 09:45:40 +01:00 committed by GitHub
parent c5df37c111
commit 01f6f2d741
2 changed files with 5 additions and 5 deletions

View File

@ -237,7 +237,7 @@ func checkJSON(input []byte) (raw []byte, err error) {
return nil, errNonString return nil, errNonString
} }
if len(input) == 2 { if len(input) == 2 {
return nil, ErrEmptyString return nil, nil // empty strings are allowed
} }
if !bytesHave0xPrefix(input[1:]) { if !bytesHave0xPrefix(input[1:]) {
return nil, ErrMissingPrefix return nil, ErrMissingPrefix
@ -255,7 +255,7 @@ func checkNumberJSON(input []byte) (raw []byte, err error) {
} }
input = input[1 : len(input)-1] input = input[1 : len(input)-1]
if len(input) == 0 { if len(input) == 0 {
return nil, ErrEmptyString return nil, nil // empty strings are allowed
} }
if !bytesHave0xPrefix(input) { if !bytesHave0xPrefix(input) {
return nil, ErrMissingPrefix return nil, ErrMissingPrefix

View File

@ -60,13 +60,13 @@ var unmarshalBytesTests = []unmarshalTest{
{input: "", wantErr: errNonString}, {input: "", wantErr: errNonString},
{input: "null", wantErr: errNonString}, {input: "null", wantErr: errNonString},
{input: "10", wantErr: errNonString}, {input: "10", wantErr: errNonString},
{input: `""`, wantErr: ErrEmptyString},
{input: `"0"`, wantErr: ErrMissingPrefix}, {input: `"0"`, wantErr: ErrMissingPrefix},
{input: `"0x0"`, wantErr: ErrOddLength}, {input: `"0x0"`, wantErr: ErrOddLength},
{input: `"0xxx"`, wantErr: ErrSyntax}, {input: `"0xxx"`, wantErr: ErrSyntax},
{input: `"0x01zz01"`, wantErr: ErrSyntax}, {input: `"0x01zz01"`, wantErr: ErrSyntax},
// valid encoding // valid encoding
{input: `""`, want: referenceBytes("")},
{input: `"0x"`, want: referenceBytes("")}, {input: `"0x"`, want: referenceBytes("")},
{input: `"0x02"`, want: referenceBytes("02")}, {input: `"0x02"`, want: referenceBytes("02")},
{input: `"0X02"`, want: referenceBytes("02")}, {input: `"0X02"`, want: referenceBytes("02")},
@ -125,7 +125,6 @@ var unmarshalBigTests = []unmarshalTest{
{input: "", wantErr: errNonString}, {input: "", wantErr: errNonString},
{input: "null", wantErr: errNonString}, {input: "null", wantErr: errNonString},
{input: "10", wantErr: errNonString}, {input: "10", wantErr: errNonString},
{input: `""`, wantErr: ErrEmptyString},
{input: `"0"`, wantErr: ErrMissingPrefix}, {input: `"0"`, wantErr: ErrMissingPrefix},
{input: `"0x"`, wantErr: ErrEmptyNumber}, {input: `"0x"`, wantErr: ErrEmptyNumber},
{input: `"0x01"`, wantErr: ErrLeadingZero}, {input: `"0x01"`, wantErr: ErrLeadingZero},
@ -133,6 +132,7 @@ var unmarshalBigTests = []unmarshalTest{
{input: `"0x1zz01"`, wantErr: ErrSyntax}, {input: `"0x1zz01"`, wantErr: ErrSyntax},
// valid encoding // valid encoding
{input: `""`, want: big.NewInt(0)},
{input: `"0x0"`, want: big.NewInt(0)}, {input: `"0x0"`, want: big.NewInt(0)},
{input: `"0x2"`, want: big.NewInt(0x2)}, {input: `"0x2"`, want: big.NewInt(0x2)},
{input: `"0x2F2"`, want: big.NewInt(0x2f2)}, {input: `"0x2F2"`, want: big.NewInt(0x2f2)},
@ -198,7 +198,6 @@ var unmarshalUint64Tests = []unmarshalTest{
{input: "", wantErr: errNonString}, {input: "", wantErr: errNonString},
{input: "null", wantErr: errNonString}, {input: "null", wantErr: errNonString},
{input: "10", wantErr: errNonString}, {input: "10", wantErr: errNonString},
{input: `""`, wantErr: ErrEmptyString},
{input: `"0"`, wantErr: ErrMissingPrefix}, {input: `"0"`, wantErr: ErrMissingPrefix},
{input: `"0x"`, wantErr: ErrEmptyNumber}, {input: `"0x"`, wantErr: ErrEmptyNumber},
{input: `"0x01"`, wantErr: ErrLeadingZero}, {input: `"0x01"`, wantErr: ErrLeadingZero},
@ -207,6 +206,7 @@ var unmarshalUint64Tests = []unmarshalTest{
{input: `"0x1zz01"`, wantErr: ErrSyntax}, {input: `"0x1zz01"`, wantErr: ErrSyntax},
// valid encoding // valid encoding
{input: `""`, want: uint64(0)},
{input: `"0x0"`, want: uint64(0)}, {input: `"0x0"`, want: uint64(0)},
{input: `"0x2"`, want: uint64(0x2)}, {input: `"0x2"`, want: uint64(0x2)},
{input: `"0x2F2"`, want: uint64(0x2f2)}, {input: `"0x2F2"`, want: uint64(0x2f2)},