Repository navigation
Conversation
📝 WalkthroughWalkthroughThe change adds a shared helper for safely constructing parameter-description queries. Both named-bind replacement paths use it. ChangesParameter-description query safety
Estimated code review effort: 2 (Simple) | ~10 minutes Mergeability Score: 🔵 Low · up to The change prevents named-bind buffer overruns, but psql can leak a small query buffer when host-variable validation rejects NULL or oversized input. The PR is mergeable with explicit owner awareness or a follow-up to release that buffer on validation failures. Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
It looks like they are doing the same thing. #1569 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/bin/psql/common.c`:
- Line 3646: In the surrounding SQL quoting function, compute the input length
once and validate it against (SIZE_MAX - 1) / 2 before calculating the
allocation size. Reject oversized input through the function’s existing error
path, then call pg_malloc0() with the safely computed doubled length plus
terminator so the subsequent copy remains within bounds.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 85675e9f-7d08-4b3d-b737-80764a64686f
📒 Files selected for processing (1)
src/bin/psql/common.c
get_hostvariables doubled the query length plus a NUL byte before calling pg_malloc0(). A wrapped size could slip through and the copy loop would then write past the allocated buffer. Reject inputs larger than (SIZE_MAX - 1) / 2 up front, matching the overflow guard already used in IvyBuildParameterDescriptionQuery. Assisted-by: opencode:qwen/qwen3.8-max-preview Signed-off-by: yyqdbngt <[email protected]>
|
@jiaoshuntian Thanks for the heads-up. #1569 was closed without merging, and its author also left it as is ("so let they do, it's fine"), so this PR remains the active fix for #1554. It covers both named-bind replacement paths in libpq via the shared IvyBuildParameterDescriptionQuery helper and the matching psql host-variable escape buffer, including the overflow guard now added in e4b5239. Please let me know if you'd like any part of #1569 folded in. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/bin/psql/common.c`:
- Around line 3643-3656: Release the PQExpBuffer allocated by createPQExpBuffer
before both early returns in the surrounding escape-query function: the sql ==
NULL validation path and the sql_len overflow path. Prefer moving
createPQExpBuffer below these checks when safe; otherwise call
destroyPQExpBuffer(query) before each return while preserving existing error
handling.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: fd519a7d-1197-419c-9eef-d420eed7c02d
📒 Files selected for processing (1)
src/bin/psql/common.c
| if (!sql) | ||
| return NULL; | ||
|
|
||
| /* | ||
| * Each input byte may be emitted twice (quoted quotes) plus one NUL | ||
| * terminator, so reject inputs whose doubled length would overflow | ||
| * before pg_malloc0() sees a wrapped size. | ||
| */ | ||
| sql_len = strlen(sql); | ||
| if (sql_len > (SIZE_MAX - 1) / 2) | ||
| { | ||
| pg_log_error("query is too long to escape for host-variable binding"); | ||
| *error = true; | ||
| return NULL; |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Release query on validation failures.
query is allocated at Line 3635 before these checks. The sql == NULL return at Lines 3643-3644 and the overflow return at Lines 3653-3656 bypass destroyPQExpBuffer(query). Move createPQExpBuffer() below both checks, or destroy query before each return.
Proposed fix
-PQExpBuffer query = createPQExpBuffer();
+PQExpBuffer query;
...
if (sql_len > (SIZE_MAX - 1) / 2)
{
pg_log_error("query is too long to escape for host-variable binding");
*error = true;
return NULL;
}
+query = createPQExpBuffer();
newsql = pg_malloc0(sql_len * 2 + 1);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if (!sql) | |
| return NULL; | |
| /* | |
| * Each input byte may be emitted twice (quoted quotes) plus one NUL | |
| * terminator, so reject inputs whose doubled length would overflow | |
| * before pg_malloc0() sees a wrapped size. | |
| */ | |
| sql_len = strlen(sql); | |
| if (sql_len > (SIZE_MAX - 1) / 2) | |
| { | |
| pg_log_error("query is too long to escape for host-variable binding"); | |
| *error = true; | |
| return NULL; | |
| PQExpBuffer query; | |
| if (!sql) | |
| return NULL; | |
| /* | |
| * Each input byte may be emitted twice (quoted quotes) plus one NUL | |
| * terminator, so reject inputs whose doubled length would overflow | |
| * before pg_malloc0() sees a wrapped size. | |
| */ | |
| sql_len = strlen(sql); | |
| if (sql_len > (SIZE_MAX - 1) / 2) | |
| { | |
| pg_log_error("query is too long to escape for host-variable binding"); | |
| *error = true; | |
| return NULL; | |
| } | |
| query = createPQExpBuffer(); | |
| newsql = pg_malloc0(sql_len * 2 + 1); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/bin/psql/common.c` around lines 3643 - 3656, Release the PQExpBuffer
allocated by createPQExpBuffer before both early returns in the surrounding
escape-query function: the sql == NULL validation path and the sql_len overflow
path. Prefer moving createPQExpBuffer below these checks when safe; otherwise
call destroyPQExpBuffer(query) before each return while preserving existing
error handling.
Summary
get_hostvariables()Root cause
The two libpq named-bind execution paths and psql's host-variable scanner allocated only
2 * query_lengthbytes for escaped SQL text. An all-quote input can fill that allocation before the terminating NUL. The libpq path also needed an explicit overflow check and safe handling of empty input.Verification
src/interfaces/libpq/ivy-exec.csrc/bin/psql/common.cFixes #1554.
Summary by CodeRabbit