summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/objects
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/d5/tengo/objects')
-rw-r--r--vendor/github.com/d5/tengo/objects/builtin_convert.go14
-rw-r--r--vendor/github.com/d5/tengo/objects/builtin_json.go6
-rw-r--r--vendor/github.com/d5/tengo/objects/builtin_print.go10
-rw-r--r--vendor/github.com/d5/tengo/objects/builtins.go167
-rw-r--r--vendor/github.com/d5/tengo/objects/bytes.go5
-rw-r--r--vendor/github.com/d5/tengo/objects/callable_func.go2
-rw-r--r--vendor/github.com/d5/tengo/objects/conversion.go10
-rw-r--r--vendor/github.com/d5/tengo/objects/errors.go6
-rw-r--r--vendor/github.com/d5/tengo/objects/string.go10
9 files changed, 158 insertions, 72 deletions
diff --git a/vendor/github.com/d5/tengo/objects/builtin_convert.go b/vendor/github.com/d5/tengo/objects/builtin_convert.go
index 7d9a8733..b5f2d05d 100644
--- a/vendor/github.com/d5/tengo/objects/builtin_convert.go
+++ b/vendor/github.com/d5/tengo/objects/builtin_convert.go
@@ -1,5 +1,7 @@
package objects
+import "github.com/d5/tengo"
+
func builtinString(args ...Object) (Object, error) {
argsLen := len(args)
if !(argsLen == 1 || argsLen == 2) {
@@ -12,6 +14,10 @@ func builtinString(args ...Object) (Object, error) {
v, ok := ToString(args[0])
if ok {
+ if len(v) > tengo.MaxStringLen {
+ return nil, ErrStringLimit
+ }
+
return &String{Value: v}, nil
}
@@ -117,11 +123,19 @@ func builtinBytes(args ...Object) (Object, error) {
// bytes(N) => create a new bytes with given size N
if n, ok := args[0].(*Int); ok {
+ if n.Value > int64(tengo.MaxBytesLen) {
+ return nil, ErrBytesLimit
+ }
+
return &Bytes{Value: make([]byte, int(n.Value))}, nil
}
v, ok := ToByteSlice(args[0])
if ok {
+ if len(v) > tengo.MaxBytesLen {
+ return nil, ErrBytesLimit
+ }
+
return &Bytes{Value: v}, nil
}
diff --git a/vendor/github.com/d5/tengo/objects/builtin_json.go b/vendor/github.com/d5/tengo/objects/builtin_json.go
index c0810f7d..b3413651 100644
--- a/vendor/github.com/d5/tengo/objects/builtin_json.go
+++ b/vendor/github.com/d5/tengo/objects/builtin_json.go
@@ -2,6 +2,8 @@ package objects
import (
"encoding/json"
+
+ "github.com/d5/tengo"
)
// to_json(v object) => bytes
@@ -15,6 +17,10 @@ func builtinToJSON(args ...Object) (Object, error) {
return &Error{Value: &String{Value: err.Error()}}, nil
}
+ if len(res) > tengo.MaxBytesLen {
+ return nil, ErrBytesLimit
+ }
+
return &Bytes{Value: res}, nil
}
diff --git a/vendor/github.com/d5/tengo/objects/builtin_print.go b/vendor/github.com/d5/tengo/objects/builtin_print.go
index c5fe36db..58f22610 100644
--- a/vendor/github.com/d5/tengo/objects/builtin_print.go
+++ b/vendor/github.com/d5/tengo/objects/builtin_print.go
@@ -2,6 +2,8 @@ package objects
import (
"fmt"
+
+ "github.com/d5/tengo"
)
// print(args...)
@@ -71,5 +73,11 @@ func builtinSprintf(args ...Object) (Object, error) {
formatArgs[idx] = objectToInterface(arg)
}
- return &String{Value: fmt.Sprintf(format.Value, formatArgs...)}, nil
+ s := fmt.Sprintf(format.Value, formatArgs...)
+
+ if len(s) > tengo.MaxStringLen {
+ return nil, ErrStringLimit
+ }
+
+ return &String{Value: s}, nil
}
diff --git a/vendor/github.com/d5/tengo/objects/builtins.go b/vendor/github.com/d5/tengo/objects/builtins.go
index 67553932..42c1a759 100644
--- a/vendor/github.com/d5/tengo/objects/builtins.go
+++ b/vendor/github.com/d5/tengo/objects/builtins.go
@@ -1,135 +1,164 @@
package objects
-// NamedBuiltinFunc is a named builtin function.
-type NamedBuiltinFunc struct {
- Name string
- Func CallableFunc
-}
-
// Builtins contains all default builtin functions.
-var Builtins = []NamedBuiltinFunc{
+// Use GetBuiltinFunctions instead of accessing Builtins directly.
+var Builtins = []BuiltinFunction{
{
- Name: "print",
- Func: builtinPrint,
+ Name: "print",
+ Value: builtinPrint,
},
{
- Name: "printf",
- Func: builtinPrintf,
+ Name: "printf",
+ Value: builtinPrintf,
},
{
- Name: "sprintf",
- Func: builtinSprintf,
+ Name: "sprintf",
+ Value: builtinSprintf,
},
{
- Name: "len",
- Func: builtinLen,
+ Name: "len",
+ Value: builtinLen,
},
{
- Name: "copy",
- Func: builtinCopy,
+ Name: "copy",
+ Value: builtinCopy,
},
{
- Name: "append",
- Func: builtinAppend,
+ Name: "append",
+ Value: builtinAppend,
},
{
- Name: "string",
- Func: builtinString,
+ Name: "string",
+ Value: builtinString,
},
{
- Name: "int",
- Func: builtinInt,
+ Name: "int",
+ Value: builtinInt,
},
{
- Name: "bool",
- Func: builtinBool,
+ Name: "bool",
+ Value: builtinBool,
},
{
- Name: "float",
- Func: builtinFloat,
+ Name: "float",
+ Value: builtinFloat,
},
{
- Name: "char",
- Func: builtinChar,
+ Name: "char",
+ Value: builtinChar,
},
{
- Name: "bytes",
- Func: builtinBytes,
+ Name: "bytes",
+ Value: builtinBytes,
},
{
- Name: "time",
- Func: builtinTime,
+ Name: "time",
+ Value: builtinTime,
},
{
- Name: "is_int",
- Func: builtinIsInt,
+ Name: "is_int",
+ Value: builtinIsInt,
},
{
- Name: "is_float",
- Func: builtinIsFloat,
+ Name: "is_float",
+ Value: builtinIsFloat,
},
{
- Name: "is_string",
- Func: builtinIsString,
+ Name: "is_string",
+ Value: builtinIsString,
},
{
- Name: "is_bool",
- Func: builtinIsBool,
+ Name: "is_bool",
+ Value: builtinIsBool,
},
{
- Name: "is_char",
- Func: builtinIsChar,
+ Name: "is_char",
+ Value: builtinIsChar,
},
{
- Name: "is_bytes",
- Func: builtinIsBytes,
+ Name: "is_bytes",
+ Value: builtinIsBytes,
},
{
- Name: "is_array",
- Func: builtinIsArray,
+ Name: "is_array",
+ Value: builtinIsArray,
},
{
- Name: "is_immutable_array",
- Func: builtinIsImmutableArray,
+ Name: "is_immutable_array",
+ Value: builtinIsImmutableArray,
},
{
- Name: "is_map",
- Func: builtinIsMap,
+ Name: "is_map",
+ Value: builtinIsMap,
},
{
- Name: "is_immutable_map",
- Func: builtinIsImmutableMap,
+ Name: "is_immutable_map",
+ Value: builtinIsImmutableMap,
},
{
- Name: "is_time",
- Func: builtinIsTime,
+ Name: "is_time",
+ Value: builtinIsTime,
},
{
- Name: "is_error",
- Func: builtinIsError,
+ Name: "is_error",
+ Value: builtinIsError,
},
{
- Name: "is_undefined",
- Func: builtinIsUndefined,
+ Name: "is_undefined",
+ Value: builtinIsUndefined,
},
{
- Name: "is_function",
- Func: builtinIsFunction,
+ Name: "is_function",
+ Value: builtinIsFunction,
},
{
- Name: "is_callable",
- Func: builtinIsCallable,
+ Name: "is_callable",
+ Value: builtinIsCallable,
},
{
- Name: "to_json",
- Func: builtinToJSON,
+ Name: "to_json",
+ Value: builtinToJSON,
},
{
- Name: "from_json",
- Func: builtinFromJSON,
+ Name: "from_json",
+ Value: builtinFromJSON,
},
{
- Name: "type_name",
- Func: builtinTypeName,
+ Name: "type_name",
+ Value: builtinTypeName,
},
}
+
+// AllBuiltinFunctionNames returns a list of all default builtin function names.
+func AllBuiltinFunctionNames() []string {
+ var names []string
+ for _, bf := range Builtins {
+ names = append(names, bf.Name)
+ }
+ return names
+}
+
+// GetBuiltinFunctions returns a slice of builtin function objects.
+// GetBuiltinFunctions removes the duplicate names, and, the returned builtin functions
+// are not guaranteed to be in the same order as names.
+func GetBuiltinFunctions(names ...string) []*BuiltinFunction {
+ include := make(map[string]bool)
+ for _, name := range names {
+ include[name] = true
+ }
+
+ var builtinFuncs []*BuiltinFunction
+ for _, bf := range Builtins {
+ if include[bf.Name] {
+ bf := bf
+ builtinFuncs = append(builtinFuncs, &bf)
+ }
+ }
+
+ return builtinFuncs
+}
+
+// GetAllBuiltinFunctions returns all builtin functions.
+func GetAllBuiltinFunctions() []*BuiltinFunction {
+ return GetBuiltinFunctions(AllBuiltinFunctionNames()...)
+}
diff --git a/vendor/github.com/d5/tengo/objects/bytes.go b/vendor/github.com/d5/tengo/objects/bytes.go
index 7d8d6694..16b61684 100644
--- a/vendor/github.com/d5/tengo/objects/bytes.go
+++ b/vendor/github.com/d5/tengo/objects/bytes.go
@@ -3,6 +3,7 @@ package objects
import (
"bytes"
+ "github.com/d5/tengo"
"github.com/d5/tengo/compiler/token"
)
@@ -27,6 +28,10 @@ func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) {
case token.Add:
switch rhs := rhs.(type) {
case *Bytes:
+ if len(o.Value)+len(rhs.Value) > tengo.MaxBytesLen {
+ return nil, ErrBytesLimit
+ }
+
return &Bytes{Value: append(o.Value, rhs.Value...)}, nil
}
}
diff --git a/vendor/github.com/d5/tengo/objects/callable_func.go b/vendor/github.com/d5/tengo/objects/callable_func.go
index cf9b43aa..ad25e65d 100644
--- a/vendor/github.com/d5/tengo/objects/callable_func.go
+++ b/vendor/github.com/d5/tengo/objects/callable_func.go
@@ -1,4 +1,4 @@
package objects
// CallableFunc is a function signature for the callable functions.
-type CallableFunc func(args ...Object) (ret Object, err error)
+type CallableFunc = func(args ...Object) (ret Object, err error)
diff --git a/vendor/github.com/d5/tengo/objects/conversion.go b/vendor/github.com/d5/tengo/objects/conversion.go
index 3c17546f..714f2617 100644
--- a/vendor/github.com/d5/tengo/objects/conversion.go
+++ b/vendor/github.com/d5/tengo/objects/conversion.go
@@ -4,6 +4,8 @@ import (
"fmt"
"strconv"
"time"
+
+ "github.com/d5/tengo"
)
// ToString will try to convert object o to string value.
@@ -194,6 +196,9 @@ func FromInterface(v interface{}) (Object, error) {
case nil:
return UndefinedValue, nil
case string:
+ if len(v) > tengo.MaxStringLen {
+ return nil, ErrStringLimit
+ }
return &String{Value: v}, nil
case int64:
return &Int{Value: v}, nil
@@ -211,6 +216,9 @@ func FromInterface(v interface{}) (Object, error) {
case float64:
return &Float{Value: v}, nil
case []byte:
+ if len(v) > tengo.MaxBytesLen {
+ return nil, ErrBytesLimit
+ }
return &Bytes{Value: v}, nil
case error:
return &Error{Value: &String{Value: v.Error()}}, nil
@@ -243,6 +251,8 @@ func FromInterface(v interface{}) (Object, error) {
return &Time{Value: v}, nil
case Object:
return v, nil
+ case CallableFunc:
+ return &UserFunction{Value: v}, nil
}
return nil, fmt.Errorf("cannot convert to object: %T", v)
diff --git a/vendor/github.com/d5/tengo/objects/errors.go b/vendor/github.com/d5/tengo/objects/errors.go
index e4012314..bcd480a1 100644
--- a/vendor/github.com/d5/tengo/objects/errors.go
+++ b/vendor/github.com/d5/tengo/objects/errors.go
@@ -20,6 +20,12 @@ var ErrInvalidOperator = errors.New("invalid operator")
// ErrWrongNumArguments represents a wrong number of arguments error.
var ErrWrongNumArguments = errors.New("wrong number of arguments")
+// ErrBytesLimit represents an error where the size of bytes value exceeds the limit.
+var ErrBytesLimit = errors.New("exceeding bytes size limit")
+
+// ErrStringLimit represents an error where the size of string value exceeds the limit.
+var ErrStringLimit = errors.New("exceeding string size limit")
+
// ErrInvalidArgumentType represents an invalid argument value type error.
type ErrInvalidArgumentType struct {
Name string
diff --git a/vendor/github.com/d5/tengo/objects/string.go b/vendor/github.com/d5/tengo/objects/string.go
index 6a53b44d..c25b0502 100644
--- a/vendor/github.com/d5/tengo/objects/string.go
+++ b/vendor/github.com/d5/tengo/objects/string.go
@@ -3,6 +3,7 @@ package objects
import (
"strconv"
+ "github.com/d5/tengo"
"github.com/d5/tengo/compiler/token"
)
@@ -28,9 +29,16 @@ func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) {
case token.Add:
switch rhs := rhs.(type) {
case *String:
+ if len(o.Value)+len(rhs.Value) > tengo.MaxStringLen {
+ return nil, ErrStringLimit
+ }
return &String{Value: o.Value + rhs.Value}, nil
default:
- return &String{Value: o.Value + rhs.String()}, nil
+ rhsStr := rhs.String()
+ if len(o.Value)+len(rhsStr) > tengo.MaxStringLen {
+ return nil, ErrStringLimit
+ }
+ return &String{Value: o.Value + rhsStr}, nil
}
}