From 76968c4d799bb54ae2c9b6d2fd0861af49d56b89 Mon Sep 17 00:00:00 2001 From: MrAlders0n Date: Sun, 22 Feb 2026 21:11:24 -0500 Subject: [PATCH] fix: wrap error in JSON and add reply buffer guard --- examples/simple_repeater/MyMesh.cpp | 2 +- src/helpers/CommonCLI.h | 2 +- src/helpers/sensors/EnvironmentSensorManager.cpp | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 80b109721..1ba972e28 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1021,7 +1021,7 @@ void MyMesh::formatPacketStatsReply(char *reply) { void MyMesh::formatExtPowerStatsReply(char *reply) { if (!sensors.formatExtPowerStats(reply)) { - strcpy(reply, "No external power monitoring board detected"); + strcpy(reply, "{\"err\":\"No external power monitoring board detected\"}"); } } diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 4eda9e19d..de40d31cb 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -76,7 +76,7 @@ public: virtual void formatRadioStatsReply(char *reply) = 0; virtual void formatPacketStatsReply(char *reply) = 0; virtual void formatExtPowerStatsReply(char *reply) { - strcpy(reply, "No external power monitoring board detected"); + strcpy(reply, "{\"err\":\"No external power monitoring board detected\"}"); }; virtual mesh::LocalIdentity& getSelfId() = 0; virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index df1fe9dca..c38a121a3 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -493,16 +493,20 @@ bool EnvironmentSensorManager::formatExtPowerStats(char* reply) { #if ENV_INCLUDE_INA3221 if (!INA3221_initialized) return false; char* dp = reply; + int remaining = 160; // max reply buffer length *dp++ = '{'; + remaining--; bool first = true; for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { if (INA3221.isChannelEnabled(i)) { int ch = i + 1; int voltage_mv = (int)(INA3221.getBusVoltage(i) * 1000); int current_ma = (int)(INA3221.getCurrentAmps(i) * 1000); - if (!first) *dp++ = ','; - sprintf(dp, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma); - dp = strchr(dp, 0); + if (!first) { *dp++ = ','; remaining--; } + int n = snprintf(dp, remaining, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma); + if (n >= remaining) break; + dp += n; + remaining -= n; first = false; } }