Skip to content

Named-bind query escaping can overrun its heap buffer #1554

Closed

Description

@Aias00

Bug Report

IvorySQL Version

master at b21cd664004bc4383edfb7944712599818d635af

Current Behavior

The two get_parameter_description() query-building paths in src/interfaces/libpq/ivy-exec.c allocate 2 * query_length bytes for an escaped query, duplicate every single quote, and then append a NUL terminator.

For an input consisting entirely of single quotes, the escaped bytes consume the whole allocation and the terminator is written one byte past the heap buffer. The Ivyreplacenamebindtoposition2() path also has no empty-query guard, so it can write a terminator through a zero-length allocation.

Affected code:

  • Ivyreplacenamebindtoposition() around lines 3791-3811
  • Ivyreplacenamebindtoposition2() around lines 4078-4099

Expected behavior/code

The escaped-query buffer must include space for the terminating NUL and reject allocation-size overflow. Empty input must also produce a valid one-byte empty string buffer.

Step to reproduce

Build or run the affected path with AddressSanitizer and prepare a query whose bytes are all single quotes, for example ''.

The current size calculation allocates four bytes, writes four escaped quote bytes, and then writes a fifth NUL byte.

Additional context that can be helpful for identifying the problem

Both functions contain the same manual escaping loop, so both entry points require the same boundary correction. This issue is limited to the allocation boundary; changing the escaping or query semantics is not necessary.

Activity

  1. jiaoshuntian commented on Aug 12, 2026

    @jiaoshuntian
    Collaborator

    Does the same issue also exist in get_hostvariables() in src/bin/psql/common.c?

  2. Aias00 commented on Aug 12, 2026

    @Aias00
    ContributorAuthor

    Does the same issue also exist in get_hostvariables() in src/bin/psql/common.c?

    Yes, the same issue exists in get_hostvariables() in src/bin/psql/common.c.

  3. added a commit that references this issue on Aug 14, 2026
    4fe229c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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