Skip to content

Avoid a panic in QuoteSQLIdent for empty input #406

Description

@apstndb

Problem

token.QuoteSQLIdent("") panics with an index-out-of-range error because needQuoteSQLIdent reads s[0] without first checking whether s is empty.

panic: runtime error: index out of range [0] with length 0

The same panic is reachable indirectly through an exported AST node:

_ = (&ast.Ident{}).SQL()

Reproduction

token.QuoteSQLIdent("")

https://go.dev/play/p/vniPESiuWGD

package main

import "github.com/cloudspannerecosystem/memefish/token"

func main() {
	_ = token.QuoteSQLIdent("")
}

(*ast.Ident).SQL()

https://go.dev/play/p/Jp6w9qpqLfE

package main

import "github.com/cloudspannerecosystem/memefish/ast"

func main() {
	_ = (&ast.Ident{Name: ""}).SQL()
}

Why change this

The function returns only a string and documents no non-empty precondition. Downstream code that constructs ast.Ident values from runtime strings must currently validate every identifier before serialization or recover at a broader boundary.

This follows the separation in google/googlesql: ParseIdentifier rejects an empty backquoted identifier, while ToIdentifierLiteral("") returns two backticks and explicitly notes that the result is not a valid identifier.

Expected behavior

QuoteSQLIdent("") returns two backticks instead of panicking. Identifier validation remains unchanged; empty identifiers are still rejected by the lexer/parser.

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