Skip to content

Commit 76968c4

Browse files
committed
fix: wrap error in JSON and add reply buffer guard
1 parent d2b4733 commit 76968c4

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

examples/simple_repeater/MyMesh.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ void MyMesh::formatPacketStatsReply(char *reply) {
10211021

10221022
void MyMesh::formatExtPowerStatsReply(char *reply) {
10231023
if (!sensors.formatExtPowerStats(reply)) {
1024-
strcpy(reply, "No external power monitoring board detected");
1024+
strcpy(reply, "{\"err\":\"No external power monitoring board detected\"}");
10251025
}
10261026
}
10271027

src/helpers/CommonCLI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class CommonCLICallbacks {
7676
virtual void formatRadioStatsReply(char *reply) = 0;
7777
virtual void formatPacketStatsReply(char *reply) = 0;
7878
virtual void formatExtPowerStatsReply(char *reply) {
79-
strcpy(reply, "No external power monitoring board detected");
79+
strcpy(reply, "{\"err\":\"No external power monitoring board detected\"}");
8080
};
8181
virtual mesh::LocalIdentity& getSelfId() = 0;
8282
virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0;

src/helpers/sensors/EnvironmentSensorManager.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,20 @@ bool EnvironmentSensorManager::formatExtPowerStats(char* reply) {
493493
#if ENV_INCLUDE_INA3221
494494
if (!INA3221_initialized) return false;
495495
char* dp = reply;
496+
int remaining = 160; // max reply buffer length
496497
*dp++ = '{';
498+
remaining--;
497499
bool first = true;
498500
for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) {
499501
if (INA3221.isChannelEnabled(i)) {
500502
int ch = i + 1;
501503
int voltage_mv = (int)(INA3221.getBusVoltage(i) * 1000);
502504
int current_ma = (int)(INA3221.getCurrentAmps(i) * 1000);
503-
if (!first) *dp++ = ',';
504-
sprintf(dp, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma);
505-
dp = strchr(dp, 0);
505+
if (!first) { *dp++ = ','; remaining--; }
506+
int n = snprintf(dp, remaining, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma);
507+
if (n >= remaining) break;
508+
dp += n;
509+
remaining -= n;
506510
first = false;
507511
}
508512
}

0 commit comments

Comments
 (0)