fix(mqtt): ensure valid hex string output in bytesToHex and packetToHex
Add checks to prevent null or zero-length hex buffers in the bytesToHex and packetToHex functions. This guarantees that an empty string is returned instead of an uninitialized buffer, improving the safety of JSON serialization.
This commit is contained in:
1 parent
c2b157dda0
commit
9609ae3932
1 file changed
+10
@@ -302,6 +302,11 @@ const char* MQTTMessageBuilder::getRouteTypeString(int route_type) {
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::bytesToHex(const uint8_t* data, size_t len, char* hex, size_t hex_size) {
|
||||
if (hex == nullptr || hex_size == 0) return;
|
||||
// Guarantee a valid (empty) string even if we bail out below, so a caller's
|
||||
// uninitialized stack buffer is never serialized into the JSON raw/hash fields
|
||||
// when the buffer is too small (A6).
|
||||
hex[0] = '\0';
|
||||
if (hex_size < len * 2 + 1) return;
|
||||
|
||||
// Nibble lookup instead of a per-byte snprintf("%02X"): same uppercase hex
|
||||
@@ -315,6 +320,11 @@ void MQTTMessageBuilder::bytesToHex(const uint8_t* data, size_t len, char* hex,
|
||||
}
|
||||
|
||||
void MQTTMessageBuilder::packetToHex(mesh::Packet* packet, char* hex, size_t hex_size) {
|
||||
if (hex == nullptr || hex_size == 0) return;
|
||||
// Empty string on any early-out below (serialization returned nothing, or the
|
||||
// hex buffer is too small) so an uninitialized raw_hex[] never reaches the
|
||||
// published JSON (A6).
|
||||
hex[0] = '\0';
|
||||
// Serialize full on-air/wire format using Packet::writeTo()
|
||||
// This includes header, transport codes (if present), path_len, path, and payload
|
||||
uint8_t raw_buf[512];
|
||||
|
||||
Reference in new issue
Block a user