Skip to content

Fix crashes reading records with too few columns - #9231

Merged
Esteban82 merged 3 commits into
masterfrom
fix-read-table-empty-first-segment
Sep 28, 2026
Merged

Esteban82 merged 3 commits into
masterfrom
fix-read-table-empty-first-segment

Conversation

@Esteban82

Copy link
Copy Markdown
Member

gmtlib_read_table (src/gmt_io.c) skips records with fewer columns than requested, but crashes when they empty the first segment (an unsigned seg-- wraps), when that segment is then read as a polygon, and when a comment follows such a record. One commit per crash, each with a test.

Tested with:

printf '30 45\n120 60\n' | gmt convert -i0:2
printf '1 2\n> P1\n0 0 1\n10 0 1\n10 10 1\n' | gmt spatial -Fp -i0:2
printf '1 2 3\n4 5\n# c\n6 7 8\n' | gmt convert

Assisted-by: Claude Opus 5.5 (max effort); tests first drafted with Claude Sonnet 5 and reviewed with Opus.

🤖 Generated with Claude Code

Esteban82 and others added 3 commits September 27, 2026 11:56
gmtlib_read_table deletes a segment whose records were all skipped for
having too few columns, and steps back with seg--. When that is the
first segment, seg (a uint64_t) wraps to UINT64_MAX and T->segment[seg]
is read out of bounds. This happens when every record of a headerless
first segment, possibly the whole file, is too short; a file that
starts with a segment header never gets here. Start over from the
first segment instead, so the table reads as if those records were
not there.

Tested with:
  printf '30 45\n120 60\n' | gmt convert -i0:2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Polygon closure and the one-time line/polygon detection ran before the
empty segment was deleted. On an emptied first segment the closure
dereferenced coordinate arrays that were not allocated yet, so gmt
spatial -Fp segfaulted; with geographic data and arrays left over from
a previous file it read x[row-1] out of bounds and kept a phantom
one-point segment of stale data. The detection was also spent on the
empty segment, so the polygons that followed were read as lines and
left open: gmt info -Fi counted 7 records instead of 8. Skip both for
an empty segment.

Tested with:
  printf '1 2\n> P1\n0 0 1\n10 0 1\n10 10 1\n' | gmt spatial -Fp -i0:2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a good record the read loop skips comment lines, but the branch
that reads on after a too-short record did not. A comment right after
such a record left In NULL, and the loop then copied In->data.

Tested with:
  printf '1 2 3\n4 5\n# c\n6 7 8\n' | gmt convert

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Esteban82

Copy link
Copy Markdown
Member Author

The outputs with this fix are:

printf '30 45\n120 60\n' | gmt convert -i0:2
gmtconvert [WARNING]: Mismatch between actual (2) and expected (3) fields near line 1 in file <stdin>
gmtconvert [WARNING]: Mismatch between actual (2) and expected (3) fields near line 2 in file <stdin>

printf '1 2\n> P1\n0 0 1\n10 0 1\n10 10 1\n' | gmt spatial -Fp -i0:2
gmtspatial [WARNING]: Mismatch between actual (2) and expected (3) fields near line 1 in file <stdin>
> P1
0       0       1
10      0       1
10      10      1
0       0       1

 printf '1 2 3\n4 5\n# c\n6 7 8\n' | gmt convert
gmtconvert [WARNING]: Mismatch between actual (2) and expected (3) fields near line 2 in file <stdin>
1       2       3
6       7       8

@Esteban82
Esteban82 marked this pull request as ready for review September 27, 2026 15:08
@Esteban82 Esteban82 added bug Something isn't working add-changelog Add PR to the changelog labels Sep 28, 2026
@seisman seisman added this to the 6.8.0 milestone Sep 28, 2026
@Esteban82
Esteban82 merged commit 0f1e400 into master Sep 28, 2026
17 of 20 checks passed
@Esteban82
Esteban82 deleted the fix-read-table-empty-first-segment branch September 28, 2026 14:54
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 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants