Skip to content

Fix memory-safety and output-correctness bugs, add test harness - #39

Merged
NT7S merged 7 commits into
masterfrom
fix/jt-encode-bugs
Nov 28, 2026
Merged

NT7S merged 7 commits into
masterfrom
fix/jt-encode-bugs

Conversation

@NT7S

@NT7S NT7S commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes the memory-safety and output-correctness bugs found in a review of the encoder library, with a new 17-check test harness to verify them. Encoder output is bit-identical for valid inputs (verified against the original 555d7f8); the only intentional behavior change is dbm < -30 clamping to -30 (lowest valid power) instead of leaving power uninitialized.

Changes

Memory safety

  • jt65_encode / jt9_encode / jt4_encode / ft8_encode: bounded strncpy instead of strcpy into fixed buffers (overflowed on oversized messages)
  • fsq_encode / fsq_dir_encode: sprintf → snprintf; buffers sized for the documented maximum
  • wspr_encode: bounded call_/loc_ copies
  • wspr_bit_packing Type 2: bounded base_call/prefix reads; guarded slash_pos + 2/3 accesses
  • wspr_bit_packing Type 3: defined fallback for a missing > (was UB from bracket_avail - callsign - 1)
  • jt9_packbits: zero-pad the 207th bit instead of reading d[206] out of bounds
  • telemetry path: bounded strcpy(message, temp_msg)

Correctness

Resource management

  • Added ~JTEncode() destructor — frees the Reed-Solomon instance (rs_inst) allocated by the constructor (was a 236-byte leak per instance)
  • Constructor now initializes callsign/locator/power (was leaving them uninitialized)

Portability

  • Portable PROGMEM shim (JTEncode_pgmspace.h): AVR chips keep tables in flash, other cores use their own pgmspace.h when available, otherwise plain memory — the library now builds on all architectures (architectures=*), not just ATmega328P/168/32U4/16U4

Docs

  • README + source docstrings: corrected the FSQ symbols-buffer contract (2 symbols per char + terminator, not message+5); corrected FSQ character limits (130/100), ft8_encode example call, jt4_encode symbol count, wspr_encode dbm type; added destructor doc; changelog v1.4.0

Tests (new)

  • tests/harness.cpp + tests/run_tests.sh: 17 fork-isolated checks (ASan-observed + deterministic); exit 0 = clean, 1 = ≥1 bug detected, 2 = stub missing. Both new checks (1-char prefix golden symbols, 13-char callsign) were proven to DETECT their bugs by temporarily reintroducing each.

Verification

  • ./tests/run_tests.sh on this branch: 17/17 PASS, exit 0
  • Regression probe (original 555d7f8 vs this branch): bit-identical output for jt65/jt9/jt4/ft8/wspr (all 4 callsign variants), power rounding (55→53, 60→60), fsq, fsq_dir, grid CN84kh — only the intended dbm=-100 → -30 difference
  • Verified by an independent pass (Test Lab): exit 0, 17/17 PASS, git status matches expected
  • g++ -Wall -Wextra -Werror clean on src/JTEncode.cpp and src/nhash.c
  • CI (arduino-lint) green on head commit 8843490

Notes

  • Nothing is merged to master; this PR is unmerged.
  • tests/build/ is gitignored; only tests/harness.cpp, tests/run_tests.sh and tests/.gitignore are committed.

Fixes #35, fixes #33, fixes #29, fixes #18, fixes #28

NT7S added 7 commits November 28, 2026 02:55
Memory safety:
- jt65/jt9/jt4/ft8: bounded strncpy instead of strcpy into fixed buffers
- fsq_encode/fsq_dir_encode: snprintf + buffer sized for documented max
- wspr_encode: bounded call_/loc_ copies
- wspr_bit_packing Type 2/3: bounded base_call/prefix reads, guard
  slash_pos+2/3, defined fallback for missing '>' (was UB)
- jt9_packbits: zero-pad the 207th bit (was out-of-bounds read)
- telemetry path: bounded strcpy(message, temp_msg)

Correctness:
- latlon_to_grid: write ret_grid[6]=0 (was unterminated)
- wspr_message_prep: clamp dbm to [-30,60] (was only >60); work on local
  copies instead of mutating caller buffers in place
- wspr_encode/wspr_message_prep: bounded callsign/locator member copies
  with NUL terminator

Resource management:
- add ~JTEncode() destructor freeing rs_inst (was a 236-byte leak per
  instance); ctor now initialises callsign/locator/power

Docs: correct FSQ symbols-buffer contract (2 symbols per char + terminator)

Tests: new 13-check fork-isolated harness (ASan + deterministic), verified
by an independent pass: 13/13 DETECTED on the original tree, 13/13 PASS on
this branch. Encoder output is bit-identical for valid inputs.
- README: document ~JTEncode() destructor, int8_t dbm with [-30,60] clamp
- README: fix stale ft8_encode example call, jt4_encode symbol count,
  FSQ character limits (130/100, not 200)
- README: add v1.4.0 changelog entry
- library.properties: version 1.3.1 -> 1.4.0, fix FSQ character limits
- rs_encode(): emit a deterministic zero table instead of dereferencing a
  NULL codec (init failure, e.g. allocation failure)
- Document that FSQ characters with no table entry are silently dropped
  (README + source docstrings)
- Fix fsq_dir_encode docstring symbols contract (was copy-paste of
  fsq_encode's: omitted to_call)
The library claimed architectures=* but the PROGMEM include was guarded
by a hard-coded list of four AVR chips, so it failed to compile on every
other architecture (including the ATmega1280/2560 Mega). Introduce
src/JTEncode_pgmspace.h: any AVR chip includes <avr/pgmspace.h> (tables
stay in flash), cores that ship their own pgmspace.h (ESP8266, ESP32)
include it, and everything else falls back to plain memory.
- Harness: add jt4_strcpy_overflow and rs_inst_null_guard checks
  (both verified to DETECT their bugs)
- Shim: fallback respects a pre-existing PROGMEM/pgm_read_byte
  definition
- keywords.txt: add latlon_to_grid
- Example: int8_t dbm to match wspr_encode signature; tx_buffer sized
  for fsq_dir_encode's documented maximum (320, was 255)
- Changelog: note the 15-check harness
…telemetry

- WSPR: right-align 1-char prefix into the 3-char field with 2 leading
  spaces (was 1, producing an incorrect hash). Verified against the
  WSJT-X reference: m = 50631 for F/PE0FKO (was 49875).
- WSPR: widen callsign member 12 -> 13 bytes; compound callsigns up to
  12 chars no longer truncated. Bounds now use sizeof(callsign).
- FT8 telemetry: snprintf "%*s" -> "%18s" (broken on Atmel), i0 now
  initialized before use.
- ESP32 -Werror: drop unused tone/i/k8, fix crc8 sign-compare.
- Harness: 17 checks (golden-symbol 1-char prefix check, 13-char callsign
  check); both proven to detect their bugs.
@NT7S
NT7S merged commit ac8b1c6 into master Nov 28, 2026
2 checks passed
@NT7S
NT7S deleted the fix/jt-encode-bugs branch November 30, 2026 03: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

1 participant