Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ytypes/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ytypes
import (
"encoding/base64"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
Expand Down Expand Up @@ -742,6 +743,9 @@ func sanitizeJSON(parent interface{}, schema *yang.Entry, fieldName string, valu
if err != nil {
return nil, fmt.Errorf("error parsing %v for schema %s: %v", value, schema.Name, err)
}
if math.IsInf(floatV, 0) || math.IsNaN(floatV) {
return nil, fmt.Errorf("error parsing %v for schema %s: decimal64 value must be finite", value, schema.Name)
}

return floatV, nil

Expand Down
10 changes: 10 additions & 0 deletions ytypes/leaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,16 @@ func TestUnmarshalLeafJSONEncoding(t *testing.T) {
json: `{"decimal-leaf" : "forty-two"}`,
wantErr: `error parsing forty-two for schema decimal-leaf: strconv.ParseFloat: parsing "forty-two": invalid syntax`,
},
{
desc: "decimal NaN",
json: `{"decimal-leaf" : "NaN"}`,
wantErr: `error parsing NaN for schema decimal-leaf: decimal64 value must be finite`,
},
{
desc: "decimal Inf",
json: `{"decimal-leaf" : "Inf"}`,
wantErr: `error parsing Inf for schema decimal-leaf: decimal64 value must be finite`,
},
{
desc: "empty valid type",
json: `{"empty-leaf": [null]}`,
Expand Down
7 changes: 7 additions & 0 deletions ytypes/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,13 @@ func TestStringToKeyType(t *testing.T) {
inFieldName: "Decimal64Key",
in: "I am a float?",
wantErrSubstring: "unable to convert",
}, {
name: "invalid: non-finite float",
inSchema: listSchema.Dir["decimal64Key"],
inParent: &allKeysListStruct{},
inFieldName: "Decimal64Key",
in: "Inf",
wantErrSubstring: "value must be finite",
}, {
name: "invalid: too big for int8",
inSchema: listSchema.Dir["int8Key"],
Expand Down
4 changes: 4 additions & 0 deletions ytypes/util_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ytypes
import (
"encoding/base64"
"fmt"
"math"
"reflect"
"strconv"

Expand Down Expand Up @@ -274,6 +275,9 @@ func stringToKeyType(schema *yang.Entry, parent interface{}, fieldName string, v
if err != nil {
return reflect.ValueOf(nil), fmt.Errorf("unable to convert %q to %v: %v", value, ykind, err)
}
if math.IsInf(floatV, 0) || math.IsNaN(floatV) {
return reflect.ValueOf(nil), fmt.Errorf("unable to convert %q to %v: value must be finite", value, ykind)
}
return reflect.ValueOf(floatV), nil
case yang.Yenum, yang.Yidentityref:
enumVal, err := enumStringToValue(parent, fieldName, value)
Expand Down
Loading