Skip to content

Commit

Permalink
fix marshal timestamp added range matching check for the time.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 12, 2024
1 parent 466d662 commit 8e52b3c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 7 additions & 5 deletions serialization/timestamp/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions tests/serialization/marshal_16_timestamp_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 8e52b3c

Please sign in to comment.