Skip to content

fix: stop ASCII subplot save from segfaulting (#2019)#2023

Merged
krystophny merged 1 commit into
mainfrom
fix/2019-ascii-subplot-segfault
Jun 15, 2026
Merged

fix: stop ASCII subplot save from segfaulting (#2019)#2023
krystophny merged 1 commit into
mainfrom
fix/2019-ascii-subplot-segfault

Conversation

@krystophny

Copy link
Copy Markdown
Collaborator

Closes #2019.

Problem

Saving a figure that uses a subplot grid to the ASCII backend
(savefig('plot.txt')) crashed with a segmentation fault. The same figure
saved fine to PNG/PDF.

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
#3  __fortplot_subplot_rendering_MOD_render_subplots
#4  __fortplot_figure_render_engine_MOD_figure_render
#5  __fortplot_figure_core_io_MOD_render_figure_impl
...

Under gdb the fault is a len_trim on a null string:

#0  _gfortran_string_len_trim (len=..., s=0x0)
#2  __fortplot_subplot_rendering_MOD_render_subplots

Root cause: compute_tight_subplot_margins only supports the raster and PDF
backends, so for ASCII it returns ok=.false. and render_subplots takes
the non-tight margin branch. That branch reserves suptitle space with

if (allocated(state%suptitle) .and. len_trim(state%suptitle) > 0) then

which relies on short-circuit evaluation that Fortran does not guarantee (and
which the project rules explicitly forbid assuming). With no suptitle set,
len_trim dereferenced the unallocated string. Raster/PDF take the tight
path and skip this block, so they were unaffected.

Fix

Split the compound .and. into a nested if so len_trim never sees an
unallocated suptitle.

Verification

Build (serial to avoid the unrelated fpm parallel-build SIGABRT):

OMP_NUM_THREADS=1 fpm build

Failing before the fix (minimal 1x2 grid, no suptitle, save to .txt):

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
#3  __fortplot_subplot_rendering_MOD_render_subplots

Passing after the fix:

$ fpm test --target test_ascii_subplots_2019
 PASS: ASCII subplot save renders without crashing

Added test/ascii/test_ascii_subplots_2019.f90, which saves a subplot grid
(no suptitle) to ASCII and asserts the file is written and non-empty.

Related ASCII/subplot tests pass: test_ascii, test_subplots,
test_suptitle, test_utf8_text_preprocessing. Rendering gate
(make verify-artifacts) passes.

Saving a subplot grid to the ASCII backend crashed with SIGSEGV in
render_subplots. compute_tight_subplot_margins only supports the raster and
PDF backends, so for ASCII it returns ok=.false. and render_subplots takes
the non-tight margin branch. That branch reserves suptitle space with

    if (allocated(state%suptitle) .and. len_trim(state%suptitle) > 0)

which relies on short-circuit evaluation that Fortran does not guarantee.
With no suptitle set, len_trim dereferenced the unallocated string.

Split the test into a nested if so len_trim never sees an unallocated
suptitle. Add an ASCII subplot regression test.
@krystophny krystophny merged commit 7d0bfe1 into main Jun 15, 2026
6 checks passed
@krystophny krystophny deleted the fix/2019-ascii-subplot-segfault branch June 15, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASCII-Plot-Error

1 participant