Skip to content

Commit 7d28bad

Browse files
zwave_api: Harden zwapi_protocol_rx_dispatch.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. 23: Potentially overflowing call to snprintf Origin: SiliconLabsSoftware#118 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 956f987 commit 7d28bad

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*****************************************************************************/
1313

1414
// Generic includes
15+
#include <assert.h>
1516
#include <string.h>
1617
#include <stdio.h>
1718

@@ -111,15 +112,28 @@ static const char *zwapi_frame_to_string(const uint8_t *buffer,
111112
static char message[1000] = {'\0'};
112113
uint16_t index = 0;
113114
for (uint16_t i = 0; i < buffer_length; i++) {
115+
int written = 0;
114116
if (i == IDX_LEN) {
115-
index += snprintf(message + index, sizeof(message) - index, "Length=");
117+
written = snprintf(message + index, sizeof(message) - index, "Length=");
116118
} else if (i == IDX_TYPE) {
117-
index += snprintf(message + index, sizeof(message) - index, "Type=");
119+
written = snprintf(message + index, sizeof(message) - index, "Type=");
118120
} else if (i == IDX_CMD) {
119-
index += snprintf(message + index, sizeof(message) - index, "Cmd=");
121+
written = snprintf(message + index, sizeof(message) - index, "Cmd=");
120122
}
121-
index
122-
+= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
123+
if (written < 0 || written >= (int)(sizeof(message) - index)) {
124+
assert(false);
125+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
126+
return NULL;
127+
}
128+
index += written;
129+
written
130+
= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
131+
if (written < 0 || written >= (int)(sizeof(message) - index)) {
132+
assert(false);
133+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
134+
return NULL;
135+
}
136+
index += written;
123137
}
124138
return message;
125139
}
@@ -650,7 +664,7 @@ void zwave_api_protocol_rx_dispatch(uint8_t *pData, uint16_t len)
650664
case FUNC_ID_ZW_REQUEST_PROTOCOL_CC_ENCRYPTION:
651665
if (zwave_api_get_callbacks()->protocol_cc_encryption_request != NULL) {
652666
// ZW->HOST: REQ | 0x6C | destination_node_id | payload_length | payload | protocol_metadata_length | protocol_metadata | use_supervision | session_id
653-
uint8_t current_index = IDX_DATA;
667+
uint8_t current_index = IDX_DATA;
654668
const zwave_node_id_t destination_node_id
655669
= zwapi_read_node_id(pData, &current_index);
656670
const uint8_t payload_length = pData[current_index++];

0 commit comments

Comments
 (0)