mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-12 18:59:51 +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>";
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user