Fix int64 overflow in newNumericDateFromSeconds (#112)

This commit is contained in:
PiotrKozimor 2021-10-27 03:14:01 +02:00 committed by GitHub
parent c0ffb890f3
commit a2aa655627
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package jwt
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"time"
@ -41,7 +42,8 @@ func NewNumericDate(t time.Time) *NumericDate {
// newNumericDateFromSeconds creates a new *NumericDate out of a float64 representing a
// UNIX epoch with the float fraction representing non-integer seconds.
func newNumericDateFromSeconds(f float64) *NumericDate {
return NewNumericDate(time.Unix(0, int64(f*float64(time.Second))))
round, frac := math.Modf(f)
return NewNumericDate(time.Unix(int64(round), int64(frac*1e9)))
}
// MarshalJSON is an implementation of the json.RawMessage interface and serializes the UNIX epoch