Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,7 @@ func (d decoder) decodeDuration(b []byte, p unsafe.Pointer) ([]byte, error) {
if len(b) > 0 && b[0] != '"' {
v, r, err := d.parseInt(b, durationType)
if err != nil {
return d.inputError(b, int32Type)
}

if v < math.MinInt64 || v > math.MaxInt64 {
return r, unmarshalOverflow(b[:len(b)-len(r)], int32Type)
return r, err
}

*(*time.Duration)(p) = time.Duration(v)
Expand Down
26 changes: 26 additions & 0 deletions json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,32 @@ func TestCodecDuration(t *testing.T) {
}
}

// TestDecodeDurationOverflow checks that decoding an integer that overflows
// int64 into a time.Duration reports the error against time.Duration, the same
// way the int64 decoder does. Previously the duration decoder reported these
// errors against int32, which is neither the field's type nor consistent with
// the rest of the package.
func TestDecodeDurationOverflow(t *testing.T) {
const overflow = `100000000000000000000000`

var d time.Duration
durErr := Unmarshal([]byte(`{"D":`+overflow+`}`), &struct{ D *time.Duration }{D: &d})
if durErr == nil {
t.Fatal("expected an error decoding an overflowing duration, got nil")
}

ute, ok := durErr.(*UnmarshalTypeError)
if !ok {
t.Fatalf("expected *UnmarshalTypeError, got %T: %v", durErr, durErr)
}
if want := reflect.TypeOf(time.Duration(0)); ute.Type != want {
t.Errorf("UnmarshalTypeError.Type = %v, want %v", ute.Type, want)
}
if strings.Contains(durErr.Error(), "int32") {
t.Errorf("error should not mention int32: %v", durErr)
}
}

var numericParseTests = [...]struct {
name string
input string
Expand Down