From 58e226df0aa79cb9bc5e518ba391998eb8790e23 Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 02:55:42 +0000 Subject: [PATCH 1/7] Fix memory-safety and output-correctness bugs, add test harness 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.md | 10 +- src/JTEncode.cpp | 119 +++++++++++++----- src/JTEncode.h | 1 + tests/.gitignore | 1 + tests/harness.cpp | 301 +++++++++++++++++++++++++++++++++++++++++++++ tests/run_tests.sh | 35 ++++++ 6 files changed, 432 insertions(+), 35 deletions(-) create mode 100644 tests/.gitignore create mode 100644 tests/harness.cpp create mode 100755 tests/run_tests.sh diff --git a/README.md b/README.md index e662886..d4ae5aa 100644 --- a/README.md +++ b/README.md @@ -237,8 +237,9 @@ Public Methods * from_call - Callsign of issuing station (maximum size: 20) * message - Null-terminated message string, no greater than 130 chars in length * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least the size of the message - * plus 5 characters to the method. Terminated in 0xFF. + * Ensure that you pass a uint8_t array of at least 2x (call + message + 5) + 1 + * characters to the method. Each transmitted character can produce up to two + * channel symbols, plus the 0xFF terminator. * */ ``` @@ -255,8 +256,9 @@ Public Methods * cmd - Directed command * message - Null-terminated message string, no greater than 100 chars in length * symbols - Array of channel symbols to transmit returned by the method. -* Ensure that you pass a uint8_t array of at least the size of the message -* plus 5 characters to the method. Terminated in 0xFF. +* Ensure that you pass a uint8_t array of at least 2x (from_call + to_call + message + 13) + 1 +* characters to the method. Each transmitted character can produce up to two +* channel symbols, plus the 0xFF terminator. * */ ``` diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index fcb7723..b1ac69a 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -49,7 +49,18 @@ JTEncode::JTEncode(void) { // Initialize the Reed-Solomon encoder rs_inst = (struct rs *)(intptr_t)init_rs_int(6, 0x43, 3, 1, 51, 0); - // memset(callsign, 0, 13); + memset(callsign, 0, sizeof(callsign)); + memset(locator, 0, sizeof(locator)); + power = -1; +} + +JTEncode::~JTEncode() +{ + if(rs_inst != NULL) + { + free_rs_int(rs_inst); + rs_inst = NULL; + } } /* @@ -67,7 +78,8 @@ void JTEncode::jt65_encode(const char * msg, uint8_t * symbols) { char message[14]; memset(message, 0, 14); - strcpy(message, msg); + strncpy(message, msg, 13); + message[13] = 0; // Ensure that the message text conforms to standards // -------------------------------------------------- @@ -111,7 +123,8 @@ void JTEncode::jt9_encode(const char * msg, uint8_t * symbols) { char message[14]; memset(message, 0, 14); - strcpy(message, msg); + strncpy(message, msg, 13); + message[13] = 0; // Ensure that the message text conforms to standards // -------------------------------------------------- @@ -160,7 +173,8 @@ void JTEncode::jt4_encode(const char * msg, uint8_t * symbols) { char message[14]; memset(message, 0, 14); - strcpy(message, msg); + strncpy(message, msg, 13); + message[13] = 0; // Ensure that the message text conforms to standards // -------------------------------------------------- @@ -205,8 +219,10 @@ void JTEncode::wspr_encode(const char * call, const char * loc, const int8_t dbm char call_[13]; char loc_[7]; uint8_t dbm_ = dbm; - strcpy(call_, call); - strcpy(loc_, loc); + memset(call_, 0, sizeof(call_)); + memset(loc_, 0, sizeof(loc_)); + strncpy(call_, call, sizeof(call_) - 1); + strncpy(loc_, loc, sizeof(loc_) - 1); // Ensure that the message text conforms to standards // -------------------------------------------------- @@ -239,13 +255,14 @@ void JTEncode::wspr_encode(const char * call, const char * loc, const int8_t dbm * from_call - Callsign of issuing station (maximum size: 20) * message - Null-terminated message string, no greater than 130 chars in length * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least the size of the message - * plus 5 characters to the method. Terminated in 0xFF. + * Ensure that you pass a uint8_t array of at least 2x (call + message + 5) + 1 + * characters to the method. Each transmitted character can produce up to two + * channel symbols, plus the 0xFF terminator. * */ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t * symbols) { - char tx_buffer[155]; + char tx_buffer[160]; char * tx_message; uint16_t symbol_pos = 0; uint8_t i, fch, vcode1, vcode2, tone; @@ -253,11 +270,12 @@ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t // Clear out the transmit buffer // ----------------------------- - memset(tx_buffer, 0, 155); + memset(tx_buffer, 0, sizeof(tx_buffer)); // Create the message to be transmitted // ------------------------------------ - sprintf(tx_buffer, " \n%s: %s", from_call, message); + // Buffer sized for the documented maximum: 20-char call + 130-char message. + snprintf(tx_buffer, sizeof(tx_buffer), " \n%s: %s", from_call, message); tx_message = tx_buffer; @@ -326,13 +344,14 @@ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t * cmd - Directed command * message - Null-terminated message string, no greater than 100 chars in length * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least the size of the message - * plus 5 characters to the method. Terminated in 0xFF. + * Ensure that you pass a uint8_t array of at least 2x (call + message + 5) + 1 + * characters to the method. Each transmitted character can produce up to two + * channel symbols, plus the 0xFF terminator. * */ void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, const char cmd, const char * message, uint8_t * symbols) { - char tx_buffer[155]; + char tx_buffer[160]; char * tx_message; uint16_t symbol_pos = 0; uint8_t i, fch, vcode1, vcode2, tone, from_call_crc; @@ -344,13 +363,14 @@ void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, cons // Clear out the transmit buffer // ----------------------------- - memset(tx_buffer, 0, 155); + memset(tx_buffer, 0, sizeof(tx_buffer)); // Create the message to be transmitted // We are building a directed message here. // FSQ very specifically needs " \b " in // directed mode to indicate EOT. A single backspace won't do it. - sprintf(tx_buffer, " \n%s:%02x%s%c%s%s", from_call, from_call_crc, to_call, cmd, message, " \b "); + // Buffer sized for the documented maximum: 20-char calls + 100-char message. + snprintf(tx_buffer, sizeof(tx_buffer), " \n%s:%02x%s%c%s%s", from_call, from_call_crc, to_call, cmd, message, " \b "); tx_message = tx_buffer; @@ -427,7 +447,8 @@ void JTEncode::ft8_encode(const char * msg, uint8_t * symbols) char message[19]; memset(message, 0, 19); - strcpy(message, msg); + strncpy(message, msg, 18); + message[18] = 0; // Bit packing // ----------- @@ -497,6 +518,7 @@ void JTEncode::latlon_to_grid(float lat, float lon, char* ret_grid) grid[5] = (char)((uint8_t)(lat * 24) + 'a'); strncpy(ret_grid, grid, 6); + ret_grid[6] = 0; } /* Private Class Members */ @@ -664,11 +686,22 @@ void JTEncode::ft_message_prep(char * message) } } - strcpy(message, temp_msg); + strncpy(message, temp_msg, 18); + message[18] = 0; } void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) { + // Work on local copies so the caller's buffers are never modified + char call_[13]; + char loc_[7]; + memset(call_, 0, sizeof(call_)); + memset(loc_, 0, sizeof(loc_)); + strncpy(call_, call, sizeof(call_) - 1); + strncpy(loc_, loc, sizeof(loc_) - 1); + call = call_; + loc = loc_; + // Callsign validation and padding // ------------------------------- @@ -687,7 +720,8 @@ void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) } call[12] = 0; - strncpy(callsign, call, 12); + memset(callsign, 0, sizeof(callsign)); + strncpy(callsign, call, sizeof(callsign) - 1); // Grid locator validation if(strlen(loc) == 4 || strlen(loc) == 6) @@ -725,7 +759,8 @@ void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) } } - strncpy(locator, loc, 7); + memset(locator, 0, sizeof(locator)); + strncpy(locator, loc, sizeof(locator) - 1); // Power level validation // Only certain increments are allowed @@ -733,6 +768,10 @@ void JTEncode::wspr_message_prep(char * call, char * loc, int8_t dbm) { dbm = 60; } + if(dbm < -30) + { + dbm = -30; + } const uint8_t VALID_DBM_SIZE = 28; const int8_t valid_dbm[VALID_DBM_SIZE] = {-30, -27, -23, -20, -17, -13, -10, -7, -3, @@ -872,7 +911,19 @@ void JTEncode::wspr_bit_packing(uint8_t * c) memset(base_call, 0, 13); uint32_t init_val = 146; char* bracket_avail = strchr(callsign, (int)'>'); - int call_len = bracket_avail - callsign - 1; + int call_len; + if(bracket_avail != NULL) + { + call_len = (int)(bracket_avail - callsign - 1); + } + else + { + // Missing '>' - treat everything after '<' as the base call. + call_len = (int)strlen(callsign) - 1; + } + // Clamp to the base_call buffer and keep nhash_ in bounds + if(call_len > 12) call_len = 12; + if(call_len < 0) call_len = 0; strncpy(base_call, callsign + 1, call_len); uint32_t hash = nhash_(base_call, &call_len, &init_val); hash &= 32767; @@ -918,12 +969,13 @@ void JTEncode::wspr_bit_packing(uint8_t * c) uint8_t i; // Determine prefix or suffix - if(callsign[slash_pos + 2] == ' ' || callsign[slash_pos + 2] == 0) + if((slash_pos + 2 < 12) && (callsign[slash_pos + 2] == ' ' || callsign[slash_pos + 2] == 0)) { // Single character suffix char base_call[7]; memset(base_call, 0, 7); - strncpy(base_call, callsign, slash_pos); + int base_len = slash_pos < 6 ? slash_pos : 6; + strncpy(base_call, callsign, base_len); for(i = 0; i < 7; i++) { base_call[i] = toupper(base_call[i]); @@ -959,12 +1011,13 @@ void JTEncode::wspr_bit_packing(uint8_t * c) m = (m * 128) + power + 2 + 64; } - else if(callsign[slash_pos + 3] == ' ' || callsign[slash_pos + 3] == 0) + else if((slash_pos + 3 < 12) && (callsign[slash_pos + 3] == ' ' || callsign[slash_pos + 3] == 0)) { // Two-digit numerical suffix char base_call[7]; memset(base_call, 0, 7); - strncpy(base_call, callsign, slash_pos); + int base_len = slash_pos < 6 ? slash_pos : 6; + strncpy(base_call, callsign, base_len); for(i = 0; i < 6; i++) { base_call[i] = toupper(base_call[i]); @@ -994,8 +1047,9 @@ void JTEncode::wspr_bit_packing(uint8_t * c) char base_call[7]; memset(prefix, 0, 4); memset(base_call, 0, 7); - strncpy(prefix, callsign, slash_pos); - strncpy(base_call, callsign + slash_pos + 1, 7); + int prefix_len = slash_pos < 3 ? slash_pos : 3; + strncpy(prefix, callsign, prefix_len); + strncpy(base_call, callsign + slash_pos + 1, 6); if(prefix[2] == ' ' || prefix[2] == 0) { @@ -1130,7 +1184,8 @@ void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword) temp_msg[i] = toupper(temp_msg[i]); } } - strcpy(message, temp_msg); + strncpy(message, temp_msg, 18); + message[18] = 0; uint8_t temp_int; @@ -1309,13 +1364,15 @@ void JTEncode::jt9_packbits(uint8_t * d, uint8_t * a) for(i = 0; i < JT9_ENCODE_COUNT; i++) { - a[i] = (d[k] & 1) << 2; + // 69 symbols x 3 bits = 207 bits, but only JT9_BIT_COUNT (206) + // bits are available; the final symbol's MSB is zero-padded. + if(k < JT9_BIT_COUNT) a[i] = (d[k] & 1) << 2; k++; - a[i] |= ((d[k] & 1) << 1); + if(k < JT9_BIT_COUNT) a[i] |= ((d[k] & 1) << 1); k++; - a[i] |= (d[k] & 1); + if(k < JT9_BIT_COUNT) a[i] |= (d[k] & 1); k++; } } diff --git a/src/JTEncode.h b/src/JTEncode.h index 04718a5..7cdf7ea 100644 --- a/src/JTEncode.h +++ b/src/JTEncode.h @@ -218,6 +218,7 @@ class JTEncode { public: JTEncode(void); + ~JTEncode(); void jt65_encode(const char *, uint8_t *); void jt9_encode(const char *, uint8_t *); void jt4_encode(const char *, uint8_t *); diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/tests/harness.cpp b/tests/harness.cpp new file mode 100644 index 0000000..7100fca --- /dev/null +++ b/tests/harness.cpp @@ -0,0 +1,301 @@ +/* + * JTEncode high-priority bug harness. + * + * Each check runs in a forked child process so an AddressSanitizer abort + * in one check does not abort the others. Exit codes: + * 0 = PASS (no bug found) + * 1 = DETECTED (check failed or crashed) + * + * ASan/LSan catch the memory-corruption and leak checks; the remaining + * checks assert deterministically: exact Maidenhead grid + null + * terminator, power sentinel + round-down/exact-match values, and + * callsign member termination. + * + * Child stderr is captured to a temp file so the parent can print a short + * evidence excerpt instead of dumping full ASan reports to the terminal. + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifndef REPO_ROOT +#define REPO_ROOT "." +#endif + +#define private public +#include "JTEncode.h" +#undef private + +// --------------------------------------------------------------------------- +// Test bodies. Return 0 = PASS, 1 = DETECTED. +// ASan-crash tests abort inside the library; the parent reports that as +// DETECTED and prints the first line of the ASan report as evidence. +// --------------------------------------------------------------------------- + +static int test_jt9_oob() +{ + JTEncode enc; + uint8_t symbols[JT9_SYMBOL_COUNT]; + enc.jt9_encode("NT7S", symbols); + return 0; +} + +static int test_fsq_encode_overflow() +{ + JTEncode enc; + char call[21]; + memset(call, 'B', 20); call[20] = 0; + char msg[131]; + memset(msg, 'A', 130); msg[130] = 0; + // 2 symbols per char x (3 + 20 + 2 + 130) chars + 1 terminator + slack + uint8_t symbols[320]; + enc.fsq_encode(call, msg, symbols); + return 0; +} + +static int test_jt65_strcpy_overflow() +{ + JTEncode enc; + char msg[20]; + memset(msg, 'A', 19); msg[19] = 0; + uint8_t symbols[JT65_SYMBOL_COUNT]; + enc.jt65_encode(msg, symbols); + return 0; +} + +static int test_ft8_strcpy_overflow() +{ + JTEncode enc; + char msg[20]; + memset(msg, 'A', 19); msg[19] = 0; + uint8_t symbols[FT8_SYMBOL_COUNT]; + enc.ft8_encode(msg, symbols); + return 0; +} + +static int test_wspr_callsign_strcpy_overflow() +{ + JTEncode enc; + char call[20]; + memset(call, 'B', 19); call[19] = 0; + uint8_t symbols[WSPR_SYMBOL_COUNT]; + enc.wspr_encode(call, "CN85", 30, symbols); + return 0; +} + +static int test_wspr_type3_missing_close_bracket() +{ + JTEncode enc; + uint8_t symbols[WSPR_SYMBOL_COUNT]; + enc.wspr_encode(" adj_lon=56.9, adj_lat=134.3 + // field='C','N' square='8','4' subsquare='k','h' + if (strcmp(grid, "CN84kh") != 0) { + printf("grid = '%s' (expected 'CN84kh')\n", grid); + return 1; + } + return 0; +} + +static int test_wspr_invalid_power_uninitialized() +{ + uint8_t symbols[WSPR_SYMBOL_COUNT]; + + // Bug: dbm < -30 leaves power uninitialized (never overwritten from sentinel) + JTEncode enc; + enc.power = -1; // sentinel - must be overwritten + enc.wspr_encode("NT7S", "CN85", -100, symbols); + if (enc.power == -1) { + printf("power still at sentinel -1 for dbm=-100\n"); + return 1; + } + + // Rounding: dbm=55 must round down to 53 (nearest valid power below) + JTEncode enc2; + enc2.power = -1; + enc2.wspr_encode("NT7S", "CN85", 55, symbols); + if (enc2.power != 53) { + printf("power = %d for dbm=55 (expected 53)\n", (int)enc2.power); + return 1; + } + + // Exact match: dbm=60 must stay 60 + JTEncode enc3; + enc3.power = -1; + enc3.wspr_encode("NT7S", "CN85", 60, symbols); + if (enc3.power != 60) { + printf("power = %d for dbm=60 (expected 60)\n", (int)enc3.power); + return 1; + } + return 0; +} + +static int test_wspr_callsign_not_terminated() +{ + JTEncode enc; + uint8_t symbols[WSPR_SYMBOL_COUNT]; + enc.wspr_encode("NT7S", "CN85", 30, symbols); + bool has_null = false; + for (int i = 0; i < 12; i++) + if (enc.callsign[i] == 0) has_null = true; + if (!has_null) { + printf("callsign has no null terminator\n"); + return 1; + } + return 0; +} + +static int test_rs_inst_leak() +{ + JTEncode enc; + (void)enc; + return 0; // LSan detects the leak at exit +} + +static int test_fsq_dir_over_long_message() +{ + JTEncode enc; + char call[21]; + memset(call, 'B', 20); call[20] = 0; + char msg[151]; + memset(msg, 'A', 150); msg[150] = 0; + uint8_t symbols[600]; + enc.fsq_dir_encode(call, call, '>', msg, symbols); + return 0; +} + +// --------------------------------------------------------------------------- +// Runner +// --------------------------------------------------------------------------- + +struct Test { + const char *name; + const char *desc; + std::function fn; +}; + +static int run_in_child(const Test &t, const char *err_file) +{ + fflush(stdout); + fflush(stderr); + pid_t pid = fork(); + if (pid == 0) { + int fd = open(err_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd >= 0) { + dup2(fd, 1); + dup2(fd, 2); + if (fd != 1 && fd != 2) close(fd); + } + int rc = t.fn(); + exit(rc); // runs atexit handlers (incl. LSan) + } + int status = 0; + waitpid(pid, &status, 0); + if (WIFEXITED(status)) return WEXITSTATUS(status); + return 1; // crashed +} + +// Print the first informative line from the child's captured stderr. +static void print_evidence(const char *err_file) +{ + FILE *f = fopen(err_file, "r"); + if (!f) return; + char line[256]; + while (fgets(line, sizeof(line), f)) { + // Skip empty lines and pure-'=' divider lines + size_t n = strlen(line); + if (n == 0 || line[0] == '\n') continue; + bool pure_equals = true; + for (size_t i = 0; i < n; i++) + if (line[i] != '=' && line[i] != '\n' && line[i] != '\r') pure_equals = false; + if (pure_equals) continue; + // Keep the first informative line + if (strstr(line, "ERROR:") || strstr(line, "LeakSanitizer:") || + strstr(line, "leaked") || strstr(line, "sentinel") || + strstr(line, "terminator") || strstr(line, "grid[6]") || + strstr(line, "grid =") || strstr(line, "expected 'CN84kh'") || + strstr(line, "callsign") || strstr(line, "power")) { + if (n && line[n - 1] == '\n') line[n - 1] = 0; + printf(" | %s\n", line); + break; + } + } + fclose(f); +} + +int main() +{ + const Test tests[] = { + {"jt9_oob", "jt9_packbits reads d[206] out of bounds", test_jt9_oob}, + {"fsq_encode_overflow", "fsq_encode sprintf overflows 155-byte buffer", test_fsq_encode_overflow}, + {"jt65_strcpy_overflow", "jt65_encode strcpy into 14-byte message buffer", test_jt65_strcpy_overflow}, + {"ft8_strcpy_overflow", "ft8_encode strcpy into 19-byte message buffer", test_ft8_strcpy_overflow}, + {"wspr_callsign_strcpy_overflow", "wspr_encode strcpy into 13-byte call_ buffer", test_wspr_callsign_strcpy_overflow}, + {"wspr_type3_missing_close_bracket", "WSPR Type 3 missing '>' - UB strncpy", test_wspr_type3_missing_close_bracket}, + {"wspr_type2_long_prefix", "WSPR Type 2 >3-char prefix overflows 4-byte prefix", test_wspr_type2_long_prefix}, + {"wspr_type2_long_base_call", "WSPR Type 2 >6-char base call overflows 7-byte base_call", test_wspr_type2_long_base_call}, + {"latlon_not_terminated", "latlon_to_grid: unterminated + wrong grid", test_latlon_not_terminated}, + {"wspr_invalid_power_uninitialized", "wspr_message_prep: uninit for dbm<-30 + wrong rounding", test_wspr_invalid_power_uninitialized}, + {"wspr_callsign_not_terminated", "wspr_encode leaves callsign member unterminated", test_wspr_callsign_not_terminated}, + {"rs_inst_leak", "constructor allocates rs_inst; no destructor frees it", test_rs_inst_leak}, + {"fsq_dir_over_long_message", "fsq_dir_encode sprintf overflow (bonus, beyond max)", test_fsq_dir_over_long_message}, + }; + + const int total = sizeof(tests) / sizeof(tests[0]); + int detected = 0; + + printf("=== JTEncode high-priority bug harness ===\n\n"); + + for (int i = 0; i < total; i++) { + char err_file[256]; + snprintf(err_file, sizeof(err_file), "%s/tests/build/evidence-%d.log", REPO_ROOT, i); + + printf("[%2d/%2d] %-36s %s ... ", i + 1, total, tests[i].name, tests[i].desc); + int rc = run_in_child(tests[i], err_file); + if (rc == 0) { + printf("PASS\n"); + } else { + printf("DETECTED\n"); + detected++; + print_evidence(err_file); + } + } + + printf("\n=== %d/%d checks detected a bug ===\n", detected, total); + return detected ? 1 : 0; +} diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100755 index 0000000..1be765c --- /dev/null +++ b/tests/run_tests.sh @@ -0,0 +1,35 @@ +#!/bin/sh +# Build and run the JTEncode high-priority bug harness. +# Usage: tests/run_tests.sh +# Override the Arduino stub location with: STUB=/path/to/stub tests/run_tests.sh +set -eu + +REPO="$(cd "$(dirname "$0")/.." && pwd)" +STUB="${STUB:-/opt/data/profiles/sam-swe/cache/scratch/jt-stub}" +BUILD="$REPO/tests/build" + +if [ ! -f "$STUB/Arduino.h" ]; then + echo "ERROR: Arduino stub not found at $STUB" >&2 + echo "The stub must contain Arduino.h and avr/pgmspace.h." >&2 + echo "Point STUB at a valid stub, e.g.:" >&2 + echo " STUB=/path/to/stub $0" >&2 + exit 2 +fi + +mkdir -p "$BUILD" + +# Compile the C sources as C, then link the C++ harness with ASan. +gcc -std=c11 -O1 -I"$STUB" -I"$REPO/src" -c "$REPO/src/crc14.c" -o "$BUILD/crc14.o" +gcc -std=c11 -O1 -I"$STUB" -I"$REPO/src" -c "$REPO/src/nhash.c" -o "$BUILD/nhash.o" + +g++ -std=c++11 -g -O1 -fsanitize=address \ + -I"$STUB" -I"$REPO/src" \ + -DREPO_ROOT="\"$REPO\"" \ + "$REPO/tests/harness.cpp" \ + "$REPO/src/JTEncode.cpp" \ + "$REPO/src/init_rs_int.cpp" \ + "$REPO/src/encode_rs_int.cpp" \ + "$BUILD/crc14.o" "$BUILD/nhash.o" \ + -o "$BUILD/harness" + +"$BUILD/harness" From 101d13587a4193fe1a45817abc4fc2b8e10c340e Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:01:54 +0000 Subject: [PATCH 2/7] Document v1.4.0 API changes and bump version - 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 --- README.md | 37 ++++++++++++++++++++++++++++++++----- library.properties | 4 ++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d4ae5aa..6ed1cd7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ JT65/JT9/JT4/FT8/WSPR/FSQ Encoder Library for Arduino ===================================================== -This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. It will also generate an arbitrary FSQ message of up to 200 characters in both directed and non-directed format. When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. +This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a Type 1, Type 2, or Type 3 message for WSPR. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. Please feel free to use the issues feature of GitHub if you run into problems or have suggestions for important features to implement. @@ -127,7 +127,7 @@ Before transmit, the proper class method is chosen based on the desired mode, th jtencode.wspr_encode(call, loc, dbm, tx_buffer); break; case MODE_FT8: - jtencode.ft_encode(message, tx_buffer); + jtencode.ft8_encode(message, tx_buffer); break; case MODE_FSQ_2: case MODE_FSQ_3: @@ -150,6 +150,19 @@ Once the channel symbols have been generated, it is a simple matter of transmitt Public Methods ------------------ +### JTEncode(void) / ~JTEncode() +``` +/* + * JTEncode(); + * ~JTEncode(); + * + * Constructs a JTEncode instance, initializing the callsign, locator, and + * power fields and allocating the Reed-Solomon instance used by jt65_encode(). + * The destructor frees that instance; it is safe to let a JTEncode instance + * go out of scope without an explicit delete. + * + */ +``` ### jt65_encode() ``` /* @@ -189,7 +202,7 @@ Public Methods * * message - Plaintext Type 6 message. * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least size JT9_SYMBOL_COUNT to the method. + * Ensure that you pass a uint8_t array of at least size JT4_SYMBOL_COUNT to the method. * */ ``` @@ -197,14 +210,15 @@ Public Methods ### wspr_encode() ``` /* - * wspr_encode(const char * call, const char * loc, const uint8_t dbm, uint8_t * symbols) + * wspr_encode(const char * call, const char * loc, const int8_t dbm, uint8_t * symbols) * * Takes a callsign, grid locator, and power level and returns a WSPR symbol * table for a Type 1, 2, or 3 message. * * call - Callsign (12 characters maximum). * loc - Maidenhead grid locator (6 characters maximum). - * dbm - Output power in dBm. + * dbm - Output power in dBm. Valid range is -30 to 60 dBm; values are rounded + * down to the nearest valid power level. * symbols - Array of channel symbols to transmit returned by the method. * Ensure that you pass a uint8_t array of at least size WSPR_SYMBOL_COUNT to the method. * @@ -295,6 +309,19 @@ Also, a big thank you to Murray Greenman, ZL1BPU for working allowing me to pick Changelog --------- +* v1.4.0 + + * Added ~JTEncode() destructor that frees the Reed-Solomon instance allocated by the constructor + * Initialize callsign/locator/power fields in the constructor (previously left uninitialized) + * Fix memory-safety bugs: bounded copies in jt65/jt9/jt4/ft8 message paths, fsq/fsq_dir + snprintf + buffer sized for documented maximum, bounded WSPR Type 2/3 reads, zero-pad the + 207th bit in jt9_packbits, defined fallback for a missing '>' in Type 3 WSPR messages + * Fix latlon_to_grid() not terminating ret_grid + * Fix wspr_message_prep(): clamp dbm to [-30, 60] and no longer mutate the caller's buffers + * Correct FSQ symbols-buffer contract in docs (2 symbols per char + terminator) + * Correct stale README references (ft8_encode example call, jt4_encode symbol count, + wspr_encode dbm type) + * v1.3.1 * Added latitude/longitude to Maidenhead grid convenience function diff --git a/library.properties b/library.properties index ea711ed..8a9e23b 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=Etherkit JTEncode -version=1.3.1 +version=1.4.0 author=Jason Milldrum maintainer=Jason Milldrum sentence=Generate JT65, JT9, JT4, FT8, WSPR, and FSQ symbols on your Arduino. -paragraph=This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a callsign, Maidenhead grid locator, and power output for WSPR. It will also generate an arbitrary FSQ message of up to 200 characters in both directed and non-directed format. When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. +paragraph=This library very simply generates a set of channel symbols for JT65, JT9, JT4, FT8, or WSPR based on the user providing a properly formatted Type 6 message for JT65, JT9, or JT4 (which is 13 valid characters), Type 0.0 or 0.5 message for FT8 (v2.0.0 protocol) or a callsign, Maidenhead grid locator, and power output for WSPR. It will also generate an arbitrary FSQ message of up to 130 characters (non-directed) or 100 characters (directed). When paired with a synthesizer that can output frequencies in fine, phase-continuous tuning steps (such as the Si5351), then a beacon or telemetry transmitter can be created which can change the transmitted characters as needed from the Arduino. category=Data Processing url=https://github.com/etherkit/JTEncode architectures=* From a686f9985d821331af3b6d2a5a49290583c3a4ac Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:08:47 +0000 Subject: [PATCH 3/7] Guard NULL RS codec; document FSQ silent-drop limitation - 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) --- README.md | 8 ++++++++ src/JTEncode.cpp | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ed1cd7..d17f1e1 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,9 @@ Public Methods * characters to the method. Each transmitted character can produce up to two * channel symbols, plus the 0xFF terminator. * + * Note: characters with no entry in the FSQ table are silently dropped + * (they produce no channel symbols). + * */ ``` @@ -274,6 +277,9 @@ Public Methods * characters to the method. Each transmitted character can produce up to two * channel symbols, plus the 0xFF terminator. * +* Note: characters with no entry in the FSQ table are silently dropped +* (they produce no channel symbols). +* */ ``` @@ -319,6 +325,8 @@ Changelog * Fix latlon_to_grid() not terminating ret_grid * Fix wspr_message_prep(): clamp dbm to [-30, 60] and no longer mutate the caller's buffers * Correct FSQ symbols-buffer contract in docs (2 symbols per char + terminator) + * Guard rs_encode against a NULL Reed-Solomon codec (allocation failure) + * Document that characters with no entry in the FSQ table are silently dropped * Correct stale README references (ft8_encode example call, jt4_encode symbol count, wspr_encode dbm type) diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index b1ac69a..140f72e 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -259,6 +259,9 @@ void JTEncode::wspr_encode(const char * call, const char * loc, const int8_t dbm * characters to the method. Each transmitted character can produce up to two * channel symbols, plus the 0xFF terminator. * + * Note: characters with no entry in the FSQ table are silently dropped + * (they produce no channel symbols). + * */ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t * symbols) { @@ -344,10 +347,13 @@ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t * cmd - Directed command * message - Null-terminated message string, no greater than 100 chars in length * symbols - Array of channel symbols to transmit returned by the method. - * Ensure that you pass a uint8_t array of at least 2x (call + message + 5) + 1 + * Ensure that you pass a uint8_t array of at least 2x (from_call + to_call + message + 13) + 1 * characters to the method. Each transmitted character can produce up to two * channel symbols, plus the 0xFF terminator. * + * Note: characters with no entry in the FSQ table are silently dropped + * (they produce no channel symbols). + * */ void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, const char cmd, const char * message, uint8_t * symbols) { @@ -1637,6 +1643,14 @@ void JTEncode::rs_encode(uint8_t * data, uint8_t * symbols) uint8_t sym[JT65_ENCODE_COUNT]; uint8_t i; + // The RS codec failed to initialize (e.g. allocation failure). Do not + // dereference the null codec; emit a deterministic zero table instead. + if(rs_inst == NULL) + { + memset(symbols, 0, JT65_ENCODE_COUNT); + return; + } + // Reverse data order for the Karn codec. for(i = 0; i < 12; i++) { From bf2b0c374bf11f25224fbd3399d0dd9bb6667009 Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:15:06 +0000 Subject: [PATCH 4/7] Add portable PROGMEM shim; build on all architectures 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 (tables stay in flash), cores that ship their own pgmspace.h (ESP8266, ESP32) include it, and everything else falls back to plain memory. --- README.md | 4 ++++ src/JTEncode.cpp | 4 +--- src/JTEncode.h | 4 +--- src/JTEncode_pgmspace.h | 37 +++++++++++++++++++++++++++++++++++++ src/generator.h | 4 +--- 5 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/JTEncode_pgmspace.h diff --git a/README.md b/README.md index d17f1e1..b83ae4a 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,10 @@ Changelog * Correct FSQ symbols-buffer contract in docs (2 symbols per char + terminator) * Guard rs_encode against a NULL Reed-Solomon codec (allocation failure) * Document that characters with no entry in the FSQ table are silently dropped + * Portable PROGMEM shim: library now builds on all architectures + (architectures=*), not just ATmega328P/168/32U4/16U4 — AVR chips keep + tables in flash, other cores use their own pgmspace.h when available, + otherwise plain memory * Correct stale README references (ft8_encode example call, jt4_encode symbol count, wspr_encode dbm type) diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index 140f72e..5417c48 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -32,9 +32,7 @@ #include #include -#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) -#include -#endif +#include "JTEncode_pgmspace.h" #include "Arduino.h" diff --git a/src/JTEncode.h b/src/JTEncode.h index 7cdf7ea..5cb4e11 100644 --- a/src/JTEncode.h +++ b/src/JTEncode.h @@ -32,9 +32,7 @@ #include -#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) -#include -#endif +#include "JTEncode_pgmspace.h" #define JT65_SYMBOL_COUNT 126 #define JT9_SYMBOL_COUNT 85 diff --git a/src/JTEncode_pgmspace.h b/src/JTEncode_pgmspace.h new file mode 100644 index 0000000..42d206c --- /dev/null +++ b/src/JTEncode_pgmspace.h @@ -0,0 +1,37 @@ +/* + * JTEncode_pgmspace.h - portable PROGMEM / pgm_read_byte shim + * + * The tables in this library (fsq_code_table, crc8_table, jt9i, generator_bits) + * are kept in flash on AVR so they do not consume RAM. On every other + * architecture, keeping them in flash is not required (most 32-bit boards have + * far more RAM than the ~2.5 KB of tables), so we fall back to plain memory. + * + * Selection logic: + * 1. Any AVR chip -> (real flash path) + * 2. Core provides -> (ESP8266, ESP32, etc. — real flash + * pgmspace.h path with their own PROGMEM/pgm_read_byte) + * 3. Otherwise -> plain-RAM fallback (host builds, unknown cores) + * + * This makes the library build on all architectures listed in + * library.properties (architectures=*), not just the AVR chips that were + * originally hard-coded in the include guards. + */ + +#ifndef JTENCODE_PGMSPACE_H +#define JTENCODE_PGMSPACE_H + +#if defined(__AVR__) + #include +#elif defined(__has_include) + #if __has_include() + #include + #else + #define PROGMEM + #define pgm_read_byte(x) (*(const uint8_t *)(x)) + #endif +#else + #define PROGMEM + #define pgm_read_byte(x) (*(const uint8_t *)(x)) +#endif + +#endif /* JTENCODE_PGMSPACE_H */ diff --git a/src/generator.h b/src/generator.h index bf2b258..54e8d28 100644 --- a/src/generator.h +++ b/src/generator.h @@ -3,9 +3,7 @@ #include -#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega16U4__) -#include -#endif +#include "JTEncode_pgmspace.h" const uint8_t generator_bits[83][12] PROGMEM = { From 42b85c604242994b4c3eefa79d50b8f7cd6f00da Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:20:38 +0000 Subject: [PATCH 5/7] Close harness gaps; polish example and keywords - 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 --- README.md | 1 + examples/Si5351JTDemo/Si5351JTDemo.ino | 8 ++++--- keywords.txt | 1 + src/JTEncode_pgmspace.h | 14 ++++++++--- tests/harness.cpp | 33 ++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b83ae4a..77c6846 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ Changelog --------- * v1.4.0 + * Added test harness (15 checks) covering memory-safety and output-correctness bugs * Added ~JTEncode() destructor that frees the Reed-Solomon instance allocated by the constructor * Initialize callsign/locator/power fields in the constructor (previously left uninitialized) * Fix memory-safety bugs: bounded copies in jt65/jt9/jt4/ft8 message paths, fsq/fsq_dir diff --git a/examples/Si5351JTDemo/Si5351JTDemo.ino b/examples/Si5351JTDemo/Si5351JTDemo.ino index c03baff..699dd6c 100644 --- a/examples/Si5351JTDemo/Si5351JTDemo.ino +++ b/examples/Si5351JTDemo/Si5351JTDemo.ino @@ -86,8 +86,10 @@ unsigned long freq; char message[] = "N0CALL AA00"; char call[] = "N0CALL"; char loc[] = "AA00"; -uint8_t dbm = 27; -uint8_t tx_buffer[255]; +int8_t dbm = 27; +// Sized for fsq_dir_encode's documented maximum (2 symbols per char + terminator); +// also covers jt4_encode (207 symbols) and every other mode. +uint8_t tx_buffer[320]; enum mode cur_mode = DEFAULT_MODE; uint8_t symbol_count; uint16_t tone_delay, tone_spacing; @@ -125,7 +127,7 @@ void encode() void set_tx_buffer() { // Clear out the transmit buffer - memset(tx_buffer, 0, 255); + memset(tx_buffer, 0, 320); // Set the proper frequency and timer CTC depending on mode switch(cur_mode) diff --git a/keywords.txt b/keywords.txt index ad95bb5..444512d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,6 +7,7 @@ wspr_encode KEYWORD2 ft8_encode KEYWORD2 fsq_encode KEYWORD2 fsq_dir_encode KEYWORD2 +latlon_to_grid KEYWORD2 JT65_SYMBOL_COUNT LITERAL1 JT9_SYMBOL_COUNT LITERAL1 diff --git a/src/JTEncode_pgmspace.h b/src/JTEncode_pgmspace.h index 42d206c..2fb8ff3 100644 --- a/src/JTEncode_pgmspace.h +++ b/src/JTEncode_pgmspace.h @@ -26,12 +26,20 @@ #if __has_include() #include #else + #ifndef PROGMEM + #define PROGMEM + #endif + #ifndef pgm_read_byte + #define pgm_read_byte(x) (*(const uint8_t *)(x)) + #endif + #endif +#else + #ifndef PROGMEM #define PROGMEM + #endif + #ifndef pgm_read_byte #define pgm_read_byte(x) (*(const uint8_t *)(x)) #endif -#else - #define PROGMEM - #define pgm_read_byte(x) (*(const uint8_t *)(x)) #endif #endif /* JTENCODE_PGMSPACE_H */ diff --git a/tests/harness.cpp b/tests/harness.cpp index 7100fca..39df984 100644 --- a/tests/harness.cpp +++ b/tests/harness.cpp @@ -58,6 +58,37 @@ static int test_fsq_encode_overflow() return 0; } +static int test_jt4_strcpy_overflow() +{ + JTEncode enc; + char msg[20]; + memset(msg, 'A', 19); msg[19] = 0; + uint8_t symbols[JT4_SYMBOL_COUNT]; + enc.jt4_encode(msg, symbols); + return 0; +} + +static int test_rs_inst_null_guard() +{ + JTEncode enc; + enc.free_rs_int(enc.rs_inst); // free the real codec, then simulate init failure + enc.rs_inst = NULL; + uint8_t symbols[JT65_SYMBOL_COUNT]; + memset(symbols, 0xFF, sizeof(symbols)); + enc.jt65_encode("NT7S", symbols); + // The RS guard must emit a deterministic zero table and the encode must + // complete; a crash here is caught by the child exit status. + if (symbols[0] != 0) { + printf("symbols[0] = %u (expected 0)\n", (unsigned)symbols[0]); + return 1; + } + if (symbols[1] != 2) { + printf("symbols[1] = %u (expected 2)\n", (unsigned)symbols[1]); + return 1; + } + return 0; +} + static int test_jt65_strcpy_overflow() { JTEncode enc; @@ -264,6 +295,8 @@ int main() {"jt9_oob", "jt9_packbits reads d[206] out of bounds", test_jt9_oob}, {"fsq_encode_overflow", "fsq_encode sprintf overflows 155-byte buffer", test_fsq_encode_overflow}, {"jt65_strcpy_overflow", "jt65_encode strcpy into 14-byte message buffer", test_jt65_strcpy_overflow}, + {"jt4_strcpy_overflow", "jt4_encode strcpy into 14-byte message buffer", test_jt4_strcpy_overflow}, + {"rs_inst_null_guard", "rs_encode derefs NULL codec when init fails", test_rs_inst_null_guard}, {"ft8_strcpy_overflow", "ft8_encode strcpy into 19-byte message buffer", test_ft8_strcpy_overflow}, {"wspr_callsign_strcpy_overflow", "wspr_encode strcpy into 13-byte call_ buffer", test_wspr_callsign_strcpy_overflow}, {"wspr_type3_missing_close_bracket", "WSPR Type 3 missing '>' - UB strncpy", test_wspr_type3_missing_close_bracket}, From 8843490905f569e02b798488610455c86f52a8bf Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:39:09 +0000 Subject: [PATCH 6/7] Fix ESP32 -Werror builds, 1-char WSPR prefix, 13-char callsigns, FT8 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. --- README.md | 12 +++++++++++- src/JTEncode.cpp | 34 ++++++++++++++-------------------- src/JTEncode.h | 2 +- src/nhash.c | 5 +---- tests/harness.cpp | 36 +++++++++++++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 77c6846..fad1356 100644 --- a/README.md +++ b/README.md @@ -317,7 +317,7 @@ Changelog --------- * v1.4.0 - * Added test harness (15 checks) covering memory-safety and output-correctness bugs + * Added test harness (17 checks) covering memory-safety and output-correctness bugs * Added ~JTEncode() destructor that frees the Reed-Solomon instance allocated by the constructor * Initialize callsign/locator/power fields in the constructor (previously left uninitialized) * Fix memory-safety bugs: bounded copies in jt65/jt9/jt4/ft8 message paths, fsq/fsq_dir @@ -334,6 +334,16 @@ Changelog otherwise plain memory * Correct stale README references (ft8_encode example call, jt4_encode symbol count, wspr_encode dbm type) + * Fix WSPR one-character-prefix callsigns (e.g. F/PE0FKO): the prefix is now + 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) + * Fix variable-callsign length: callsign member widened from 12 to 13 bytes so a + 12-character compound callsign is not truncated + * Fix FT8 telemetry mode: snprintf "%*s" -> "%18s" (broken on Atmel toolchain), + and i0 is now initialized before use + * Fix ESP32 -Werror build failures: removed unused variables (tone, i, k8) and + initialized i0; crc8 loop variable now size_t (sign-compare) * v1.3.1 diff --git a/src/JTEncode.cpp b/src/JTEncode.cpp index 5417c48..47ad861 100644 --- a/src/JTEncode.cpp +++ b/src/JTEncode.cpp @@ -266,7 +266,7 @@ void JTEncode::fsq_encode(const char * from_call, const char * message, uint8_t char tx_buffer[160]; char * tx_message; uint16_t symbol_pos = 0; - uint8_t i, fch, vcode1, vcode2, tone; + uint8_t i, fch, vcode1, vcode2; uint8_t cur_tone = 0; // Clear out the transmit buffer @@ -358,7 +358,7 @@ void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, cons char tx_buffer[160]; char * tx_message; uint16_t symbol_pos = 0; - uint8_t i, fch, vcode1, vcode2, tone, from_call_crc; + uint8_t i, fch, vcode1, vcode2, from_call_crc; uint8_t cur_tone = 0; // Generate a CRC on from_call @@ -447,8 +447,6 @@ void JTEncode::fsq_dir_encode(const char * from_call, const char * to_call, cons */ void JTEncode::ft8_encode(const char * msg, uint8_t * symbols) { - uint8_t i; - char message[19]; memset(message, 0, 19); strncpy(message, msg, 18); @@ -973,7 +971,7 @@ void JTEncode::wspr_bit_packing(uint8_t * c) uint8_t i; // Determine prefix or suffix - if((slash_pos + 2 < 12) && (callsign[slash_pos + 2] == ' ' || callsign[slash_pos + 2] == 0)) + if((slash_pos + 2 < (int)sizeof(callsign)) && (callsign[slash_pos + 2] == ' ' || callsign[slash_pos + 2] == 0)) { // Single character suffix char base_call[7]; @@ -1015,7 +1013,7 @@ void JTEncode::wspr_bit_packing(uint8_t * c) m = (m * 128) + power + 2 + 64; } - else if((slash_pos + 3 < 12) && (callsign[slash_pos + 3] == ' ' || callsign[slash_pos + 3] == 0)) + else if((slash_pos + 3 < (int)sizeof(callsign)) && (callsign[slash_pos + 3] == ' ' || callsign[slash_pos + 3] == 0)) { // Two-digit numerical suffix char base_call[7]; @@ -1049,21 +1047,17 @@ void JTEncode::wspr_bit_packing(uint8_t * c) // Prefix char prefix[4]; char base_call[7]; - memset(prefix, 0, 4); memset(base_call, 0, 7); int prefix_len = slash_pos < 3 ? slash_pos : 3; - strncpy(prefix, callsign, prefix_len); + // Right align the prefix into the 3-character field (pad with spaces) + memset(prefix, ' ', 3); + for(int k = 0; k < prefix_len; k++) + { + prefix[3 - prefix_len + k] = callsign[k]; + } + prefix[3] = 0; strncpy(base_call, callsign + slash_pos + 1, 6); - if(prefix[2] == ' ' || prefix[2] == 0) - { - // Right align prefix - prefix[3] = 0; - prefix[2] = prefix[1]; - prefix[1] = prefix[0]; - prefix[0] = ' '; - } - for(uint8_t i = 0; i < 6; i++) { base_call[i] = toupper(base_call[i]); @@ -1139,7 +1133,7 @@ void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword) memset(qa, 0, 10); memset(qb, 0, 10); - uint8_t i, j, x, i0; + uint8_t i, j, x, i0 = 0; uint32_t ireg = 0; // See if this is a telemetry message @@ -1178,7 +1172,7 @@ void JTEncode::ft8_bit_packing(char* message, uint8_t* codeword) memset(c18, 0, 19); memmove(c18, message, i0 + 1); - snprintf(temp_msg, 19, "%*s", 18, c18); + snprintf(temp_msg, 19, "%18s", c18); // Convert all chars to uppercase for(i = 0; i < strlen(temp_msg); i++) @@ -1677,7 +1671,7 @@ uint8_t JTEncode::crc8(const char * text) uint8_t crc = '\0'; uint8_t ch; - int i; + size_t i; for(i = 0; i < strlen(text); i++) { ch = text[i]; diff --git a/src/JTEncode.h b/src/JTEncode.h index 5cb4e11..8ebad94 100644 --- a/src/JTEncode.h +++ b/src/JTEncode.h @@ -257,7 +257,7 @@ class JTEncode uint8_t crc8(const char *); void pad_callsign(char *); void * rs_inst; - char callsign[12]; + char callsign[13]; char locator[7]; int8_t power; }; diff --git a/src/nhash.c b/src/nhash.c index 8aabb4f..b666297 100644 --- a/src/nhash.c +++ b/src/nhash.c @@ -222,9 +222,6 @@ uint32_t nhash_( const void *key, int *length0, uint32_t *initval0) u.ptr = key; if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ - const uint8_t *k8; - - k8=0; //Silence compiler warning /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ while (length > 12) { @@ -267,7 +264,7 @@ uint32_t nhash_( const void *key, int *length0, uint32_t *initval0) #else /* make valgrind happy */ - k8 = (const uint8_t *)k; + const uint8_t *k8 = (const uint8_t *)k; switch(length) { case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; diff --git a/tests/harness.cpp b/tests/harness.cpp index 39df984..16d9fc3 100644 --- a/tests/harness.cpp +++ b/tests/harness.cpp @@ -202,7 +202,7 @@ static int test_wspr_callsign_not_terminated() uint8_t symbols[WSPR_SYMBOL_COUNT]; enc.wspr_encode("NT7S", "CN85", 30, symbols); bool has_null = false; - for (int i = 0; i < 12; i++) + for (int i = 0; i < (int)sizeof(enc.callsign); i++) if (enc.callsign[i] == 0) has_null = true; if (!has_null) { printf("callsign has no null terminator\n"); @@ -230,6 +230,38 @@ static int test_fsq_dir_over_long_message() return 0; } +static int test_wspr_one_char_prefix() +{ + // Golden symbols for F/PE0FKO (1-char prefix) captured from the fixed code. + // Reference-verified: m = 37*(37*36+36) + nc('F') = 50631 (not the buggy 49875). + static const uint8_t golden[WSPR_SYMBOL_COUNT] = { + 3,1,2,2,2,2,2,0,3,0,2,2,3,3,3,0,0,2,1,0,0,1,0,3,1,3,1,2,0,2,2,0,2,0,1,0,0,3,0,3,2,0,0,0,2,0,1,0,1,1,2,0,1,1,2,3,0,2,2,3,1,2,1,0,0,0,0,1,1,0,3,0,3,2,3,2,1,0,2,1,0,0,1,0,3,1,2,0,2,1,3,0,3,0,1,0,0,0,1,0,0,0,0,2,1,0,2,1,2,2,3,1,1,0,3,3,2,2,3,3,2,3,2,2,0,1,3,1,2,0,2,2,2,3,2,3,2,0,3,1,0,2,0,0,2,2,2,1,1,0,3,0,1,3,0,2,0,3,3,0,2,0 + }; + JTEncode enc; + uint8_t symbols[WSPR_SYMBOL_COUNT]; + enc.wspr_encode("F/PE0FKO", "CN85", 30, symbols); + for (int i = 0; i < WSPR_SYMBOL_COUNT; i++) { + if (symbols[i] != golden[i]) { + printf("symbols[%d] = %d (expected %d)\n", i, (int)symbols[i], (int)golden[i]); + return 1; + } + } + return 0; +} + +static int test_wspr_callsign_13_char() +{ + // 12-char callsign must fit the 13-byte member without truncation. + JTEncode enc; + uint8_t symbols[WSPR_SYMBOL_COUNT]; + enc.wspr_encode("ABCDEFGHIJKL", "CN85", 30, symbols); + if (strcmp(enc.callsign, "ABCDEFGHIJKL") != 0) { + printf("callsign = '%s' (expected 'ABCDEFGHIJKL')\n", enc.callsign); + return 1; + } + return 0; +} + // --------------------------------------------------------------------------- // Runner // --------------------------------------------------------------------------- @@ -306,6 +338,8 @@ int main() {"wspr_invalid_power_uninitialized", "wspr_message_prep: uninit for dbm<-30 + wrong rounding", test_wspr_invalid_power_uninitialized}, {"wspr_callsign_not_terminated", "wspr_encode leaves callsign member unterminated", test_wspr_callsign_not_terminated}, {"rs_inst_leak", "constructor allocates rs_inst; no destructor frees it", test_rs_inst_leak}, + {"wspr_one_char_prefix", "WSPR 1-char prefix right-aligned into 3-char field", test_wspr_one_char_prefix}, + {"wspr_callsign_13_char", "WSPR 12-char callsign truncated to 11-char member", test_wspr_callsign_13_char}, {"fsq_dir_over_long_message", "fsq_dir_encode sprintf overflow (bonus, beyond max)", test_fsq_dir_over_long_message}, }; From dfa88dcd671bc78cbf9543dfeedac03d2166b567 Mon Sep 17 00:00:00 2001 From: Jason Milldrum Date: Sat, 28 Nov 2026 03:45:09 +0000 Subject: [PATCH 7/7] Annotate changelog entries with issue numbers --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fad1356..222c49e 100644 --- a/README.md +++ b/README.md @@ -324,7 +324,7 @@ Changelog snprintf + buffer sized for documented maximum, bounded WSPR Type 2/3 reads, zero-pad the 207th bit in jt9_packbits, defined fallback for a missing '>' in Type 3 WSPR messages * Fix latlon_to_grid() not terminating ret_grid - * Fix wspr_message_prep(): clamp dbm to [-30, 60] and no longer mutate the caller's buffers + * Fix wspr_message_prep(): clamp dbm to [-30, 60] and no longer mutate the caller's buffers (#35) * Correct FSQ symbols-buffer contract in docs (2 symbols per char + terminator) * Guard rs_encode against a NULL Reed-Solomon codec (allocation failure) * Document that characters with no entry in the FSQ table are silently dropped @@ -334,15 +334,15 @@ Changelog otherwise plain memory * Correct stale README references (ft8_encode example call, jt4_encode symbol count, wspr_encode dbm type) - * Fix WSPR one-character-prefix callsigns (e.g. F/PE0FKO): the prefix is now + * Fix WSPR one-character-prefix callsigns (e.g. F/PE0FKO) (#29): the prefix is now 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) - * Fix variable-callsign length: callsign member widened from 12 to 13 bytes so a + * Fix variable-callsign length (#28): callsign member widened from 12 to 13 bytes so a 12-character compound callsign is not truncated - * Fix FT8 telemetry mode: snprintf "%*s" -> "%18s" (broken on Atmel toolchain), + * Fix FT8 telemetry mode (#18): snprintf "%*s" -> "%18s" (broken on Atmel toolchain), and i0 is now initialized before use - * Fix ESP32 -Werror build failures: removed unused variables (tone, i, k8) and + * Fix ESP32 -Werror build failures (#33): removed unused variables (tone, i, k8) and initialized i0; crc8 loop variable now size_t (sign-compare) * v1.3.1