Repository: codenotary/immudb (Go)

Issue:
Immudb crash when callins SQLExec with `nil` parameter
<!-- Please use this template while reporting a bug and provide as much info as possible. 
If applicable, add shell logs or screenshots to help explain your problem.
Thanks! -->

**What happened**

Immudb server crashed when executing an (invalid) SQLExec query

**What you expected to happen**

Invalid or inconsistent queries should rejected in the client SDK, and definitively shouldn't crash the server

**How to reproduce it (as minimally and precisely as possible)**
Create this table on immudb, `defaultdb` database: 
```sql
CREATE TABLE IF NOT EXISTS t (id VARCHAR[256], value INTEGER, PRIMARY KEY id)
```
Then execute this go code:
```go
package main

import (
	"context"
	immuclient "github.com/codenotary/immudb/pkg/client"
	"log"
)

func main() {
	opts := immuclient.DefaultOptions().WithAddress("127.0.0.1").WithPort(3322)
	client := immuclient.NewClient().WithOptions(opts)
	ctx := context.Background()
	err := client.OpenSession(ctx, []byte("immudb"), []byte("immudb"), "defaultdb")
	if err != nil {
		log.Fatalln("Failed to connect. Reason:", err)
	}
	_, err = client.SQLExec(ctx, "update t set value=@val where id=@id", nil)
	if err != nil {
		log.Fatalln("Error", err)
	}
}
```
**Environment**
```shell
# run "immu* version" and copy/paste the output here
$ ./immudb-v1.4.1-linux-amd64-static version
immudb 1.4.1
Commit  : 99e4f1ea5970177be5287da2529849cd5ee64696
Built at: Mon, 21 Nov 2022 14:50:26 CET
Static  : true
```



Discussion:
> 

With current master I'm not able to reproduce it.

Expected error is returned instead `"table does not exist (t)"`, error detected when parsing the sql statement
Actually, I'm not able to reproduce it either with v1.4.1

`commit 99e4f1ea5970177be5287da2529849cd5ee64696 (HEAD, tag: v1.4.1, origin/release/v1.4.1)`
Tried again, it is not triggering if table `t` is missing or empty.

After inserting a couple of rows, it will trigger:
```
immuclient>exec CREATE TABLE IF NOT EXISTS t (id VARCHAR[256], value INTEGER, PRIMARY KEY id)
Updated rows: 0 
immuclient>exec insert into t(id,value) values('aa',12);
Updated rows: 1 
immuclient>exec insert into t(id,value) values('ab',13);
Updated rows: 1 
```

and then `go run`

https://user-images.githubusercontent.com/11316967/218041806-7cd96220-fb96-4106-a00e-7a561dd6edcc.mp4





Patch:
diff --git a/embedded/sql/stmt.go b/embedded/sql/stmt.go
index 96e5c70b04..d09ad2dede 100644
--- a/embedded/sql/stmt.go
+++ b/embedded/sql/stmt.go
@@ -1040,6 +1040,8 @@ func (stmt *UpdateStmt) execAt(ctx context.Context, tx *SQLTx, params map[string
 		row, err := rowReader.Read(ctx)
 		if err == ErrNoMoreRows {
 			break
+		} else if err != nil {
+			return nil, err
 		}
 
 		valuesByColID := make(map[uint32]TypedValue, len(row.ValuesBySelector))


Test patch:
diff --git a/embedded/sql/engine_test.go b/embedded/sql/engine_test.go
index 90c616efea..3dd56e3125 100644
--- a/embedded/sql/engine_test.go
+++ b/embedded/sql/engine_test.go
@@ -1654,6 +1654,27 @@ func TestUpdate(t *testing.T) {
 	})
 }
 
+func TestErrorDuringUpdate(t *testing.T) {
+	engine := setupCommonTest(t)
+
+	_, _, err := engine.Exec(
+		context.Background(), nil,
+		`
+		create table mytable(id varchar[256], value integer, primary key id);
+		insert into mytable(id, value) values('aa',12), ('ab',13);
+	`, nil)
+	require.NoError(t, err)
+
+	_, _, err = engine.Exec(context.Background(), nil, "update mytable set value=@val where id=@id", nil)
+	require.ErrorIs(t, err, ErrMissingParameter)
+
+	params := make(map[string]interface{})
+	params["id"] = "ab";
+	params["val"] = 15;
+	_, _, err = engine.Exec(context.Background(), nil, "update mytable set value=@val where id=@id", params)
+	require.NoError(t, err)
+}
+
 func TestTransactions(t *testing.T) {
 	engine := setupCommonTest(t)