Avoid float overflow errors at parse time.

This commit is contained in:
Dave Cunningham 2020-02-23 21:24:41 +00:00
parent 413234ce49
commit 8b2b1ba839
5 changed files with 10 additions and 333 deletions

View File

@ -495,7 +495,6 @@ type LiteralNull struct{ NodeBase }
// LiteralNumber represents a JSON number
type LiteralNumber struct {
NodeBase
Value float64
OriginalString string
}

File diff suppressed because it is too large Load Diff

@ -1 +1 @@
Subproject commit 7d624c1cb2ef095c1585bd25a8be883fb16f9e73
Subproject commit 76d6ecd32e253a5429ad9568538df7ec07f470fc

View File

@ -19,7 +19,6 @@ package parser
import (
"fmt"
"strconv"
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/internal/errors"
@ -870,15 +869,8 @@ func (p *parser) parseTerminal() (ast.Node, error) {
// Literals
case tokenNumber:
// This shouldn't fail as the lexer should make sure we have good input but
// we handle the error regardless.
num, err := strconv.ParseFloat(tok.data, 64)
if err != nil {
return nil, errors.MakeStaticError("Could not parse floating point number.", tok.loc)
}
return &ast.LiteralNumber{
NodeBase: ast.NewNodeBaseLoc(tok.loc, tok.fodder),
Value: num,
OriginalString: tok.data,
}, nil
case tokenStringDouble, tokenStringSingle,

View File

@ -22,6 +22,7 @@ import (
"math"
"reflect"
"sort"
"strconv"
"github.com/google/go-jsonnet/ast"
"github.com/google/go-jsonnet/astgen"
@ -476,7 +477,14 @@ func (i *interpreter) evaluate(a ast.Node, tc tailCallStatus) (value, error) {
return makeValueNull(), nil
case *ast.LiteralNumber:
return makeValueNumber(node.Value), nil
// Since the lexer ensures that OriginalString is of
// the right form, this will only fail if the number is
// too large to fit in a double.
num, err := strconv.ParseFloat(node.OriginalString, 64)
if err != nil {
return nil, i.Error(fmt.Sprintf("overflow"), trace)
}
return makeValueNumber(num), nil
case *ast.LiteralString:
return makeValueString(node.Value), nil