grdimage: Fix uninitialized inc[] read with variable transparency - #9185
Merged
Conversation
For an image with variable transparency, GMT_grdimage does "goto tr_image", which jumps into the middle of the "if (need_to_project)" block and therefore skips the initializers of the variables declared there. nx_proj/ny_proj are reassigned before use, but inc[] is not, so gmt_project_init() branched on leftover stack bytes: when those happened to look like two positive doubles it derived the projected dimensions from inc[] instead of nx_proj/ny_proj and collapsed the image to 1x1 pixels, drawing a blank or degenerate panel. Since the outcome depended on stack contents, it failed on roughly half the runs, which is what made test/grdimage/image_vartrans.sh flaky. It affects any RGBA image with a variable alpha channel, with or without -Q. Declaring the variables at function scope keeps their initializers on every path into the block. Found with valgrind, which reports the read on every run - only its consequence is random. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
seisman
reviewed
Sep 8, 2026
Member
Author
|
I simplified the comment. |
seisman
approved these changes
Sep 8, 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.
test/grdimage/image_vartrans.shhas been failing intermittently, on roughly half the runs. I have seen this myself running the same script on different occasions: sometimes panels 3 and 4 come out fine, and sometimes they are blank.According to Claude the cause is an uninitialized variable. For images with a variable alpha channel,
GMT_grdimagedoesgoto tr_image, which jumps into theif (need_to_project)block past the declaration ofdouble inc[2] = {0.0, 0.0}, so the initializer never runs.gmt_project_init()then branches on that leftover stack value and, when it happens to look positive, projects the image to 1x1 pixels — a blank panel, with no warning. Hence the coin-flip behavior.This PR adds the fix proposed by Claude Opus 5.