From 4ccdeb35266134ddf3d57aa542746d03b5a56bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Thu, 24 Aug 2017 17:26:25 +0200 Subject: [PATCH] Using strlen with snprintf statements --- code/espurna/domoticz.ino | 2 +- code/espurna/emon.ino | 6 +++--- code/espurna/hlw8012.ino | 8 ++++---- code/espurna/web.ino | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index cd870ae6..fdf84df3 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -72,7 +72,7 @@ template void domoticzSend(const char * key, T nvalue, const char * unsigned int idx = getSetting(key).toInt(); if (idx > 0) { char payload[128]; - snprintf(payload, 128, "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); + snprintf(payload, strlen(payload), "{\"idx\": %d, \"nvalue\": %s, \"svalue\": \"%s\"}", idx, String(nvalue).c_str(), svalue); mqttSendRaw(getSetting("dczTopicIn", DOMOTICZ_IN_TOPIC).c_str(), payload); } } diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index dd5348c8..5c5565c1 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -191,11 +191,11 @@ void powerMonitorLoop() { #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf_P(buffer, 20, PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf_P(buffer, 20, PSTR("%s"), String(energy_delta, 3).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf_P(buffer, 20, PSTR("%s"), String(_emonCurrent, 3).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(_emonCurrent, 3).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/hlw8012.ino b/code/espurna/hlw8012.ino index d3faf45a..1d3a41ef 100644 --- a/code/espurna/hlw8012.ino +++ b/code/espurna/hlw8012.ino @@ -295,13 +295,13 @@ void hlw8012Loop() { #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf_P(buffer, 20, PSTR("%d;%s"), _hlwPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%d;%s"), _hlwPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf_P(buffer, 20, PSTR("%s"), String(energy_delta, 3).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf_P(buffer, 20, PSTR("%d"), _hlwVoltage); + snprintf_P(buffer, strlen(buffer), PSTR("%d"), _hlwVoltage); domoticzSend("dczVoltIdx", 0, buffer); - snprintf_P(buffer, 20, PSTR("%s"), String(_hlwCurrent).c_str()); + snprintf_P(buffer, strlen(buffer), PSTR("%s"), String(_hlwCurrent).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 8e881a2a..861b56e6 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -762,7 +762,7 @@ void apiRegister(const char * url, const char * key, apiGetCallbackFunction getF // Store it web_api_t api; char buffer[40]; - snprintf_P(buffer, 39, PSTR("/api/%s"), url); + snprintf_P(buffer, strlen(buffer), PSTR("/api/%s"), url); api.url = strdup(buffer); api.key = strdup(key); api.getFn = getFn;