Skip to content

Commit 3727e2b

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 bc5bf48 commit 3727e2b

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 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,22 @@ 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+
written
124+
= snprintf(message + index, sizeof(message) - index, "%02X ", buffer[i]);
125+
if (written < 0 || written >= (int)(sizeof(message) - index)) {
126+
assert(false);
127+
sl_log_error(LOG_TAG, "Overflow in zwapi_frame_to_string\n");
128+
return NULL;
129+
}
130+
index += written;
123131
}
124132
return message;
125133
}
@@ -650,7 +658,7 @@ void zwave_api_protocol_rx_dispatch(uint8_t *pData, uint16_t len)
650658
case FUNC_ID_ZW_REQUEST_PROTOCOL_CC_ENCRYPTION:
651659
if (zwave_api_get_callbacks()->protocol_cc_encryption_request != NULL) {
652660
// 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;
661+
uint8_t current_index = IDX_DATA;
654662
const zwave_node_id_t destination_node_id
655663
= zwapi_read_node_id(pData, &current_index);
656664
const uint8_t payload_length = pData[current_index++];

0 commit comments

Comments
 (0)