Skip to content

gmtgravmag3d: Fix heap out-of-bounds read in read_xyz - #9308

Merged
Esteban82 merged 1 commit into
masterfrom
gravmag3d-oob-read
Nov 18, 2026
Merged

Esteban82 merged 1 commit into
masterfrom
gravmag3d-oob-read

Conversation

@Esteban82

Copy link
Copy Markdown
Member

I asked Claude to analyze why the test fc_okb.sh failed on macOS.

Cause and fix

Claude proposes this fix.

In read_xyz, the loop that finds the latitude range of geographic input reads Ctrl->triang[n].y instead of Ctrl->triang[k].y. triang has just been reallocated to exactly n elements, so this reads 8 bytes past the end of the array (bug from 8dab545, 2020).

Usually that stray value is harmless and the result is correct. When it happens to be larger than ±FLT_MAX, lat_0 becomes huge, every y coordinate collapses to the same value, the triangles become degenerate and the anomaly is exactly 0.

Test

I tested it running gmtgravmag3d from the test under valgrind:

gmt grd2xyz @earth_relief_10m -R-41:50/-41:20/47:30/47:50 -fg > fc.dat
gmt triangulate fc.dat > fc_tri.dat
valgrind gmt gmtgravmag3d -C1700 -Gfc_okb.nc -R-41:50/-41:20/47:30/47:50 -I1m -Z-4300 -fg fc.dat -Tvfc_tri.dat

On master I get this output:

==961017== Invalid read of size 8
==961017==    at 0x568048C2: read_xyz.part.0 (in /usr/local/lib/gmt/plugins/supplements.so)
==961017==    by 0x568097EE: GMT_gmtgravmag3d (in /usr/local/lib/gmt/plugins/supplements.so)
==961017==    by 0x48CDE9C: GMT_Call_Module (in /usr/local/lib/libgmt.so.6.8.0)
==961017==    by 0x400193C: main (in /usr/local/bin/gmt)
==961017==  Address 0x56af08d8 is 8 bytes after a block of size 288 alloc'd
==961017==    at 0x485A07F: realloc (vg_replace_malloc.c:1804)
==961017==    by 0x494D9A9: gmt_memory_func (in /usr/local/lib/libgmt.so.6.8.0)
==961017==    by 0x56804823: read_xyz.part.0 (in /usr/local/lib/gmt/plugins/supplements.so)
==961017==    by 0x568097EE: GMT_gmtgravmag3d (in /usr/local/lib/gmt/plugins/supplements.so)
==961017==    by 0x48CDE9C: GMT_Call_Module (in /usr/local/lib/libgmt.so.6.8.0)
==961017==    by 0x400193C: main (in /usr/local/bin/gmt)
...
==961017== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

And this with the fix:

==963252== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Generated using Claude Opus 5.5.

The loop that computes the central latitude of geographic -T input read
Ctrl->triang[n].y (one past the end of the array, which was just
reallocated to n elements) instead of Ctrl->triang[k].y. When the
garbage value exceeded +/-FLT_MAX, lat_0 became huge, all y coordinates
collapsed to the same value and the computed anomaly was exactly zero.
This is why test/potential/fc_okb.sh failed intermittently on macOS CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Esteban82 Esteban82 added the bug Something isn't working label Nov 18, 2026
@Esteban82
Esteban82 requested review from a team and joa-quim November 18, 2026 15:15
@Esteban82 Esteban82 added the bug Something isn't working label Nov 18, 2026
@Esteban82
Esteban82 merged commit 7552e3c into master Nov 18, 2026
18 of 20 checks passed
@Esteban82
Esteban82 deleted the gravmag3d-oob-read branch November 18, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants