Browse Source

fix: wrap error in JSON and add reply buffer guard

pull/1797/head
MrAlders0n 4 months ago
parent
commit
76968c4d79
  1. 2
      examples/simple_repeater/MyMesh.cpp
  2. 2
      src/helpers/CommonCLI.h
  3. 10
      src/helpers/sensors/EnvironmentSensorManager.cpp

2
examples/simple_repeater/MyMesh.cpp

@ -1021,7 +1021,7 @@ void MyMesh::formatPacketStatsReply(char *reply) {
void MyMesh::formatExtPowerStatsReply(char *reply) { void MyMesh::formatExtPowerStatsReply(char *reply) {
if (!sensors.formatExtPowerStats(reply)) { if (!sensors.formatExtPowerStats(reply)) {
strcpy(reply, "No external power monitoring board detected"); strcpy(reply, "{\"err\":\"No external power monitoring board detected\"}");
} }
} }

2
src/helpers/CommonCLI.h

@ -76,7 +76,7 @@ public:
virtual void formatRadioStatsReply(char *reply) = 0; virtual void formatRadioStatsReply(char *reply) = 0;
virtual void formatPacketStatsReply(char *reply) = 0; virtual void formatPacketStatsReply(char *reply) = 0;
virtual void formatExtPowerStatsReply(char *reply) { 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 mesh::LocalIdentity& getSelfId() = 0;
virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0; virtual void saveIdentity(const mesh::LocalIdentity& new_id) = 0;

10
src/helpers/sensors/EnvironmentSensorManager.cpp

@ -493,16 +493,20 @@ bool EnvironmentSensorManager::formatExtPowerStats(char* reply) {
#if ENV_INCLUDE_INA3221 #if ENV_INCLUDE_INA3221
if (!INA3221_initialized) return false; if (!INA3221_initialized) return false;
char* dp = reply; char* dp = reply;
int remaining = 160; // max reply buffer length
*dp++ = '{'; *dp++ = '{';
remaining--;
bool first = true; bool first = true;
for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) {
if (INA3221.isChannelEnabled(i)) { if (INA3221.isChannelEnabled(i)) {
int ch = i + 1; int ch = i + 1;
int voltage_mv = (int)(INA3221.getBusVoltage(i) * 1000); int voltage_mv = (int)(INA3221.getBusVoltage(i) * 1000);
int current_ma = (int)(INA3221.getCurrentAmps(i) * 1000); int current_ma = (int)(INA3221.getCurrentAmps(i) * 1000);
if (!first) *dp++ = ','; if (!first) { *dp++ = ','; remaining--; }
sprintf(dp, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma); int n = snprintf(dp, remaining, "\"ch%d_voltage_mv\":%d,\"ch%d_current_ma\":%d", ch, voltage_mv, ch, current_ma);
dp = strchr(dp, 0); if (n >= remaining) break;
dp += n;
remaining -= n;
first = false; first = false;
} }
} }

Loading…
Cancel
Save