houqp / sqlvet

Go fearless SQL. Sqlvet performs static analysis on raw SQL queries in your Go code base.
MIT License
493 stars 25 forks source link

Does not work without specifying schema #39

Open oschwald opened 6 months ago

oschwald commented 6 months ago

When using the binary from the release page or building master from the source, the command fails when a schema has not been specified:

[!] No schema specified, will run without table and column validation.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x870c29]

goroutine 1 [running]:
main.(*SQLVet).Vet(0xc000150ee0)
    /home/runner/work/sqlvet/sqlvet/main.go:43 +0x29
main.main.func2(0xc000189700?, {0xc00011ebd0?, 0x7?, 0xb7435e?})
    /home/runner/work/sqlvet/sqlvet/main.go:147 +0x45
github.com/spf13/cobra.(*Command).execute(0xc00016d808, {0xc000132010, 0x1, 0x1})
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 +0x867
github.com/spf13/cobra.(*Command).ExecuteC(0xc00016d808)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x3a5
github.com/spf13/cobra.(*Command).Execute(...)
    /home/runner/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968
main.main()
    /home/runner/work/sqlvet/sqlvet/main.go:166 +0x1d6

It appears that s.Schema is nil in vet.NewContext(tables: s.Schema.Tables).

aleksey-korolev commented 5 months ago

Reproduces for me too with sqlvet@v1.1.8

houqp commented 5 months ago

could you try adding a nil check in there and pass in an empty map when schema is nil?