Skip to content

Fix segfault in plot/psxyz when -a references an unknown aspatial field or -C is missing - #9157

Merged
Esteban82 merged 2 commits into
masterfrom
fix-9153
Aug 27, 2026
Merged

Fix segfault in plot/psxyz when -a references an unknown aspatial field or -C is missing#9157
Esteban82 merged 2 commits into
masterfrom
fix-9153

Conversation

@Esteban82

Copy link
Copy Markdown
Member

Issue: plot crashes with a segfault when -a references an aspatial field name that doesn't exist in the shapefile, or when -aZ=<field> is used without -C. With this fix, now fail with a clear error message instead:

$ gmt plot ne_110m_admin_0_countries.shp -W1p -G+z -aZ=POP -pdf map -L -Ctest.cpt
plot [ERROR]: Option -a: No such named aspatial item: POP
plot [ERROR]: Aspatial associations set with -a but input file is not in OGR/GMT format!

$ gmt plot ne_110m_admin_0_countries.shp -W1p -G+z -aZ=POP_EST -pdf map -L
plot [ERROR]: Option -a...=Z: Must supply a CPT via -C to look up the fill color

@seisman are those messages ok?

Fixes #9153

Done with Sonnet 5

Esteban82 and others added 2 commits August 27, 2026 11:58
gmtio_align_ogr_values() stored gmt_get_ogr_id()'s GMT_NOTSET (-1)
return value unchecked into GMT->common.a.ogr[], which was later used
as an array index (e.g. T[GMT->common.a.ogr[col]] in
gmtio_build_text_from_ogr()) whenever -a<flag>=<name> named a field
that does not exist in the OGR/shapefile attribute table. This read
out of bounds and either crashed directly in strncat() or corrupted
the segment header buffer, crashing later in gmt_parse_segment_header()
-- matching both stack traces in the report.

A validation for this exact case already existed in
gmtio_select_all_ogr_if_requested(), but it only runs once the first
data record's column types are examined, which is after the crash
already occurs while processing the first segment header. Move the
check to where the index is computed so it fires before any use, and
also mark the record-loop's associated bail-out path as EOF so the
resulting error is reported once instead of once per record.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…hout -C

The issue's third repro used a *valid* aspatial field name (-aZ=POP_EST)
but omitted -C. plot/psxy and psxyz initialize their CPT pointer P to
NULL until -C is parsed. gmt_parse_segment_header()'s OGR/aspatial branch
dereferenced P unconditionally in the GMT_IS_Z case (P->categorical),
crashing with SIGSEGV in gmt_parse_segment_header - matching the second
stack trace in the report. The plain (non-aspatial) -Z branch just below
already guards the equivalent lookup with `if (P && ...)`; the aspatial
branch was missing the same guard.

Added the same guard at the point of use in gmt_io.c, and also added an
early, clear validation error in psxy.c/psxyz.c's option parsing (mirroring
the existing "Option -Z: No CPT given via -C" check) so users seeing
-a<flag>=Z without -C get a helpful message and a non-zero exit code
instead of silently plotting without color or (previously) crashing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@Esteban82
Esteban82 requested review from joa-quim and seisman August 27, 2026 15:29
@Esteban82 Esteban82 added add-changelog Add PR to the changelog AI-assisted All (or most) of the code was written by Artificial Intelligence. labels Aug 27, 2026
@Esteban82
Esteban82 merged commit c076e23 into master Aug 27, 2026
11 of 14 checks passed
@Esteban82
Esteban82 deleted the fix-9153 branch August 27, 2026 17:15
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.

plot crashes when invalid aspatial name is passed

2 participants