From 1a0c8b018f9072617232927120df52e1d2e1baed Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 24 Sep 2018 18:04:45 +0100 Subject: [PATCH] Error when accessing table with undefined param When using the SQL table() function to define a table with an undefined paramater `table($undefined)`, the database would attempt to access a nil table, and would eventually crash. --- util/fncs/batch.go | 8 ++++++++ util/fncs/model.go | 9 +++++++++ util/fncs/table.go | 8 ++++++++ util/fncs/thing.go | 8 ++++++++ 4 files changed, 33 insertions(+) diff --git a/util/fncs/batch.go b/util/fncs/batch.go index 9ffea14a..1858e130 100644 --- a/util/fncs/batch.go +++ b/util/fncs/batch.go @@ -17,11 +17,19 @@ package fncs import ( "context" + "github.com/abcum/surreal/mem" "github.com/abcum/surreal/sql" ) func batch(ctx context.Context, args ...interface{}) (interface{}, error) { + tb, _ := ensureString(args[0]) id, _ := ensureSlice(args[1]) + + if len(tb) == 0 { + return nil, mem.ErrorTBNotFound + } + return sql.NewBatch(tb, id), nil + } diff --git a/util/fncs/model.go b/util/fncs/model.go index 99ec4372..241d9896 100644 --- a/util/fncs/model.go +++ b/util/fncs/model.go @@ -17,11 +17,18 @@ package fncs import ( "context" + "github.com/abcum/surreal/mem" "github.com/abcum/surreal/sql" ) func model(ctx context.Context, args ...interface{}) (interface{}, error) { + tb, _ := ensureString(args[0]) + + if len(tb) == 0 { + return nil, mem.ErrorTBNotFound + } + switch len(args) { case 2: if max, ok := ensureFloat(args[1]); ok { @@ -42,5 +49,7 @@ func model(ctx context.Context, args ...interface{}) (interface{}, error) { } } } + return nil, nil + } diff --git a/util/fncs/table.go b/util/fncs/table.go index 61d09059..d7a39fda 100644 --- a/util/fncs/table.go +++ b/util/fncs/table.go @@ -17,10 +17,18 @@ package fncs import ( "context" + "github.com/abcum/surreal/mem" "github.com/abcum/surreal/sql" ) func table(ctx context.Context, args ...interface{}) (interface{}, error) { + tb, _ := ensureString(args[0]) + + if len(tb) == 0 { + return nil, mem.ErrorTBNotFound + } + return sql.NewTable(tb), nil + } diff --git a/util/fncs/thing.go b/util/fncs/thing.go index 9f233c84..abbd5023 100644 --- a/util/fncs/thing.go +++ b/util/fncs/thing.go @@ -17,10 +17,18 @@ package fncs import ( "context" + "github.com/abcum/surreal/mem" "github.com/abcum/surreal/sql" ) func thing(ctx context.Context, args ...interface{}) (interface{}, error) { + tb, _ := ensureString(args[0]) + + if len(tb) == 0 { + return nil, mem.ErrorTBNotFound + } + return sql.NewThing(tb, args[1]), nil + }