mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
fix(filter): undefined chan_mask shift when deleting the last channel of a full store
delChannel() remaps each rule's chan_mask with (chan_mask >> (idx + 1)) << idx. The v7 branch widened chan_mask to 32 bits and the store to 32 channels, so deleting the last channel of a full store shifts by exactly 32 — undefined behavior. On x86 (variable shift counts mask to 5 bits) a rule naming channel 0 came out of the delete with a spurious bit 31 set instead. Guard the shift: at idx == FILTER_MAX_CHANNELS - 1 nothing sits above the deleted channel, so the high contribution is simply zero. Regression test covers the full-store edge (mask integrity plus a behavioral re-check that the surviving named channel still matches).
This commit is contained in:
1 parent
62373cad30
commit
c3a7cb99f3
3 files changed
+30
-3
No files matched your search
@@ -59,7 +59,7 @@ commit** — the guides are user-facing API documentation, not optional docs.
|
||||
dispatch, lazy-save loop).
|
||||
- `main.cpp` — serial CLI entry (upstream + fork's buffer-hardening lines).
|
||||
|
||||
**Tests:** `test/test_packet_filter/` (294 behavior-level cases: matching,
|
||||
**Tests:** `test/test_packet_filter/` (295 behavior-level cases: matching,
|
||||
content rules, limiter, persistence upgrades, CLI surface, TinyRegex,
|
||||
PatternMatch) and `test/test_battery_gate/`. Test-only shims: `NativeShim.h`,
|
||||
`NativeTestStubs.cpp`, `RegionMapStub.cpp`, `FilterTestHelpers.h`.
|
||||
@@ -218,7 +218,7 @@ FILTER.md:
|
||||
paths, added in the same commit. The suites are behavior-level (native
|
||||
googletest); reach them via the same CLI/`checkPacket`/`checkContent`
|
||||
entry points a user or the firmware would.
|
||||
- Pure refactors keep both suites green **unchanged** — the suites (294 filter,
|
||||
- Pure refactors keep both suites green **unchanged** — the suites (295 filter,
|
||||
44 battery) are the safety net that proves no behavior slipped.
|
||||
- Run both suites, then re-read the diff:
|
||||
|
||||
|
||||
@@ -251,7 +251,9 @@ void FilterRules::delChannel(int idx) {
|
||||
for (int i = 0; i < num_rules; i++) {
|
||||
FilterRule* r = &rules[i];
|
||||
uint32_t lo = r->chan_mask & (uint32_t)((1u << idx) - 1);
|
||||
uint32_t hi = (r->chan_mask >> (idx + 1)) << idx;
|
||||
// a full store deletes at idx == FILTER_MAX_CHANNELS - 1: nothing sits
|
||||
// above it, and chan_mask >> 32 would be an undefined shift
|
||||
uint32_t hi = (idx + 1 < FILTER_MAX_CHANNELS) ? ((r->chan_mask >> (idx + 1)) << idx) : 0;
|
||||
r->chan_mask = lo | hi;
|
||||
}
|
||||
markDirty();
|
||||
|
||||
@@ -4350,6 +4350,31 @@ TEST_F(FilterTest, ChanDelRemapsRuleMasks) {
|
||||
FILTER_ACT_DROP);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, ChanDelRemapsMasksAtTheWidenedStoreEdge) {
|
||||
// chan_mask is 32 bits wide and the store can hold 32 channels: deleting the
|
||||
// LAST channel of a full store shifts by idx+1 == 32, which must yield no
|
||||
// "bits above" contribution at all (a naive chan_mask >> 32 is undefined)
|
||||
for (int i = 0; i < FILTER_MAX_CHANNELS - 1; i++) {
|
||||
ASSERT_EQ(cli(filter, ("chan add #c" + std::to_string(i)).c_str()).substr(0, 3), "OK ");
|
||||
}
|
||||
ASSERT_EQ(filter.getNumChannels(), FILTER_MAX_CHANNELS);
|
||||
int last = filter.indexOfChannel("#c30");
|
||||
int first = filter.indexOfChannel("Public");
|
||||
ASSERT_EQ(last, FILTER_MAX_CHANNELS - 1);
|
||||
ASSERT_EQ(first, 0);
|
||||
ASSERT_EQ(cli(filter, "add chan=Public,#c30"), "OK - rule 0 added");
|
||||
EXPECT_EQ(filter.getRule(0)->chan_mask, (1u << first) | (1u << last));
|
||||
|
||||
ASSERT_EQ(cli(filter, "chan del #c30"), "OK - chan #c30 deleted");
|
||||
EXPECT_EQ(filter.getRule(0)->chan_mask, 1u << first); // exactly Public, nothing above
|
||||
|
||||
// behaviourally: the rule still matches the surviving named channel
|
||||
auto pkt = makePacket(ROUTE_TYPE_FLOOD, PAYLOAD_TYPE_GRP_TXT, 10);
|
||||
EXPECT_EQ(filter.checkContent(&pkt, PAYLOAD_TYPE_GRP_TXT, channelFromStore(filter, first),
|
||||
nullptr, 0, nullptr),
|
||||
FILTER_ACT_DROP);
|
||||
}
|
||||
|
||||
TEST_F(FilterTest, ChanListStaysWithinReplyBuffer) {
|
||||
// real CLI reply buffers are 160 B (serial/ethernet reply[160]; mesh path is
|
||||
// &temp[5] of temp[166] — CliUtil.h caps radd() at CLI_REPLY_MAX for this);
|
||||
|
||||
Reference in new issue
Block a user