mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-29 01:11:02 +02:00
Avoid float overflow errors at parse time.
This commit is contained in:
parent
413234ce49
commit
8b2b1ba839
@ -495,7 +495,6 @@ type LiteralNull struct{ NodeBase }
|
||||
// LiteralNumber represents a JSON number
|
||||
type LiteralNumber struct {
|
||||
NodeBase
|
||||
Value float64
|
||||
OriginalString string
|
||||
}
|
||||
|
||||
|
322
astgen/stdast.go
322
astgen/stdast.go
File diff suppressed because it is too large
Load Diff
@ -1 +1 @@
|
||||
Subproject commit 7d624c1cb2ef095c1585bd25a8be883fb16f9e73
|
||||
Subproject commit 76d6ecd32e253a5429ad9568538df7ec07f470fc
|
@ -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,
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user