Skip to content

Commit 8d3f3a9

Browse files
zwave_api: Harden zwapi_connection.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. 19: Potentially overflowing call to snprintf More refactoring can be done in later change 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 6b68856 commit 8d3f3a9

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

applications/zpc/components/zwave_api/src/zwapi_connection.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
*****************************************************************************/
1313

14+
#include <assert.h>
1415
#include <string.h>
1516
#include "zwapi_connection.h"
1617
#include "zwapi_serial.h"
@@ -45,19 +46,44 @@ static const char *zwapi_frame_to_string(const uint8_t *buffer,
4546
{
4647
static char message[1000] = {'\0'};
4748
uint16_t index = 0;
49+
int written = 0;
4850
for (uint16_t i = 0; i < buffer_length; i++) {
4951
if (i == 0) {
5052
// Don't log the SOF byte.
5153
continue;
5254
} else if (i == 1) {
53-
index += snprintf(message + index, sizeof(message) - index, "Length=");
55+
written = snprintf(message + index, sizeof(message) - index, "Length=");
56+
if (written < 0 || written >= sizeof(message) - index) {
57+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
58+
assert(false);
59+
return NULL;
60+
}
61+
index += written;
5462
} else if (i == 2) {
55-
index += snprintf(message + index, sizeof(message) - index, "Type=");
63+
written = snprintf(message + index, sizeof(message) - index, "Type=");
64+
if (written < 0 || written >= sizeof(message) - index) {
65+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
66+
assert(false);
67+
return NULL;
68+
}
69+
index += written;
5670
} else if (i == 3) {
57-
index += snprintf(message + index, sizeof(message) - index, "Cmd=");
71+
written = snprintf(message + index, sizeof(message) - index, "Cmd=");
72+
if (written < 0 || written >= sizeof(message) - index) {
73+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
74+
assert(false);
75+
return NULL;
76+
}
77+
index += written;
78+
}
79+
written
80+
= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
81+
if (written < 0 || written >= sizeof(message) - index) {
82+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
83+
assert(false);
84+
return NULL;
5885
}
59-
index
60-
+= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
86+
index += written;
6187
}
6288
return message;
6389
}

0 commit comments

Comments
 (0)