Skip to content

fix: stop recode file crashing with a NULL codefrom - #160

Open
antLI-dev wants to merge 1 commit into
masterfrom
fix/stnrecode-file-null-crash
Open

fix: stop recode file crashing with a NULL codefrom#160
antLI-dev wants to merge 1 commit into
masterfrom
fix/stnrecode-file-null-crash

Conversation

@antLI-dev

@antLI-dev antLI-dev commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Every use of recode file <recodefile> crashed SNAP unconditionally,
regardless of the recode file's own content, leaving an empty .err file
behind - matching a user report of the feature being "unclear... doesn't
work... error file empty". Reported by @mermy

read_station_recode_definition (stnrecode.cpp:410) handles three
distinct recode grammars in one function: file, suffix, and inline
<code> to <code>. The file branch fully delegates to
read_station_recode_file, which already applies every recode from the
CSV itself - but the branch never returned afterward, so execution fell
through into ~200 lines of optional uncertainty/date-range parsing that
only apply to the other two grammars (confirmed: the CSV format's own
columns have no uncertainty equivalent at all), ending in a call to
add_stn_recode_to_map_err with codefrom/codeto still NULL (never
set on this branch). _stricmp(NULL, ...) segfaults immediately.

The crash happening before close_output_files() can run is why the
.err file was empty - same underlying mechanism as
fix/snap-exit-code-error-reporting, just unreachable by that fix since a
segfault isn't a C++ exception.

Fix: the file branch now returns immediately after handling itself,
since it never shared any of the fall-through logic to begin with.

Test plan

  • Reproduced the crash directly (hand-built minimal .snp/.crd/
    .dat/recode-CSV, release build segfaults immediately, both .err
    and .lst come out as 0 bytes)
  • Root-caused with AddressSanitizer (no gdb available, no root to
    install it) - pointed directly at the NULL dereference
  • Confirmed fixed: same reproduction now completes normally, produces
    a correct .lst with the recode actually applied, no .err
  • Full regression suite (testall.pl -r) passes unchanged

GSR-1011

"recode file <recodefile>" fully delegates to
read_station_recode_file, which already applies every recode from
the file itself. The function never returned afterward though, so
execution fell through into logic meant only for the other two
recode grammars (inline "recode X to Y" and "recode suffix ..."),
which share optional uncertainty/date-range parsing this form has no
equivalent of. That logic ends with a call to
add_stn_recode_to_map_err using codefrom/codeto, which are never set
on the file branch - codefrom stays NULL, and _stricmp(codefrom, ...)
segfaults immediately.

This meant every use of "recode file X" crashed unconditionally, with
an empty .err file since the crash happens before
close_output_files() can run - not a fault in the recode file's own
content.

Reproduced and confirmed fixed with AddressSanitizer, pointing
directly at the NULL dereference. Full regression suite (testall.pl
-r) passes unchanged.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@antLI-dev
antLI-dev requested review from ccrook and mermy August 4, 2026 02:32
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.

1 participant