Skip to content

sqlc generate runs into nil pointer dereference #176

Closed
@dominikbraun

Description

@dominikbraun

My db.sql:

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

-- ...

My sqlc.json:

{
  "version": "1",
  "packages": [
    {
      "name": "db",
      "emit_json_tags": true,
      "emit_prepared_queries": false,
      "path": "db",
      "queries": "./db.sql",
      "schema": "./db.sql"
    }
  ]
}

Running sqlc generate causes a panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x88 pc=0x6b9249]

goroutine 1 [running]:
github.com/kyleconroy/sqlc/internal/dinosql.ParseQueries(0xc00012ffb0, 0xe038f1, 0x1, 0xc000116300, 0x1, 0x4, 0x0, 0x0, 0x0, 0x0, ...)
        /home/runner/work/sqlc/sqlc/internal/dinosql/parser.go:226 +0x689
github.com/kyleconroy/sqlc/internal/cmd.glob..func4(0xdfaea0, 0xe2d4d8, 0x0, 0x0)
        /home/runner/work/sqlc/sqlc/internal/cmd/cmd.go:156 +0x9aa
github.com/spf13/cobra.(*Command).execute(0xdfaea0, 0xe2d4d8, 0x0, 0x0, 0xdfaea0, 0xe2d4d8)
...

The eror occurs here - parser.go:221:

query, err := parseQuery(c, stmt, source)
if err != nil {
	merr.Add(filename, source, location(stmt), err)
	continue
}
query.Filename = filepath.Base(filename)

The code assumes that after checking for err != nil it is safe to access query. This is not correct, since parseQuery may also return nil for both the query and the error - parser.go:384:

default:
	return nil, nil

The other cases do not match because the statement seems to be an instance of pg_query.CreateStmt.

I'm not sure if the actual problem is that the statement's type does not quite match, or that there is no error indicating that the statement has an 'unexpected' type, or even that the returned query is nil.
In any case, the nil pointer dereference should be fixed IMO.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions