crawshaw / sqlite

Go SQLite3 driver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sqlite misuse on interrupted connections

tv42 opened this issue · comments

commented

Step guards itself against Stmt.stmt == nil, but the other methods don't:

package main

import (
	"fmt"
	"log"

	"crawshaw.io/sqlite"
)

func run() error {
	sqlite.Logger = func(code sqlite.ErrorCode, msg []byte) {
		log.Printf("sqlite: %v %s", code, msg)
	}

	conn, err := sqlite.OpenConn(":memory:", 0)
	if err != nil {
		return fmt.Errorf("sqlite open: %w", err)
	}
	defer conn.Close()

	interrupt := make(chan struct{})
	// interrupt immediately
	close(interrupt)
	conn.SetInterrupt(interrupt)

	stmt := conn.Prep(`select 1`)
	defer stmt.Finalize()
	stmt.BindText(1, "kaboom")
	return nil
}

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}
2020/02/10 11:24:29 sqlite: SQLITE_MISUSE API called with NULL prepared statement
2020/02/10 11:24:29 sqlite: SQLITE_MISUSE misuse at line 83847 of [3bfa9cc97d]

Having this library trigger misuses means I can't use sqlite debugging to notice/diagnose my own bugs.

Thanks for the bug report. I agree this is a flaw in the Stmt API.

It looks like the thing to do is to add a check in all the bind statements to avoid the MISUSE.