fix: hoist intToString() temporaries to avoid dangling strValue pointers - #1133
Merged
Conversation
intToString() returns an IntString struct by value. Taking .strValue from a compound literal like intToString(x).strValue produces a pointer into a struct that is destroyed at the end of the statement. Any use of that pointer after the semicolon is undefined behaviour; when the value is stored in a paramValues[] array and read by a subsequent pgsql_execute_with_params() call, it reliably reads freed stack memory. Fix: declare a named IntString variable at function scope so that .strValue remains valid across the entire parameter-building and query-execution block. Files and instance counts: monitor.c 20 instances across 13 functions coordinator.c 10 instances across 5 functions demoapp.c 8 instances across 3 functions pgctl.c 1 instance (pg_ctl_postgres args array) pgsql.c 1 instance (send_node_active_message loop) ipaddr.c 1 instance (fetchLocalIPAddress GetAddrInfo call) Clang -Wdangling-assignment is clean after this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
intToString()returns anIntStringstruct by value. Taking.strValuefrom the result inline:stores a pointer into a temporary struct that is destroyed at the end of the statement. Any subsequent read of
paramValues[1]— including thepgsql_execute_with_params()call a few lines later — reads freed stack memory. This is undefined behaviour; in practice it produces garbage bytes in libpq parameter arrays, which propagate into PostgreSQL error messages and can causeUnicodeDecodeErrorin any Python code that processes those messages.Clang catches this with
-Wdangling-assignment.The fix
Declare a named
IntStringvariable at function scope so that.strValueremains valid across the entire parameter-building and query-execution block:Files changed
src/bin/pg_autoctl/monitor.csrc/bin/pg_autoctl/coordinator.csrc/bin/pg_autoctl/demoapp.csrc/bin/pg_autoctl/pgctl.cpg_ctl_postgresargs arraysrc/bin/pg_autoctl/pgsql.csend_node_active_messageloopsrc/bin/pg_autoctl/ipaddr.cfetchLocalIPAddressGetAddrInfocall41 instances total. No logic changes; only the lifetime of the intermediate struct is extended.
citus_indent --checkpasses.-Wdangling-assignmentis clean after this change.