Repository navigation
Fix memory-safety and output-correctness bugs, add test harness - #39
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 isdbm < -30clamping to-30(lowest valid power) instead of leavingpoweruninitialized.Changes
Memory safety
jt65_encode/jt9_encode/jt4_encode/ft8_encode: boundedstrncpyinstead ofstrcpyinto fixed buffers (overflowed on oversized messages)fsq_encode/fsq_dir_encode:sprintf→snprintf; buffers sized for the documented maximumwspr_encode: boundedcall_/loc_copieswspr_bit_packingType 2: boundedbase_call/prefixreads; guardedslash_pos + 2/3accesseswspr_bit_packingType 3: defined fallback for a missing>(was UB frombracket_avail - callsign - 1)jt9_packbits: zero-pad the 207th bit instead of readingd[206]out of boundsstrcpy(message, temp_msg)Correctness
latlon_to_grid: writesret_grid[6] = 0(was unterminated); verified grid for 44.3N, 123.1W =CN84khwspr_message_prep: clampsdbmto[-30, 60](was only clamping > 60 — leftpowerat sentinel fordbm < -30); works on local copies instead of mutating the caller'scall/locbuffers in place (the public API segfaulted on string literals) (JTEncode.cpp can cause unterminated character array, leading to undefined behavior #35)wspr_encode/wspr_message_prep: boundedcallsign/locatormember copies with NUL terminatorF/PE0FKO): prefix right-aligned into the 3-character field with the correct number of leading spaces, matching the WSJT-X reference — was 1 leading space for 1-char prefixes, producing an incorrect hash (WSPR Callsign with one char prefix failed. #29)callsignmember widened 12 → 13 bytes so a 12-character compound callsign is not truncated; Type 1/2 bounds now usesizeof(callsign)(variable callsigin: length correct? #28)snprintf("%*s", 18, c18)→snprintf("%18s", c18)(broken on Atmel toolchain);i0now initialized before use (Bug in FT8 Telemetry mode and an easy fix. (Atmel tool chain and possibly others) #18)-Werrorbuild failures: removed unusedtone/i/k8, initializedi0,crc8loop nowsize_t(ESP32 compiler sees errors #33)Resource management
~JTEncode()destructor — frees the Reed-Solomon instance (rs_inst) allocated by the constructor (was a 236-byte leak per instance)callsign/locator/power(was leaving them uninitialized)Portability
JTEncode_pgmspace.h): AVR chips keep tables in flash, other cores use their ownpgmspace.hwhen available, otherwise plain memory — the library now builds on all architectures (architectures=*), not just ATmega328P/168/32U4/16U4Docs
ft8_encodeexample call,jt4_encodesymbol count,wspr_encodedbm type; added destructor doc; changelog v1.4.0Tests (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.shon this branch: 17/17 PASS, exit 0555d7f8vs this branch): bit-identical output for jt65/jt9/jt4/ft8/wspr (all 4 callsign variants), power rounding (55→53, 60→60), fsq, fsq_dir, gridCN84kh— only the intendeddbm=-100 → -30differenceg++ -Wall -Wextra -Werrorclean onsrc/JTEncode.cppandsrc/nhash.c8843490Notes
master; this PR is unmerged.tests/build/is gitignored; onlytests/harness.cpp,tests/run_tests.shandtests/.gitignoreare committed.Fixes #35, fixes #33, fixes #29, fixes #18, fixes #28