replace VLAs with standard C++

This commit is contained in:
proddy
2025-12-12 00:00:49 +01:00
parent 55b8c2d04c
commit e34291fbd5
2 changed files with 9 additions and 7 deletions

View File

@@ -495,11 +495,11 @@ std::string Helpers::data_to_hex(const uint8_t * data, const uint8_t length) {
return "<empty>";
}
char str[length * 3];
memset(str, 0, sizeof(str));
std::vector<char> 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)

View File

@@ -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<char> 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) {