Skip to content

Commit

Permalink
The decimal's range should not be parsed if parsing fraction digits f…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
fredgan committed Nov 11, 2020
1 parent ec3a833 commit c0ea443
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/yang/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkg/yang/types_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/yang/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c0ea443

Please sign in to comment.