mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-13 19:29:55 +03:00
replace VLAs with standard C++
This commit is contained in:
@@ -495,11 +495,11 @@ std::string Helpers::data_to_hex(const uint8_t * data, const uint8_t length) {
|
|||||||
return "<empty>";
|
return "<empty>";
|
||||||
}
|
}
|
||||||
|
|
||||||
char str[length * 3];
|
std::vector<char> str(length * 3);
|
||||||
memset(str, 0, sizeof(str));
|
memset(str.data(), 0, str.size());
|
||||||
|
|
||||||
char buffer[4];
|
char buffer[4];
|
||||||
char * p = &str[0];
|
char * p = str.data();
|
||||||
for (uint8_t i = 0; i < length; i++) {
|
for (uint8_t i = 0; i < length; i++) {
|
||||||
Helpers::hextoa(buffer, data[i]);
|
Helpers::hextoa(buffer, data[i]);
|
||||||
*p++ = buffer[0];
|
*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
|
*--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)
|
// takes a hex string and convert it to an unsigned 32bit number (max 8 hex digits)
|
||||||
|
|||||||
@@ -222,9 +222,11 @@ void Mqtt::on_message(const char * topic, const uint8_t * payload, size_t len) {
|
|||||||
// the payload is not terminated
|
// the payload is not terminated
|
||||||
// convert payload to a null-terminated char string
|
// convert payload to a null-terminated char string
|
||||||
// see https://www.emelis.net/espMqttClient/#code-samples
|
// see https://www.emelis.net/espMqttClient/#code-samples
|
||||||
char message[len + 1];
|
// fix variable-length arrays (VLAs) "char message[len + 1]" as they are not standard C++; they're a Clang/GCC extension.
|
||||||
memcpy(message, payload, len);
|
std::vector<char> message_buffer(len + 1);
|
||||||
message[len] = '\0';
|
memcpy(message_buffer.data(), payload, len);
|
||||||
|
message_buffer[len] = '\0';
|
||||||
|
char * message = message_buffer.data();
|
||||||
|
|
||||||
#if defined(EMSESP_DEBUG)
|
#if defined(EMSESP_DEBUG)
|
||||||
if (len) {
|
if (len) {
|
||||||
|
|||||||
Reference in New Issue
Block a user