From 89a92971a3320c4ab1ce1c47feeb35eb3cd7e8c3 Mon Sep 17 00:00:00 2001 From: ICHINOSE Shogo Date: Mon, 2 Dec 2024 22:57:15 +0900 Subject: [PATCH] fix handling NaN --- encode.go | 7 +++++-- encode_test.go | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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,