From d12eefc7bc19fb4da615b1b45c1235899f2e4fb1 Mon Sep 17 00:00:00 2001 From: Andy Fingerhut Date: Tue, 18 Feb 2025 21:18:38 -0500 Subject: [PATCH] Fix #1291 Do not assert when --dump-packet-data option is enabled ... (#1293) * Fix #1291 Do not assert when --dump-packet-data option is enabled ... ... when sending a length 0 packet. Granted this is an unusual situation, but there are test programs for p4c and p4testgen where p4testgen creates tests where the expected output packet is 0 bytes long, and if you run them while enabling --dump-packet-data N option for behavioral-model, simple_switch crashes with an assert. Signed-off-by: Andy Fingerhut * Defensive programming Signed-off-by: Andy Fingerhut * Address review commments. Signed-off-by: Andy Fingerhut --------- Signed-off-by: Andy Fingerhut --- src/bm_sim/dev_mgr.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bm_sim/dev_mgr.cpp b/src/bm_sim/dev_mgr.cpp index 2ecc5b6d7..fcc9c1059 100644 --- a/src/bm_sim/dev_mgr.cpp +++ b/src/bm_sim/dev_mgr.cpp @@ -282,7 +282,9 @@ DevMgr::clear_port_stats(port_t port_num) { std::string DevMgr::sample_packet_data(const char *buffer, int len) { size_t amount = std::min(dump_packet_data, static_cast(len)); - assert(amount > 0); + if (amount == 0) { + return ""; + } std::ostringstream ret; utils::dump_hexstring(ret, &buffer[0], &buffer[amount]); return ret.str();