Skip to content

fix(schema): make the ? placeholder parser skip string literals and comments - #1400

Closed
namtzigla wants to merge 1 commit into
uptrace:masterfrom
namtzigla:fix/placeholder-parser-literal-comment-aware
Closed

namtzigla wants to merge 1 commit into
uptrace:masterfrom
namtzigla:fix/placeholder-parser-literal-comment-aware

Conversation

@namtzigla

Copy link
Copy Markdown
Contributor

What

Makes the query placeholder parser aware of string literals and comments, so a ?
inside a '...'/"..." literal or a -- / /* */ comment is no longer mistaken for a
bind placeholder.

Closes #1399.

Why

The formatter scanned for ? with a plain byte search (ReadSep('?')), so a ? in a
literal or comment consumed a positional argument and shifted every subsequent binding:

db.NewRaw(`... WHERE note='huh?' AND org_id=? AND secret=?`, 42, "s3cr3t")
// before: ... WHERE note='huh42' AND org_id='s3cr3t' AND secret=?
//   -> org_id gets the wrong value; secret=? dangles (invalid SQL)
// after:  ... WHERE note='huh?'  AND org_id=42        AND secret='s3cr3t'

Change

  • Add Parser.ReadUntilPlaceholder (internal/parser): a literal/comment-aware
    replacement for ReadSep('?'). It skips '...'/"..." quoted strings (with doubled-quote
    escaping), -- line comments, and nesting /* */ block comments.
  • schema/querygen.go: use ReadUntilPlaceholder() instead of ReadSep('?') (one line).

The scanner is deliberately conservative so it can never skip a genuine placeholder:
backslash escaping inside literals is not interpreted (matches
standard_conforming_strings=on, the default since PG 9.1), and $$...$$ / E'...' strings
degrade to the previous behavior rather than over-consuming. The existing \? escape and
named/positional argument handling are unchanged.

Tests

  • internal/parser: unit tests for ReadUntilPlaceholder (literals, doubled quotes,
    quoted identifiers, line/nested-block comments, placeholder-before-literal).
  • schema: formatter-level regression test asserting exact output; it fails on the old
    ReadSep behavior and passes with the fix. Full go test ./internal/... ./schema/... .
    passes; gofmt/go vet clean.

@namtzigla
namtzigla force-pushed the fix/placeholder-parser-literal-comment-aware branch from 1306eea to ab1e81c Compare July 22, 2026 03:42
…omments

The query formatter scanned for '?' with a plain byte search, so a '?' that
appears inside a string literal or a comment in the query template was mistaken
for a bind placeholder. That consumed a positional argument and silently shifted
every subsequent binding, e.g.

    NewRaw("... WHERE note='huh?' AND org_id=? AND secret=?", 42, "s3cr3t")
    => ... WHERE note='huh42' AND org_id='s3cr3t' AND secret=?

org_id receives the wrong value and the last placeholder dangles.

Add Parser.ReadUntilPlaceholder, a literal/comment-aware replacement for
ReadSep('?'): it skips '...'/"..." quoted strings (with doubled-quote escaping),
-- line comments, and nesting /* */ block comments. It is deliberately
conservative so it never skips a genuine placeholder: backslash escaping inside
literals is not interpreted (matches standard_conforming_strings=on) and
dollar-/E-strings degrade to the previous behaviour rather than over-consuming.
@namtzigla
namtzigla force-pushed the fix/placeholder-parser-literal-comment-aware branch from ab1e81c to 71b8288 Compare July 22, 2026 03:50
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this pr will be closed. Please feel free to give a status update now, ping for review, when it's ready. Thank you for your contributions!

@github-actions github-actions Bot added the stale label Aug 22, 2026
@github-actions github-actions Bot closed this Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Placeholder parser mistakes '?' inside string literals/comments for a bind placeholder → argument shift

1 participant