diff --git a/serialization/timestamp/marshal_utils.go b/serialization/timestamp/marshal_utils.go index 834d539fc..2336c58df 100644 --- a/serialization/timestamp/marshal_utils.go +++ b/serialization/timestamp/marshal_utils.go @@ -6,11 +6,10 @@ import ( "time" ) -const ( - maxValInt64 int64 = 86399999999999 - minValInt64 int64 = 0 - maxValDur time.Duration = 86399999999999 - minValDur time.Duration = 0 +var ( + maxTimestamp = time.Date(292278994, 8, 17, 7, 12, 55, 807*1000000, time.UTC) + zeroTimestamp = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) + minTimestamp = time.Date(-292275055, 5, 16, 16, 47, 4, 192*1000000, time.UTC) ) func EncInt64(v int64) ([]byte, error) { @@ -25,6 +24,9 @@ func EncInt64R(v *int64) ([]byte, error) { } func EncTime(v time.Time) ([]byte, error) { + if v.After(maxTimestamp) || v.Before(minTimestamp) { + return nil, fmt.Errorf("failed to marshal timestamp: the (%T)(%s) value should be in the range from -292275055-05-16T16:47:04.192Z to 292278994-08-17T07:12:55.807", v, v.Format(time.RFC3339Nano)) + } if v.IsZero() { return make([]byte, 0), nil } diff --git a/tests/serialization/marshal_16_timestamp_corrupt_test.go b/tests/serialization/marshal_16_timestamp_corrupt_test.go index 436c1c1df..438df5536 100644 --- a/tests/serialization/marshal_16_timestamp_corrupt_test.go +++ b/tests/serialization/marshal_16_timestamp_corrupt_test.go @@ -40,9 +40,33 @@ func TestMarshalTimestampCorrupt(t *testing.T) { } for _, tSuite := range testSuites { + marshal := tSuite.marshal unmarshal := tSuite.unmarshal t.Run(tSuite.name, func(t *testing.T) { + serialization.NegativeMarshalSet{ + Values: mod.Values{ + time.Date(292278994, 8, 17, 7, 12, 55, 808*1000000, time.UTC), + time.Date(292278994, 8, 17, 7, 12, 56, 807*1000000, time.UTC), + time.Date(292278994, 8, 17, 7, 13, 55, 807*1000000, time.UTC), + time.Date(292278994, 8, 17, 8, 12, 55, 807*1000000, time.UTC), + time.Date(292278994, 8, 18, 7, 12, 55, 807*1000000, time.UTC), + time.Date(292278994, 9, 17, 7, 12, 55, 807*1000000, time.UTC), + time.Date(292278995, 8, 17, 7, 12, 55, 807*1000000, time.UTC), + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + serialization.NegativeMarshalSet{ + Values: mod.Values{ + time.Date(-292275055, 5, 16, 16, 47, 4, 191*1000000, time.UTC), + time.Date(-292275055, 5, 16, 16, 47, 3, 192*1000000, time.UTC), + time.Date(-292275055, 5, 16, 16, 46, 4, 192*1000000, time.UTC), + time.Date(-292275055, 5, 16, 15, 47, 4, 192*1000000, time.UTC), + time.Date(-292275055, 5, 15, 16, 47, 4, 192*1000000, time.UTC), + time.Date(-292275055, 4, 16, 16, 47, 4, 192*1000000, time.UTC), + time.Date(-292275056, 5, 16, 16, 47, 4, 192*1000000, time.UTC), + }.AddVariants(mod.All...), + }.Run("small_vals", t, marshal) serialization.NegativeUnmarshalSet{ Data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff\xff"),