diff --git a/encode.go b/encode.go index ae5cd91..0c93774 100644 --- a/encode.go +++ b/encode.go @@ -63,10 +63,13 @@ func (s *encodeState) encodeInteger(v int64) error { // encodeDecimal serializes an decimal according to RFC 8941 Section 4.1.5. func (s *encodeState) encodeDecimal(v float64) error { - i := int64(math.RoundToEven(v * 1000)) - if i > MaxInteger || i < MinInteger { + if math.IsNaN(v) || math.IsInf(v, 0) { + return fmt.Errorf("sfv: decimal %f is not a finite number", v) + } + if v > MaxDecimal || v < MinDecimal { return fmt.Errorf("sfv: decimal %f is out of range", v) } + i := int64(math.RoundToEven(v * 1000)) // write the sign if i < 0 { diff --git a/encode_test.go b/encode_test.go index 43beb5c..7fedba8 100644 --- a/encode_test.go +++ b/encode_test.go @@ -442,6 +442,14 @@ func TestEncode_invalidTypes(t *testing.T) { t.Error("want error, not not") } + // NaN + _, err = EncodeItem(Item{ + Value: math.NaN(), + }) + if err == nil { + t.Error("want error, not not") + } + // in Parameters _, err = EncodeItem(Item{ Value: 1,