Skip to content

Commit 19c21f8

Browse files
authored
Backport fix v2.0 -> v1.5 sanitize NaN/Infinity time
Decode CBOR NaN and Infinity time values to Go zero time. Also show reminder in doc.go that v2 is available and provide link. Closed: #141
1 parent 7b81f3e commit 19c21f8

4 files changed

Lines changed: 54 additions & 18 deletions

File tree

cache.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
// Copyright (c) Faye Amacker. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
/*
5-
Package cbor provides a fuzz-tested CBOR encoder and decoder with full support
6-
for float16, Canonical CBOR, CTAP2 Canonical CBOR, and custom settings.
7-
8-
Encoding options allow "preferred serialization" by encoding integers and floats
9-
to their smallest forms (like float16) when values fit.
10-
11-
Go struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.
12-
If both struct tags are specified then `cbor` is used.
13-
14-
Struct tags like "keyasint", "toarray", and "omitempty" make it easy to use
15-
very compact formats like COSE and CWT (CBOR Web Tokens) with structs.
16-
17-
For example, the "toarray" struct tag encodes/decodes struct fields as array elements.
18-
And "keyasint" struct tag encodes/decodes struct fields to values of maps with specified int keys.
19-
20-
fxamacker/cbor-fuzz provides coverage-guided fuzzing for this package.
21-
*/
224
package cbor
235

246
import (

decode.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,10 @@ func fillFloat(t cborType, val float64, v reflect.Value) error {
951951
return nil
952952
}
953953
if v.Type() == typeTime {
954+
if math.IsNaN(val) || math.IsInf(val, 0) {
955+
v.Set(reflect.ValueOf(time.Time{}))
956+
return nil
957+
}
954958
f1, f2 := math.Modf(val)
955959
tm := time.Unix(int64(f1), int64(f2*1e9))
956960
v.Set(reflect.ValueOf(tm))

decode_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,24 @@ func TestDecodeTime(t *testing.T) {
15811581
cborUnixTime: hexDecode("f6"),
15821582
wantTime: time.Time{},
15831583
},
1584+
{
1585+
name: "NaN",
1586+
cborRFC3339Time: hexDecode("f97e00"),
1587+
cborUnixTime: hexDecode("f97e00"),
1588+
wantTime: time.Time{},
1589+
},
1590+
{
1591+
name: "positive infinity",
1592+
cborRFC3339Time: hexDecode("f97c00"),
1593+
cborUnixTime: hexDecode("f97c00"),
1594+
wantTime: time.Time{},
1595+
},
1596+
{
1597+
name: "negative infinity",
1598+
cborRFC3339Time: hexDecode("f9fc00"),
1599+
cborUnixTime: hexDecode("f9fc00"),
1600+
wantTime: time.Time{},
1601+
},
15841602
{
15851603
name: "time without fractional seconds", // positive integer
15861604
cborRFC3339Time: hexDecode("74323031332d30332d32315432303a30343a30305a"),

doc.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Faye Amacker. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
/*
5+
Package cbor provides a fuzz-tested CBOR encoder and decoder with full support
6+
for float16, Canonical CBOR, CTAP2 Canonical CBOR, and custom settings.
7+
8+
THIS VERSION IS OUTDATED
9+
10+
V2 IS AVAILABLE
11+
12+
https://github.com/fxamacker/cbor/releases
13+
14+
Basics
15+
16+
Encoding options allow "preferred serialization" by encoding integers and floats
17+
to their smallest forms (like float16) when values fit.
18+
19+
Go struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.
20+
If both struct tags are specified then `cbor` is used.
21+
22+
Struct tags like "keyasint", "toarray", and "omitempty" make it easy to use
23+
very compact formats like COSE and CWT (CBOR Web Tokens) with structs.
24+
25+
For example, the "toarray" struct tag encodes/decodes struct fields as array elements.
26+
And "keyasint" struct tag encodes/decodes struct fields to values of maps with specified int keys.
27+
28+
fxamacker/cbor-fuzz provides coverage-guided fuzzing for this package.
29+
30+
For latest API docs, see: https://github.com/fxamacker/cbor#api
31+
*/
32+
package cbor

0 commit comments

Comments
 (0)