Skip to content

Commit

Permalink
fix int overflows that happen on 32bit systems
Browse files Browse the repository at this point in the history
 - explicitly type constant in time encoding
 - use math.MaxInt32 in int tests to seed the fuzzer
  • Loading branch information
liamsi committed Nov 6, 2018
1 parent f484a51 commit 2757cf3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
11 changes: 5 additions & 6 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func EncodeFloat64(w io.Writer, f float64) (err error) {

const (
// seconds of 01-01-0001
minSeconds = -62135596800
minSeconds = int64(-62135596800)
// seconds of 10000-01-01
maxSeconds = 253402300800
maxSeconds = int64(253402300800)

// nanos have to be in interval: [0, 999999999]
maxNanos = 999999999
Expand All @@ -137,9 +137,7 @@ func (e InvalidTimeErr) Error() string {
// Milliseconds are used to ease compatibility with Javascript,
// which does not support finer resolution.
func EncodeTime(w io.Writer, t time.Time) (err error) {
var s = t.Unix()
var ns = int32(t.Nanosecond()) // this int64 -> int32 is safe.

s := t.Unix()
// TODO: We are hand-encoding a struct until MarshalAmino/UnmarshalAmino is supported.
// skip if default/zero value:
if s != 0 {
Expand All @@ -156,9 +154,10 @@ func EncodeTime(w io.Writer, t time.Time) (err error) {
return
}
}
ns := int32(t.Nanosecond()) // this int64 -> int32 cast is safe (nanos are in [0, 999999999])
// skip if default/zero value:
if ns != 0 {
// do not encode if not in interval [0, 999999999]
// do not encode if nanos exceed allowed interval
if ns < 0 || ns > maxNanos {
// we could as well panic here:
// time.Time.Nanosecond() guarantees nanos to be in [0, 999,999,999]
Expand Down
5 changes: 3 additions & 2 deletions tests/fuzz/binary/init-corpus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"math"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -48,7 +49,7 @@ func main() {
Int32Ar: [4]int32{0x7FFFFFFF, 0x6FFFFFFF, 0x5FFFFFFF, 0x77777777},
Int64Ar: [4]int64{0x7FFFFFFFFFFFF, 0x6FFFFFFFFFFFF, 0x5FFFFFFFFFFFF, 0x80808000FFFFF},
VarintAr: [4]int64{0x7FFFFFFFFFFFF, 0x6FFFFFFFFFFFF, 0x5FFFFFFFFFFFF, 0x80808000FFFFF},
IntAr: [4]int{0x7FFFFFFF, 0x6FFFFFFF, 0x5FFFFFFF, 0x80808000},
IntAr: [4]int{0x7FFFFFFF, 0x6FFFFFFF, 0x5FFFFFFF, math.MaxInt32},
ByteAr: [4]byte{0xDE, 0xAD, 0xBE, 0xEF},
Uint8Ar: [4]uint8{0xFF, 0xFF, 0x00, 0x88},
Uint16Ar: [4]uint16{0xFFFF, 0xFFFF, 0xFF00, 0x8800},
Expand All @@ -66,7 +67,7 @@ func main() {
Int32Sl: []int32{0x6FFFFFFF, 0x5FFFFFFF, 0x7FFFFFFF, 0x7F000000},
Int64Sl: []int64{0x6FFFFFFFFFFFF, 0x5FFFFFFFFFFFF, 0x7FFFFFFFFFFFF, 0x80808000FFFFF},
VarintSl: []int64{0x5FFFFFFFFFFFF, 0x7FFFFFFFFFFFF, 0x6FFFFFFFFFFFF, 0x80808000FFFFF},
IntSl: []int{0x6FFFFFFF, 0x7FFFFFFF, 0x80808000, 0x5FFFFFFF},
IntSl: []int{0x6FFFFFFF, 0x7FFFFFFF, math.MaxInt32, 0x5FFFFFFF},
ByteSl: []byte{0xAD, 0xBE, 0xDE, 0xEF},
Uint8Sl: []uint8{0xFF, 0x00, 0x88, 0xFF},
Uint16Sl: []uint16{0xFFFF, 0xFFFF, 0xFF00, 0x8800},
Expand Down

0 comments on commit 2757cf3

Please sign in to comment.