diff --git a/src/core/helpers.cpp b/src/core/helpers.cpp index 9c14b82f3..207167ac8 100644 --- a/src/core/helpers.cpp +++ b/src/core/helpers.cpp @@ -495,11 +495,11 @@ std::string Helpers::data_to_hex(const uint8_t * data, const uint8_t length) { return ""; } - char str[length * 3]; - memset(str, 0, sizeof(str)); + std::vector str(length * 3); + memset(str.data(), 0, str.size()); char buffer[4]; - char * p = &str[0]; + char * p = str.data(); for (uint8_t i = 0; i < length; i++) { Helpers::hextoa(buffer, data[i]); *p++ = buffer[0]; @@ -508,7 +508,7 @@ std::string Helpers::data_to_hex(const uint8_t * data, const uint8_t length) { } *--p = '\0'; // null terminate just in case, loosing the trailing space - return std::string(str); + return std::string(str.data()); } // takes a hex string and convert it to an unsigned 32bit number (max 8 hex digits) diff --git a/src/core/mqtt.cpp b/src/core/mqtt.cpp index b2090121a..39395aa1e 100644 --- a/src/core/mqtt.cpp +++ b/src/core/mqtt.cpp @@ -222,9 +222,11 @@ void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) { // the payload is not terminated // convert payload to a null-terminated char string // see https://www.emelis.net/espMqttClient/#code-samples - char message[len + 1]; - memcpy(message, payload, len); - message[len] = '\0'; + // fix variable-length arrays (VLAs) "char message[len + 1]" as they are not standard C++; they're a Clang/GCC extension. + std::vector message_buffer(len + 1); + memcpy(message_buffer.data(), payload, len); + message_buffer[len] = '\0'; + char * message = message_buffer.data(); #if defined(EMSESP_DEBUG) if (len) {