Skip to content

Commit

Permalink
fix handling NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Dec 2, 2024
1 parent e4ef2fd commit 89a9297
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 89a9297

Please sign in to comment.