From bde5617e184be032586d8cf87d8a91a9608169d7 Mon Sep 17 00:00:00 2001 From: Esteban82 Date: Wed, 18 Nov 2026 11:31:49 -0300 Subject: [PATCH] gmtgravmag3d: Fix heap out-of-bounds read in read_xyz 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 --- src/potential/gmtgravmag3d.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/potential/gmtgravmag3d.c b/src/potential/gmtgravmag3d.c index 5c20e1ef8c3..23cc06e661c 100644 --- a/src/potential/gmtgravmag3d.c +++ b/src/potential/gmtgravmag3d.c @@ -622,7 +622,7 @@ GMT_LOCAL int read_xyz(struct GMT_CTRL *GMT, struct GMTGRAVMAG3D_CTRL *Ctrl, str /* compute the central lat lon For y_min/max we reverse MIN/MAX because we already have multiplied y by -1 above. */ for (k = 0; k < n; k++) { x_min = MIN(Ctrl->triang[k].x, x_min); x_max = MAX(Ctrl->triang[k].x, x_max); - y_min = MAX(Ctrl->triang[n].y, y_min); y_max = MIN(Ctrl->triang[n].y, y_max); + y_min = MAX(Ctrl->triang[k].y, y_min); y_max = MIN(Ctrl->triang[k].y, y_max); } *lon_0 = (x_min + x_max) / 2; *lat_0 = (y_min + y_max) / 2;