sijms / go-ora

Pure go oracle client
MIT License
806 stars 175 forks source link

Panic on nil value insert #356

Closed martin-viggiano closed 1 year ago

martin-viggiano commented 1 year ago

I am working on a project that uses the sql.NullInt64 type mapped to a number column that can be null. When trying to insert any record where the Valid property of the sql.NullInt64 type variable is false (aka, the value is null), a panic happens.

I am using:

Panic details ``` panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x7566c8] goroutine 1 [running]: github.com/sijms/go-ora/v2.(*Stmt)._exec(0xc0000a6e60, {0xc0001d62d0, 0x1, 0x0?}) /home/martin/go/pkg/mod/github.com/sijms/go-ora/v2@v2.7.2/command.go:2176 +0x4e8 github.com/sijms/go-ora/v2.(*Stmt).ExecContext(0xc0000a6e60, {0x9e9f08?, 0xc00002a0e0}, {0xc0001d62d0, 0x1, 0x1}) /home/martin/go/pkg/mod/github.com/sijms/go-ora/v2@v2.7.2/command.go:1622 +0x265 github.com/sijms/go-ora/v2.(*Connection).ExecContext(0xc0001eb6a8?, {0x9e9f08, 0xc00002a0e0}, {0xc0002429c0?, 0x507960?}, {0xc0001d62d0, 0x1, 0x1}) /home/martin/go/pkg/mod/github.com/sijms/go-ora/v2@v2.7.2/connection.go:1094 +0xd0 database/sql.ctxDriverExec({0x9e9f08?, 0xc00002a0e0?}, {0x7f30306c3c38?, 0xc0000b5680?}, {0x0?, 0x0?}, {0xc0002429c0?, 0x88338a?}, {0xc0001d62d0, 0x1, ...}) /usr/local/go/src/database/sql/ctxutil.go:31 +0xdd database/sql.(*DB).execDC.func2() /usr/local/go/src/database/sql/sql.go:1675 +0x175 database/sql.withLock({0x9e7e70, 0xc0000dc2d0}, 0xc0001eb950) /usr/local/go/src/database/sql/sql.go:3405 +0x8c database/sql.(*DB).execDC(0xc0000d4500?, {0x9e9f08, 0xc00002a0e0}, 0xc0000dc2d0, 0x78?, {0xc0002429c0, 0x40}, {0xc000025c60, 0x1, 0x1}) /usr/local/go/src/database/sql/sql.go:1670 +0x266 database/sql.(*Tx).ExecContext(0xc0000d4480, {0x9e9f08, 0xc00002a0e0}, {0xc0002429c0, 0x40}, {0xc000025c60, 0x1, 0x1}) /usr/local/go/src/database/sql/sql.go:2471 +0xb7 xorm.io/xorm/core.(*Tx).ExecContext(0xc00007c6e0, {0x9e9f08?, 0xc00002a0e0}, {0xc0002429c0, 0x40}, {0xc000025c60, 0x1, 0x1}) /home/martin/Documents/xorm/core/tx.go:137 +0x1fc xorm.io/xorm.(*Session).exec(0xc0000a3c70, {0xc0002429c0, 0x40}, {0xc000025c60, 0x1, 0x1}) /home/martin/Documents/xorm/session_raw.go:167 +0x3b7 xorm.io/xorm.(*Session).insertStruct(0xc0000a3c70, {0x8eace0, 0xc000028348}) /home/martin/Documents/xorm/session_insert.go:333 +0x485 xorm.io/xorm.(*Session).Insert(0xc0000a3c70, {0xc0001ebf60?, 0x1, 0x0?}) /home/martin/Documents/xorm/session_insert.go:56 +0x33e xorm.io/xorm.(*Engine).Insert(0x8eace0?, {0xc0000bdf60, 0x1, 0x1}) /home/martin/Documents/xorm/engine.go:1217 +0x8d main.main() /home/martin/Documents/test/oracle.go:40 +0x105 exit status 2 ```
POC that reproduces the error ``` package main import ( "database/sql" "log" go_ora "github.com/sijms/go-ora/v2" "xorm.io/xorm" ) /* SQL: CREATE SEQUENCE SEQ_TEST MINVALUE 1 NOMAXVALUE START WITH 1 INCREMENT BY 1 NOCYCLE NOCACHE; CREATE TABLE "test" ( "id" NUMBER(19, 0) DEFAULT SEQ_TEST.nextval NOT NULL, "null_id" NUMBER(19, 0) NULL ); */ type myStruct struct { ID int64 `xorm:"pk autoincr <- 'id'"` NullableID sql.NullInt64 `xorm:"'null_id'"` } func (myStruct) TableName() string { return "test" } func main() { dsn := go_ora.BuildUrl("localhost", 1521, "XEPDB1", "root", "root", nil) engine, err := xorm.NewEngine("oracle", dsn) if err != nil { log.Fatal(err) } test := myStruct{ ID: 1, } _, err = engine.Insert(test) if err != nil { log.Fatal(err) } } ```
sijms commented 1 year ago

fixed i will add a new release with fix the result:

2023/05/04 19:01:35 ORA-00942: table or view does not exist
sijms commented 1 year ago

fixed in v2.7.4