Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic index out of range inserting empty []byte in BLOB column #73

Closed
xhit opened this issue Mar 23, 2020 · 4 comments · Fixed by #74
Closed

panic index out of range inserting empty []byte in BLOB column #73

xhit opened this issue Mar 23, 2020 · 4 comments · Fixed by #74

Comments

@xhit
Copy link
Contributor

xhit commented Mar 23, 2020

  • os Version: Windows 10
  • cli-driver Version: 11.1
  • Db2 Server Version: 11.5.0.1077
  • go Version: 1.13.5
go env Output:
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\sdelacruz\AppData\Local\go-build
set GOENV=C:\Users\sdelacruz\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\sdelacruz\Documents\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\sdelacruz\Documents\go\src\sdi\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\SDELAC~1\AppData\Local\Temp\go-build670067514=/tmp/go-build -gno-record-gcc-switches

The driver cannot insert nil in a column with data type BLOB, launch this error from IBM:
SQLExecute: {HY099} [IBM][CLI Driver] CLI0164E Nullable type out of range. SQLSTATE=HY099

So trying to insert a empty []byte in a data type BLOB return this panic:

panic: runtime error: index out of range [0] with length 0

goroutine 18 [running]:
github.com/ibmdb/go_ibm_db.(*Parameter).BindValue(0xc0000940d8, 0x10001, 0x3, 0xcba240, 0xc00029a240, 0x0, 0x0)
        C:/Users/sdelacruz/Documents/go/pkg/mod/github.com/ibmdb/go_ibm_db@v0.2.0/param.go:126 +0x17a8
github.com/ibmdb/go_ibm_db.(*ODBCStmt).Exec(0xc00225e410, 0xc000322640, 0x14, 0x14, 0x14, 0x14)
        C:/Users/sdelacruz/Documents/go/pkg/mod/github.com/ibmdb/go_ibm_db@v0.2.0/odbcstmt.go:107 +0x94c
github.com/ibmdb/go_ibm_db.(*Stmt).Exec(0xc000298000, 0xc000322640, 0x14, 0x14, 0x0, 0x0, 0x0, 0x0)
        C:/Users/sdelacruz/Documents/go/pkg/mod/github.com/ibmdb/go_ibm_db@v0.2.0/stmt.go:66 +0xf0
database/sql.ctxDriverStmtExec(0xee74c0, 0xc000020108, 0xee8100, 0xc000298000, 0xc000017500, 0x14, 0x14, 0x14, 0x14, 0x0, ...)
        c:/go/src/database/sql/ctxutil.go:77 +0x16c
database/sql.resultFromStatement(0xee74c0, 0xc000020108, 0xee55c0, 0xc00029d3e0, 0xc00002c040, 0xc000322140, 0x14, 0x14, 0x0, 0x0, ...)
        c:/go/src/database/sql/sql.go:2435 +0x15d
database/sql.(*Stmt).ExecContext(0xc000318000, 0xee74c0, 0xc000020108, 0xc000322140, 0x14, 0x14, 0x0, 0x0, 0x0, 0x0)
        c:/go/src/database/sql/sql.go:2411 +0x200
        c:/go/src/database/sql/sql.go:2423
...
exit status 2
@akhilravuri1
Copy link
Contributor

Hi @xhit
please provide a sample program to reproduce the error?
Is that column accepts null data?
Thanks,
Akhil

@xhit
Copy link
Contributor Author

xhit commented May 12, 2020

A BLOB column can accept NULL but with this driver trigger an error but that is another issue. See #80.

This issue is about insert an empty []byte and PR #74 fix this.

To reproduce:

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/ibmdb/go_ibm_db"
)

func main() {
	//connecto to DB
	db, err := connectDB2("localhost", "SAMPLE", "SCHEMATEST", "db2admin", "pass", 50000)
	if err != nil {
		panic(err)
	}

	defer db.Close()

	queryDrop := `drop table "issue73";`

	db.Exec(queryDrop)

	queryCreate := `CREATE TABLE "issue73" ("blob_column" BLOB(50));`

	_, err = db.Exec(queryCreate)
	if err != nil {
		panic(err)
	}

	defer db.Exec(queryDrop)

	queryInsert := `insert into "issue73" ("blob_column") values (?);‪`

	//inserting a empty []byte panic with error panic: runtime error: index out of range [0] with length 0
	_, err = db.Exec(queryInsert, []byte{})
	if err != nil {
		panic(err)
	}
}

//connect to db2 database
func connectDB2(host, database, schema, user, password string, port int) (*sql.DB, error) {

	//Password cannot contains ;
	connString := fmt.Sprintf("HOSTNAME=%s;DATABASE=%s;PORT=%d;UID=%s;PWD=%s", host, database, port, user, password)

	db, err := sql.Open("go_ibm_db", connString)

	if err != nil {
		return nil, err
	}

	err = db.Ping()

	if err != nil {
		return nil, err
	}

	db.SetMaxOpenConns(1)
	db.SetMaxIdleConns(1)

	//set the schema
	if len(schema) > 0 {
		_, err = db.Exec("SET CURRENT SCHEMA ?", schema)

		if err != nil {
			return nil, err
		}
	}

	return db, nil
}

@xhit
Copy link
Contributor Author

xhit commented May 23, 2020

Hi @akhilravuri1, can you check this panic?

Thanks.

@akhilravuri1
Copy link
Contributor

Hi @xhit
I have checked this panic. Your pull req also passes all the test cases.
We are planning to release a new version. I will add your fix in that version.

Thanks,
Akhil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants