diff --git a/examples/simple_repeater/FleetManager.cpp b/examples/simple_repeater/FleetManager.cpp index efc8d9f4..626b8c7c 100644 --- a/examples/simple_repeater/FleetManager.cpp +++ b/examples/simple_repeater/FleetManager.cpp @@ -279,16 +279,18 @@ bool FleetManager::checkForward(const mesh::Packet* packet) { // ---------------------------------------------------------------- channels int FleetManager::appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[], - int max_matches) { + int max_matches, int filled) { if (!enabled || psk_len == 0) return 0; + if (filled >= max_matches) return 0; if (chan_hash != hash[0]) return 0; // the filter store may hold the same PSK: handing core the same secret twice - // would burn one of its few candidate slots - for (int i = 0; i < max_matches; i++) { + // would burn one of its few candidate slots. Only the filled prefix of dest + // is initialised (core's caller passes raw stack), so only it is compared. + for (int i = 0; i < filled; i++) { if (memcmp(dest[i].secret, psk, sizeof(psk)) == 0) return 0; } - dest[0].hash[0] = chan_hash; - memcpy(dest[0].secret, psk, sizeof(dest[0].secret)); + dest[filled].hash[0] = chan_hash; + memcpy(dest[filled].secret, psk, sizeof(dest[filled].secret)); return 1; } diff --git a/examples/simple_repeater/FleetManager.h b/examples/simple_repeater/FleetManager.h index fe42f9eb..e8b98be0 100644 --- a/examples/simple_repeater/FleetManager.h +++ b/examples/simple_repeater/FleetManager.h @@ -195,10 +195,13 @@ public: void runScripts(CliExecFn fn, void* exec_ctx); // Keyed-channel supply for the core's group decryption: offers the fleet - // channel when its hash matches, skipping a secret already in `dest` (the - // filter store may hold the same PSK; a duplicate would burn one of core's - // few candidate slots). Returns how many entries were appended. - int appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches); + // channel when its hash matches, skipping a secret already in the `filled` + // leading entries of `dest` (the filter store may hold the same PSK; a + // duplicate would burn one of core's few candidate slots — and only that + // prefix is initialised, core hands over raw stack for the rest). Appends + // at dest[filled]; returns how many entries were appended. + int appendChannelByHash(const uint8_t* hash, mesh::GroupChannel dest[], int max_matches, + int filled); void markDirty() { save_flag.markDirty(); } diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 08abe952..d4c71f93 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -473,7 +473,7 @@ int MyMesh::searchChannelsByHash(const uint8_t* hash, mesh::GroupChannel channel // merge: the filter's keyed-channel store plus the fleet channel (the same // PSK in both is skipped by the fleet side; any matching key decrypts) int n = filter.searchChannelsByHash(hash, channels, max_matches); - if (n < max_matches) n += fleet.appendChannelByHash(hash, channels + n, max_matches - n); + if (n < max_matches) n += fleet.appendChannelByHash(hash, channels, max_matches, n); return n; } diff --git a/test/test_fleet/test_fleet.cpp b/test/test_fleet/test_fleet.cpp index 33c22359..b8103d21 100644 --- a/test/test_fleet/test_fleet.cpp +++ b/test/test_fleet/test_fleet.cpp @@ -1111,20 +1111,31 @@ TEST_F(FleetHooksTest, CheckForwardExemptsOnlyFleetChannelGroupPackets) { TEST_F(FleetHooksTest, AppendChannelByHash) { mesh::GroupChannel dest[4]; memset(dest, 0, sizeof(dest)); - // hash match: the fleet channel is offered, deduped against what the filter - // store already handed over + // hash match: the fleet channel is offered, deduped against the entries the + // filter store already handed over (the filled prefix) uint8_t hash[1] = { chan.hash[0] }; memcpy(dest[0].secret, chan.secret, PUB_KEY_SIZE); - EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 0); // same secret already present + EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 1), 0); // same secret already present memset(dest, 0, sizeof(dest)); - EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 1); + EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 0), 1); EXPECT_EQ(dest[0].hash[0], chan.hash[0]); EXPECT_EQ(memcmp(dest[0].secret, chan.secret, PUB_KEY_SIZE), 0); + // a different secret with the same hash is appended at dest[filled], after + // the filter's entries — never over them + mesh::GroupChannel mixed[4]; + memset(mixed, 0, sizeof(mixed)); + memset(mixed[0].secret, 0xEE, PUB_KEY_SIZE); + EXPECT_EQ(fleet.appendChannelByHash(hash, mixed, 4, 1), 1); + EXPECT_EQ(mixed[1].hash[0], chan.hash[0]); + EXPECT_EQ(memcmp(mixed[1].secret, chan.secret, PUB_KEY_SIZE), 0); + EXPECT_EQ(mixed[0].secret[0], 0xEE); // the earlier entry is untouched + // store full: nothing offered + EXPECT_EQ(fleet.appendChannelByHash(hash, mixed, 4, 4), 0); // no hash match, or hooks idle: nothing offered uint8_t other_hash[1] = { (uint8_t)(chan.hash[0] ^ 0xFF) }; - EXPECT_EQ(fleet.appendChannelByHash(other_hash, dest, 4), 0); + EXPECT_EQ(fleet.appendChannelByHash(other_hash, dest, 4, 0), 0); fleet.setEnabled(false); - EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4), 0); + EXPECT_EQ(fleet.appendChannelByHash(hash, dest, 4, 0), 0); } // ------------------------------------------------------------------ CLI