From 29a65aa13d492af561debf9ae786f8469a93fc67 Mon Sep 17 00:00:00 2001 From: Benjamin Faershtein <119711889+RCGV1@users.noreply.github.com> Date: Wed, 9 Sep 2026 12:27:20 +0000 Subject: [PATCH] fix(mesh): use valid default packet history size (#11786) --- src/mesh/PacketHistory.cpp | 12 ++++++----- src/mesh/PacketHistory.h | 2 +- test/test_packet_history/test_main.cpp | 29 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/mesh/PacketHistory.cpp b/src/mesh/PacketHistory.cpp index da745a25e7..3efeee2a75 100644 --- a/src/mesh/PacketHistory.cpp +++ b/src/mesh/PacketHistory.cpp @@ -17,14 +17,16 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0) // Initialize members { if (size < 4 || size > PACKETHISTORY_MAX) { // Copilot suggested - makes sense - LOG_WARN("Packet History - Invalid size %d, using default %d", size, PACKETHISTORY_MAX); + LOG_WARN("Packet History - Invalid size %u, using default %u", static_cast(size), + static_cast(PACKETHISTORY_MAX)); size = PACKETHISTORY_MAX; // Use default size if invalid } #if !MESHTASTIC_EXCLUDE_PKT_HISTORY_HASH // Ensure capacity fits in uint16_t hash index (HASH_EMPTY = 0xFFFF is the sentinel) if (size >= HASH_EMPTY) { - LOG_WARN("Packet History - Clamping size %d to %d (hash index limit)", size, HASH_EMPTY - 1); + LOG_WARN("Packet History - Clamping size %u to %u (hash index limit)", static_cast(size), + static_cast(HASH_EMPTY - 1)); size = HASH_EMPTY - 1; } #endif @@ -33,7 +35,7 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0) // Initia recentPacketsCapacity = size; recentPackets.reset(new PacketRecord[recentPacketsCapacity]); if (!recentPackets) { // No logging here, console/log probably uninitialized yet. - LOG_ERROR("Packet History - Memory allocation failed for size=%d entries / %d Bytes", size, + LOG_ERROR("Packet History - Memory allocation failed for size=%u entries / %zu Bytes", static_cast(size), sizeof(PacketRecord) * recentPacketsCapacity); recentPacketsCapacity = 0; // mark allocation fail return; // return early @@ -49,7 +51,7 @@ PacketHistory::PacketHistory(uint32_t size) : recentPacketsCapacity(0) // Initia hashMask = hashCapacity - 1; hashIndex.reset(new uint16_t[hashCapacity]); if (!hashIndex) { - LOG_ERROR("Packet History - Hash index allocation failed for %d entries", hashCapacity); + LOG_ERROR("Packet History - Hash index allocation failed for %u entries", static_cast(hashCapacity)); hashCapacity = 0; hashMask = 0; return; @@ -601,4 +603,4 @@ inline uint8_t PacketHistory::getOurTxHopLimit(const PacketRecord &r) inline void PacketHistory::setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit) { r.hop_limit = (r.hop_limit & ~HOP_LIMIT_OUR_TX_MASK) | ((hopLimit << HOP_LIMIT_OUR_TX_SHIFT) & HOP_LIMIT_OUR_TX_MASK); -} \ No newline at end of file +} diff --git a/src/mesh/PacketHistory.h b/src/mesh/PacketHistory.h index 84af59874e..42359e5808 100644 --- a/src/mesh/PacketHistory.h +++ b/src/mesh/PacketHistory.h @@ -68,7 +68,7 @@ class PacketHistory void setOurTxHopLimit(PacketRecord &r, uint8_t hopLimit); public: - explicit PacketHistory(uint32_t size = -1); // Constructor with size parameter, default is PACKETHISTORY_MAX + explicit PacketHistory(uint32_t size = PACKETHISTORY_MAX); /** * Update recentBroadcasts and return true if we have already seen this packet diff --git a/test/test_packet_history/test_main.cpp b/test/test_packet_history/test_main.cpp index 913417f4d2..bfce175df2 100644 --- a/test/test_packet_history/test_main.cpp +++ b/test/test_packet_history/test_main.cpp @@ -10,8 +10,11 @@ #include "PacketHistory.h" +#include "SerialConsole.h" #include "TestUtil.h" +#include #include +#include // --------------------------------------------------------------------------- // Constants @@ -25,6 +28,18 @@ static constexpr uint32_t SMALL_CAPACITY = 8; // --------------------------------------------------------------------------- static PacketHistory *ph = nullptr; +class RecordingPrint : public Print +{ + public: + size_t write(uint8_t value) override + { + output.push_back(value); + return 1; + } + + std::vector output; +}; + // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- @@ -65,6 +80,19 @@ void test_init_valid_size(void) TEST_ASSERT_TRUE(h.initOk()); } +void test_init_default_size_does_not_warn(void) +{ + RecordingPrint sink; + console->setDestination(&sink); + + PacketHistory h; + + console->setDestination(&Serial); + const std::string output(sink.output.begin(), sink.output.end()); + TEST_ASSERT_TRUE(h.initOk()); + TEST_ASSERT_NULL(strstr(output.c_str(), "Packet History - Invalid size")); +} + void test_init_minimum_size(void) { PacketHistory h(4); @@ -741,6 +769,7 @@ void setup() // Group 1 - Initialization RUN_TEST(test_init_valid_size); + RUN_TEST(test_init_default_size_does_not_warn); RUN_TEST(test_init_minimum_size); RUN_TEST(test_init_too_small_falls_back);