Skip to content

Commit 0b3dc7b

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 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 8d3f3a9 commit 0b3dc7b

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

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

Lines changed: 17 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,25 @@ 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=");
122+
} else {
123+
written = snprintf(message + index,
124+
sizeof(message) - index,
125+
"%02X ",
126+
buffer[i]);
120127
}
121-
index
122-
+= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
128+
if (written < 0 || written >= (int)(sizeof(message) - index)) {
129+
assert(false);
130+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
131+
return NULL;
132+
}
133+
index += written;
123134
}
124135
return message;
125136
}
@@ -650,7 +661,7 @@ void zwave_api_protocol_rx_dispatch(uint8_t *pData, uint16_t len)
650661
case FUNC_ID_ZW_REQUEST_PROTOCOL_CC_ENCRYPTION:
651662
if (zwave_api_get_callbacks()->protocol_cc_encryption_request != NULL) {
652663
// 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;
664+
uint8_t current_index = IDX_DATA;
654665
const zwave_node_id_t destination_node_id
655666
= zwapi_read_node_id(pData, &current_index);
656667
const uint8_t payload_length = pData[current_index++];

0 commit comments

Comments
 (0)