From e7ff7dcfb0e839ac934bef8cfe0b564ccb48db39 Mon Sep 17 00:00:00 2001 From: Esteban82 Date: Tue, 27 Oct 2026 07:33:07 -0300 Subject: [PATCH 1/2] Fix legend D without offset and G without argument - D: "D pen flag" (offset left out) was read as offset=pen, pen=flag when estimating the legend height, but as pen+flag when drawing, so the automatic height differed from "D 0 pen flag". Read it the same way. - G: a G record without argument read past the end of the record, giving garbage warnings and exit code 0. Report a clear error instead. Co-Authored-By: Claude Sonnet 5.5 --- src/pslegend.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pslegend.c b/src/pslegend.c index 80ccaca81a3..8fa5049768b 100644 --- a/src/pslegend.c +++ b/src/pslegend.c @@ -717,14 +717,18 @@ EXTERN_MSC int GMT_pslegend (void *V_API, int mode, void *args) { case 'D': /* Delimiter record: D offset pen [-|=|+] */ txt_c[0] = '\0'; - sscanf (&line[2], "%s %s %s", txt_a, txt_b, txt_c); + n = sscanf (&line[2], "%s %s %s", txt_a, txt_b, txt_c); + if (n == 2 && strchr ("-=+", txt_b[0])) strcpy (txt_c, txt_b); /* Offset was skipped: D pen flag */ if (!(txt_c[0] == '-' || txt_c[0] == '=')) height += quarter_line_spacing; if (!(txt_c[0] == '+' || txt_c[0] == '=')) height += quarter_line_spacing; column_number = 0; break; case 'G': /* Gap record */ - sscanf (&line[2], "%s", txt_a); + if (strlen (line) < 3 || sscanf (&line[2], "%s", txt_a) != 1) { + GMT_Report (API, GMT_MSG_ERROR, "Not enough arguments given to G operator\n"); + Return (GMT_RUNTIME_ERROR); + } height += (txt_a[strlen(txt_a)-1] == 'l') ? atof (txt_a) * one_line_spacing : gmt_M_to_inch (GMT, txt_a); column_number = 0; break; @@ -1239,7 +1243,10 @@ EXTERN_MSC int GMT_pslegend (void *V_API, int mode, void *args) { break; case 'G': /* Gap record: G gap (will be filled with current fill[0] setting if active) */ - sscanf (&line[2], "%s", txt_a); + if (strlen (line) < 3 || sscanf (&line[2], "%s", txt_a) != 1) { + GMT_Report (API, GMT_MSG_ERROR, "Not enough arguments given to G operator\n"); + Return (GMT_RUNTIME_ERROR); + } row_height = (txt_a[strlen(txt_a)-1] == 'l') ? atof (txt_a) * one_line_spacing : gmt_M_to_inch (GMT, txt_a); pslegend_fillcell (GMT, Ctrl->D.refpoint->x, row_base_y-row_height, row_base_y+gap, x_off_col, &d_line_after_gap, n_columns, fill); row_base_y -= row_height; From 6abb63958f088b0f8b12140c47028e57177417d8 Mon Sep 17 00:00:00 2001 From: Esteban82 Date: Wed, 28 Oct 2026 13:40:07 -0300 Subject: [PATCH 2/2] Update ex41 baseline for the legend D fix --- doc/examples/images/ex41.ps.dvc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/examples/images/ex41.ps.dvc b/doc/examples/images/ex41.ps.dvc index 3a084a84317..a728045c9e4 100644 --- a/doc/examples/images/ex41.ps.dvc +++ b/doc/examples/images/ex41.ps.dvc @@ -1,5 +1,5 @@ outs: -- md5: 57dae2c4622e72b2b4b3c64088cfa7d5 - size: 1774717 +- md5: bf5f7933c8807702d4ba387b34ab261f + size: 1753972 hash: md5 path: ex41.ps