Skip to content

grdimage: Fix uninitialized inc[] read with variable transparency - #9185

Merged
Esteban82 merged 2 commits into
masterfrom
fix-grdimage-variable-transparency-uninit
Sep 8, 2026
Merged

grdimage: Fix uninitialized inc[] read with variable transparency#9185
Esteban82 merged 2 commits into
masterfrom
fix-grdimage-variable-transparency-uninit

Conversation

@Esteban82

Copy link
Copy Markdown
Member

test/grdimage/image_vartrans.sh has 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_grdimage does goto tr_image, which jumps into the if (need_to_project) block past the declaration of double 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.

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>
@Esteban82 Esteban82 added add-changelog Add PR to the changelog AI-assisted All (or most) of the code was written by Artificial Intelligence. labels Sep 7, 2026
Comment thread src/grdimage.c Outdated
@Esteban82

Copy link
Copy Markdown
Member Author

I simplified the comment.

@Esteban82
Esteban82 merged commit 0124717 into master Sep 8, 2026
10 of 13 checks passed
@Esteban82
Esteban82 deleted the fix-grdimage-variable-transparency-uninit branch September 8, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

add-changelog Add PR to the changelog AI-assisted All (or most) of the code was written by Artificial Intelligence.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants