Skip to content

Commit 4f86970

Browse files
utils: Harden zpc_converters.c by checking snprintf
Checking snprintf results, reminder : If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available This was found using CodeQL: Potential fix for code scanning alert no. 12: Potentially overflowing call to snprintf Relate-to: SiliconLabsSoftware#100 Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Philippe Coval <[email protected]>
1 parent f6ce631 commit 4f86970

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

applications/zpc/components/zpc_utils/src/zpc_converters.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#include <errno.h>
1919
#include <stdio.h>
2020

21-
sl_status_t zpc_converters_dsk_str_to_internal(const char *src,
22-
zwave_dsk_t dst)
21+
sl_status_t zpc_converters_dsk_str_to_internal(const char *src, zwave_dsk_t dst)
2322
{
2423
unsigned int dst_idx = 0;
2524
const char *idx = src;
@@ -51,8 +50,13 @@ sl_status_t zpc_converters_dsk_to_str(const zwave_dsk_t src,
5150
}
5251
size_t index = 0;
5352
for (int i = 0; i < sizeof(zwave_dsk_t); i += 2) {
54-
int d = (src[i] << 8) | src[i + 1];
55-
index += snprintf(&dst[index], dst_max_len - index, "%05i-", d);
53+
int d = (src[i] << 8) | src[i + 1];
54+
int written = snprintf(&dst[index], dst_max_len - index, "%05i-", d);
55+
if (written < 0 || written >= dst_max_len - index) {
56+
assert(false);
57+
return SL_STATUS_WOULD_OVERFLOW;
58+
}
59+
index += written;
5660
}
5761
// Erase the last "-"
5862
if (index > 0) {

0 commit comments

Comments
 (0)