Fix use-after-free bug in postgres example (14-02.zig)#127
Draft
jiacai2050 with Copilot wants to merge 2 commits into
Draft
Fix use-after-free bug in postgres example (14-02.zig)#127jiacai2050 with Copilot wants to merge 2 commits into
jiacai2050 with Copilot wants to merge 2 commits into
Conversation
The `color_id` slice pointed into PGresult memory that was freed by `defer c.PQclear(res)` when the `blk:` block exited via `break`. On macOS the freed memory is zeroed, causing an empty string to be passed as `color_id` to the next PQexecPrepared call, which then failed with "invalid input syntax for type integer: ''". Fix: remove the blk: scope and defer PQclear(color_res) at the outer inline for loop level, so color_id stays valid for the inner loop that uses it.
Copilot
AI
changed the title
[WIP] Fix failing GitHub Actions job examples (macos-latest)
Fix use-after-free bug in postgres example (14-02.zig)
Jul 16, 2026
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
examples (macos-latest)CI job was failing inrun exe examples-14-02withERROR: invalid input syntax for type integer: ""becausecolor_idheld a dangling pointer into a freedPGresultbuffer.Root cause
color_idwas extracted viastd.mem.span(c.PQgetvalue(res, 0, 0))inside ablk:block that also haddefer c.PQclear(res). The defer fires when the block exits — including onbreak :blk— so by the timecolor_idwas used in the inner loop, thePGresultbuffer it pointed into was already freed. On macOS, freed memory is zeroed, socolor_idread as"", and PostgreSQL rejected it as an invalid integer.Fix
Remove the
blk:scope. ExecutePQexecPreparedforinsert_cat_colorsdirectly in the outerinline forbody and deferPQclear(color_res)there, socolor_idstays valid for the full duration of the inner loop.