Skip to content

Is this a bug in the database/sql layer? Panic on Exec of empty string #963

Closed
@otoolep

Description

@otoolep

Passing an empty to a string causes a panic in the database/sql layer. Is there some initialization I'm missing? I see nothing in the docs that state this should panic.

~/repos/go-empty-sql-statement/src/github/com/otoolep/go-empty-sql-statement (master)$ cat main.go         
package main                                                                                                                                         
                                                                                                                                                     
import (                                                                                                                                             
        "database/sql"                                                                                                                               
        "fmt"                                                                                                                                        
        "log"                                                                                                                                        
        "os"                                                                                                                                         
                                                                                                                                                     
        _ "github.com/mattn/go-sqlite3"                                                                                                              
)                                                                                                                                                    
                                                                                                                                                     
func main() {                                                                                                                                        
        os.Remove("foo.bar")                                                                                                                         
                                                                                                                                                     
        db, err := sql.Open("sqlite3", "foo.bar")                                                                                                    
        if err != nil {                                                                                                                              
                log.Fatalf("failed to open database: %s", err.Error())                                                                               
        }                                                                                                                                            
                                                                                                                                                     
        results, err := db.Exec("")
        if err != nil {
                log.Fatalf("failed to get results: %s", err.Error())
        }
        li, err := results.LastInsertId()
        fmt.Println(li, err)
}
~/repos/go-empty-sql-statement/src/github/com/otoolep/go-empty-sql-statement (master)$ go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x4fa135]

goroutine 1 [running]:
database/sql.driverResult.LastInsertId(0x6513a0, 0xc000144000, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/philip/.gvm/gos/go1.15/src/database/sql/sql.go:3266 +0x75
main.main()
        /home/philip/repos/go-empty-sql-statement/src/github/com/otoolep/go-empty-sql-statement/main.go:24 +0xfd
exit status 2
~/repos/go-empty-sql-statement/src/github/com/otoolep/go-empty-sql-statement (master)$ 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions