Skip to content

libpq: prevent named-bind escape buffer overrun - #1567

Closed
btlqql wants to merge 1 commit into
IvorySQL:masterfrom
btlqql:wangyue/fix-1554-named-bind-escape
Closed

btlqql wants to merge 1 commit into
IvorySQL:masterfrom
btlqql:wangyue/fix-1554-named-bind-escape

Conversation

@btlqql

@btlqql btlqql commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • centralize construction of the named-bind parameter-description query
  • reserve space for worst-case quote doubling and the terminating NUL
  • reject size overflow and handle empty queries safely

Root cause

Both named-bind execution paths allocated only 2 * query_length bytes for the escaped SQL text. An all-quote input filled that allocation before the NUL terminator, and the second path could write through a zero-length allocation for an empty query.

Testing

  • targeted compilation of src/interfaces/libpq/ivy-exec.c
  • empty, quoted, and all-quote boundary cases under AddressSanitizer

Fixes #1554

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when executing queries with named parameters.
    • Correctly handles parameter names containing single quotes.
    • Added safeguards against allocation-size overflow during query preparation.
    • Preserved clear error reporting when query preparation cannot complete.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5ca455cb-31d6-43a6-9136-7272adbb3143

📥 Commits

Reviewing files that changed from the base of the PR and between b21cd66 and 615ab89.

📒 Files selected for processing (1)
  • src/interfaces/libpq/ivy-exec.c

📝 Walkthrough

Walkthrough

The change adds a shared helper for escaped parameter-description queries. Both name replacement paths use the helper. The helper checks allocation-size overflow and handles allocation failure.

Changes

Parameter query construction

Layer / File(s) Summary
Escaped query builder
src/interfaces/libpq/ivy-exec.c
Adds IvyBuildParameterDescriptionQuery and its private declaration. The helper escapes single quotes, checks allocation-size overflow, and returns NULL on allocation failure.
Replacement path integration
src/interfaces/libpq/ivy-exec.c
Updates Ivyreplacenamebindtoposition and Ivyreplacenamebindtoposition2 to use the shared helper and removes duplicated query construction logic.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • IvorySQL/IvorySQL#1556: Both changes modify parameter-name replacement helpers in ivy-exec.c, but this PR changes query construction while that PR addresses Ivyresult cleanup.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the named-bind escape buffer overrun fix addressed by the pull request.
Linked Issues check ✅ Passed The helper fixes escaping capacity, NUL termination, overflow checks, empty queries, and both affected paths required by issue #1554.
Out of Scope Changes check ✅ Passed The changes are limited to shared query construction and allocation safety in the two affected named-bind execution paths.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@btlqql btlqql closed this Aug 11, 2026
@btlqql
btlqql deleted the wangyue/fix-1554-named-bind-escape branch August 11, 2026 16:01
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.

Named-bind query escaping can overrun its heap buffer

1 participant