Fix snprintf usage

Some code used snprintf on std::string objects.
This caused unexpected behaviour where '\0' was printed to the output file.
This commit adds intermediate char* buffer which is later used to
initialize std::string. This string is later resized to the correct
expected length.

Signed-off-by: Maciej Dudek <[email protected]>
This commit is contained in:
Maciej Dudek
2025-01-24 11:48:37 +01:00
parent 5847ec2666
commit e8d8ecc363
4 changed files with 54 additions and 25 deletions
+11 -6
View File
@@ -63,15 +63,19 @@ int FsParser::parseHeader()
uint8_t key = c & 0x7F;
uint64_t val = bitToVal(buffer.c_str(), buffer.size());
char __buf[10];
int __buf_valid_bytes;
switch (key) {
case 0x06: /* idCode */
_idcode = (0xffffffff & val);
_hdr["idcode"] = string(8, ' ');
snprintf(&_hdr["idcode"][0], 9, "%08x", _idcode);
__buf_valid_bytes = snprintf(__buf, 9, "%08x", _idcode);
_hdr["idcode"] = string(__buf, __buf_valid_bytes);
_hdr["idcode"].resize(8, ' ');
break;
case 0x0A: /* user code or checksum ? */
_hdr["CheckSum"] = string(8, ' ');
snprintf(&_hdr["CheckSum"][0], 9, "%08x", (uint32_t)(0xffffffff & val));
__buf_valid_bytes = snprintf(__buf, 9, "%08x", (uint32_t)(0xffffffff & val));
_hdr["CheckSum"] = string(__buf, __buf_valid_bytes);
_hdr["CheckSum"].resize(8, ' ');
break;
case 0x0B: /* only present when bit_security is set */
_hdr["SecurityBit"] = "ON";
@@ -107,8 +111,9 @@ int FsParser::parseHeader()
case 0x52: /* documentation issue */
uint32_t flash_addr;
flash_addr = val & 0xffffffff;
_hdr["SPIAddr"] = string(8, ' ');
snprintf(&_hdr["SPIAddr"][0], 9, "%08x", flash_addr);
__buf_valid_bytes = snprintf(__buf, 9, "%08x", flash_addr);
_hdr["SPIAddr"] = string(__buf, __buf_valid_bytes);
_hdr["SPIAddr"].resize(8, ' ');
break;
case 0x3B: /* last header line with crc and cfg data length */