mirror of
https://github.com/google/go-jsonnet.git
synced 2025-09-29 09:21:03 +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
|
// LiteralNumber represents a JSON number
|
||||||
type LiteralNumber struct {
|
type LiteralNumber struct {
|
||||||
NodeBase
|
NodeBase
|
||||||
Value float64
|
|
||||||
OriginalString string
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/google/go-jsonnet/ast"
|
"github.com/google/go-jsonnet/ast"
|
||||||
"github.com/google/go-jsonnet/internal/errors"
|
"github.com/google/go-jsonnet/internal/errors"
|
||||||
@ -870,15 +869,8 @@ func (p *parser) parseTerminal() (ast.Node, error) {
|
|||||||
|
|
||||||
// Literals
|
// Literals
|
||||||
case tokenNumber:
|
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{
|
return &ast.LiteralNumber{
|
||||||
NodeBase: ast.NewNodeBaseLoc(tok.loc, tok.fodder),
|
NodeBase: ast.NewNodeBaseLoc(tok.loc, tok.fodder),
|
||||||
Value: num,
|
|
||||||
OriginalString: tok.data,
|
OriginalString: tok.data,
|
||||||
}, nil
|
}, nil
|
||||||
case tokenStringDouble, tokenStringSingle,
|
case tokenStringDouble, tokenStringSingle,
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/google/go-jsonnet/ast"
|
"github.com/google/go-jsonnet/ast"
|
||||||
"github.com/google/go-jsonnet/astgen"
|
"github.com/google/go-jsonnet/astgen"
|
||||||
@ -476,7 +477,14 @@ func (i *interpreter) evaluate(a ast.Node, tc tailCallStatus) (value, error) {
|
|||||||
return makeValueNull(), nil
|
return makeValueNull(), nil
|
||||||
|
|
||||||
case *ast.LiteralNumber:
|
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:
|
case *ast.LiteralString:
|
||||||
return makeValueString(node.Value), nil
|
return makeValueString(node.Value), nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user