From c0ea443fd638f1b316a7a5b2693c25d0d51a08ce Mon Sep 17 00:00:00 2001 From: fredgan Date: Wed, 11 Nov 2020 10:08:33 +0800 Subject: [PATCH] The decimal's range should not be parsed if parsing fraction digits fails --- pkg/yang/types.go | 3 ++- pkg/yang/types_builtin.go | 2 +- pkg/yang/types_test.go | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/yang/types.go b/pkg/yang/types.go index 12977a9..96d5674 100644 --- a/pkg/yang/types.go +++ b/pkg/yang/types.go @@ -280,7 +280,8 @@ check: y.IdentityBase = resolvedBase.Identity } - if t.Range != nil { + // If parsing fractionDigits fails, the ranges should not be parsed + if t.Range != nil && isDecimal64 && y.FractionDigits != 0 { yr, err := parseRanges(t.Range.Name, isDecimal64, uint8(y.FractionDigits)) switch { case err != nil: diff --git a/pkg/yang/types_builtin.go b/pkg/yang/types_builtin.go index 62267a2..40bfc8d 100644 --- a/pkg/yang/types_builtin.go +++ b/pkg/yang/types_builtin.go @@ -623,7 +623,7 @@ func ParseDecimal(s string, fracDigRequired uint8) (n Number, err error) { // numStr must conform to Section 9.3.4. func decimalValueFromString(numStr string, fracDigRequired uint8) (n Number, err error) { if fracDigRequired > MaxFractionDigits || fracDigRequired < 1 { - return n, fmt.Errorf("invalid number of fraction digits %d > max of %d, minimum 1", fracDigRequired, MaxFractionDigits) + return n, fmt.Errorf("invalid number of fraction digits %d: out of range [1..%d]", fracDigRequired, MaxFractionDigits) } s := numStr diff --git a/pkg/yang/types_test.go b/pkg/yang/types_test.go index 58f6cc4..966866b 100644 --- a/pkg/yang/types_test.go +++ b/pkg/yang/types_test.go @@ -67,6 +67,14 @@ func TestTypeResolve(t *testing.T) { }, err: "unknown: value 42 out of range [1..18]", }, + { + in: &Type{ + Name: "decimal64", + FractionDigits: &Value{Name: "42"}, + Range: &Range{Name: "-10 .. 10"}, + }, + err: "unknown: value 42 out of range [1..18]", + }, { in: &Type{ Name: "decimal64",