diff --git a/examples/simple_repeater/ChanListUtil.h b/examples/simple_repeater/ChanListUtil.h index 5949748d..9567f5c7 100644 --- a/examples/simple_repeater/ChanListUtil.h +++ b/examples/simple_repeater/ChanListUtil.h @@ -71,6 +71,8 @@ static int chan_list_add(char* list, int cap, const char* name, int name_len) { // Remove the (case-insensitively) matching entry. Returns false when absent. static bool chan_list_del(char* list, const char* name) { while (*name == ' ') name++; + int name_len = strlen(name); + while (name_len > 0 && name[name_len - 1] == ' ') name_len--; // trim trailing too (RF pads) char out[512]; // callers' lists are far smaller (filter_deny is 128 B) int out_len = 0; bool removed = false; @@ -86,7 +88,7 @@ static bool chan_list_del(char* list, const char* name) { while (elen > 0 && *e == ' ') { e++; elen--; } while (elen > 0 && e[elen - 1] == ' ') elen--; bool match = false; - if ((int)strlen(name) == elen) { + if (name_len == elen) { int i = 0; while (i < elen && tolower((unsigned char)name[i]) == tolower((unsigned char)e[i])) i++; match = (i == elen); diff --git a/examples/simple_repeater/ChannelFilter.h b/examples/simple_repeater/ChannelFilter.h index 26c0343b..19b01bec 100644 --- a/examples/simple_repeater/ChannelFilter.h +++ b/examples/simple_repeater/ChannelFilter.h @@ -47,7 +47,7 @@ bool MyMesh::filterHandleCommand(const char* command, char* reply, int reply_max int rc = chan_list_add(_prefs.filter_deny, sizeof(_prefs.filter_deny), name, strlen(name)); if (rc == CHAN_LIST_OK) { savePrefs(); - sprintf(reply, "OK - deny: %s", _prefs.filter_deny); + snprintf(reply, reply_max, "OK - deny: %s", _prefs.filter_deny); } else if (rc == CHAN_LIST_DUP) { strcpy(reply, "Err - already listed"); } else if (rc == CHAN_LIST_FULL) { diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 2e7aec59..ccbc17ad 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -738,7 +738,7 @@ bool MyMesh::fairnessHandleCommand(const char *command, char *reply, int reply_m if (strcmp(arg, "on") == 0 || strcmp(arg, "off") == 0) { _prefs.fairness_enabled = (arg[1] == 'n') ? 1 : 0; savePrefs(); - sprintf(reply, "OK - fairness %s", _prefs.fairness_enabled ? "on" : "off"); + snprintf(reply, reply_max, "OK - fairness %s", _prefs.fairness_enabled ? "on" : "off"); return true; } @@ -749,7 +749,9 @@ bool MyMesh::fairnessHandleCommand(const char *command, char *reply, int reply_m return true; } - if (memcmp(arg, "cap ", 4) == 0) { + // strncmp, not memcmp: for the bare `fairness` command arg is the 1-byte "" + // literal, and memcmp(arg, "cap ", 4) would over-read it (UB, trips ASAN). + if (strncmp(arg, "cap ", 4) == 0) { int g = 0, s = 0, a = 0; if (sscanf(arg + 4, "%d %d %d", &g, &s, &a) != 3 || g < 0 || s < 0 || a < 0 || g > 255 || s > 255 || a > 255) { strcpy(reply, "Err - fairness cap (0=default)"); @@ -762,13 +764,13 @@ bool MyMesh::fairnessHandleCommand(const char *command, char *reply, int reply_m fairness_limiter.setSenderNormalCap(_prefs.fair_sender_cap); fairness_limiter.setSenderLowCap(_prefs.fair_advert_cap); savePrefs(); - sprintf(reply, "OK - cap group %u sender %u advert %u", + snprintf(reply, reply_max, "OK - cap group %u sender %u advert %u", (uint32_t)fairness_limiter.groupCap(), (uint32_t)fairness_limiter.senderNormalCap(), (uint32_t)fairness_limiter.senderLowCap()); return true; } - if (memcmp(arg, "refill ", 7) == 0) { + if (strncmp(arg, "refill ", 7) == 0) { int g = 0, s = 0, a = 0; if (sscanf(arg + 7, "%d %d %d", &g, &s, &a) != 3 || g < 0 || s < 0 || a < 0 || g > 65535 || s > 65535 || a > 65535) { strcpy(reply, "Err - fairness refill (0=default)"); @@ -782,7 +784,7 @@ bool MyMesh::fairnessHandleCommand(const char *command, char *reply, int reply_m next_fair_sender_normal_refill = futureMillis(fairSenderRefillMs()); next_fair_sender_low_refill = futureMillis(fairAdvertRefillMs()); savePrefs(); - sprintf(reply, "OK - refill group %us sender %us advert %us", + snprintf(reply, reply_max, "OK - refill group %us sender %us advert %us", (uint32_t)(fairGroupRefillMs() / 1000), (uint32_t)(fairSenderRefillMs() / 1000), (uint32_t)(fairAdvertRefillMs() / 1000)); return true; diff --git a/src/helpers/FairnessLimiter.cpp b/src/helpers/FairnessLimiter.cpp index 129feda8..93c4e3d8 100644 --- a/src/helpers/FairnessLimiter.cpp +++ b/src/helpers/FairnessLimiter.cpp @@ -187,9 +187,11 @@ static int fmt_deny_cat(char* out, int max_len, const char* label, } int FairnessLimiter::formatGrid(char* out, int max_len) const { + if (max_len <= 0) return 0; // Work on scratch copies so the busiest-first selection can null entries as - // it consumes them without disturbing the live counters. - static uint16_t g[GROUP_MAP_SIZE], sn[SENDER_NORMAL_MAP_SIZE], sl[SENDER_LOW_MAP_SIZE]; + // it consumes them without disturbing the live counters. Plain locals (~448 B + // on the stack) -- called only from the main-loop CLI path, never an ISR. + uint16_t g[GROUP_MAP_SIZE], sn[SENDER_NORMAL_MAP_SIZE], sl[SENDER_LOW_MAP_SIZE]; memcpy(g, group_deny, sizeof(g)); memcpy(sn, sender_normal_deny, sizeof(sn)); memcpy(sl, sender_low_deny, sizeof(sl)); diff --git a/test/test_chan_list/test_chan_list.cpp b/test/test_chan_list/test_chan_list.cpp index 1eb7b4ec..02eb3d30 100644 --- a/test/test_chan_list/test_chan_list.cpp +++ b/test/test_chan_list/test_chan_list.cpp @@ -117,6 +117,13 @@ TEST(ChanListDel, AbsentEntryLeavesListAlone) { EXPECT_STREQ(list, "a,b"); } +// RF-padded `filter del Bob ` (trailing space) must still match, like add trims. +TEST(ChanListDel, TrimsTrailingSpaceOfName) { + char list[128] = "Alice,Bob,Carol"; + EXPECT_TRUE(chan_list_del(list, " Bob ")); + EXPECT_STREQ(list, "Alice,Carol"); +} + // ── round trip: what add put in, has finds and del removes ─────────────────── TEST(ChanList, RoundTrip) {