Skip to content

fix(golang): escape generated enum values (fix #4448)#4449

Open
sjh9714 wants to merge 1 commit into
sqlc-dev:mainfrom
sjh9714:fix-go-enum-value-escaping
Open

fix(golang): escape generated enum values (fix #4448)#4449
sjh9714 wants to merge 1 commit into
sqlc-dev:mainfrom
sjh9714:fix-go-enum-value-escaping

Conversation

@sjh9714
Copy link
Copy Markdown

@sjh9714 sjh9714 commented May 20, 2026

Summary

Fixes PostgreSQL enum values containing Go string metacharacters being emitted into generated Go constants without string literal escaping.

What changed

  • Updated the Go enum constant template to render enum values with Go string literal escaping (printf "%q").
  • Added an end-to-end PostgreSQL stdlib fixture covering a backslash-n enum value and a double-quote/code-like enum value.

Why

The previous template wrapped raw enum values in quotes, so user\nadmin became a newline at runtime and values containing " could produce invalid generated Go code. The new template delegates literal formatting to Go's %q formatting.

Testing

  • Red check: go test ./internal/endtoend -run 'TestReplay/base/enum_escaped_values/postgresql/stdlib' failed before the template change with a generated models.go diff missing escapes.
  • go test ./internal/endtoend -run 'TestReplay/base/enum_escaped_values/postgresql/stdlib'
  • go test ./internal/codegen/golang
  • gofmt -l internal/endtoend/testdata/enum_escaped_values/postgresql/stdlib/go/db.go internal/endtoend/testdata/enum_escaped_values/postgresql/stdlib/go/models.go internal/endtoend/testdata/enum_escaped_values/postgresql/stdlib/go/query.sql.go
  • go test ./...
  • go test --tags=examples -timeout 20m ./...

Fixes #4448

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Silent Data Corruption via Unescaped Enum Values in Generated Go Code

1 participant