Skip to content

Commit f1c5d53

Browse files
z_rx: Harden zwave_rx_print_protocol_version in zwave_rx.c
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. 15: Potentially overflowing call to snprintf For the record this function escape the git commit to hex form (in ascii) Origin: SiliconLabsSoftware#104 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 786e54c commit f1c5d53

File tree

1 file changed

+16
-9
lines changed
  • applications/zpc/components/zwave/zwave_rx/src

1 file changed

+16
-9
lines changed

applications/zpc/components/zwave/zwave_rx/src/zwave_rx.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
*****************************************************************************/
1313
//Generic includes
14+
#include <assert.h>
1415
#include <stdlib.h>
1516

1617
// Includes from other components
@@ -89,10 +90,16 @@ static void zwave_rx_print_protocol_version(
8990
char git_commit_string[GIT_COMMIT_HASH_SIZE * 2 + 1] = {0};
9091
uint16_t index = 0;
9192
for (uint8_t i = 0; i < GIT_COMMIT_HASH_SIZE; i++) {
92-
index += snprintf(git_commit_string + index,
93-
sizeof(git_commit_string) - index,
94-
"%x",
95-
zwapi_version.git_commit[i]);
93+
int written = snprintf(git_commit_string + index,
94+
sizeof(git_commit_string) - index,
95+
"%x",
96+
zwapi_version.git_commit[i]);
97+
if (written < 0 || written >= (int)(sizeof(git_commit_string) - index)) {
98+
sl_log_error(LOG_TAG, "Overflow in zwave_rx_print_protocol_version\n");
99+
assert(false);
100+
return;
101+
}
102+
index += written;
96103
}
97104

98105
sl_log_info(LOG_TAG,
@@ -118,7 +125,7 @@ sl_status_t zwave_rx_init(const char *serial_port,
118125
= zwave_rx_application_command_handler_bridge;
119126
zwave_rx_zwapi_callbacks.zwapi_started = zwave_rx_zwave_api_started;
120127
zwave_rx_zwapi_callbacks.poll_request = zwave_rx_poll_request;
121-
bool soft_reset_needed = false;
128+
bool soft_reset_needed = false;
122129
// Initialize our Z-Wave API.
123130
sl_status_t command_status
124131
= zwapi_init(serial_port, serial_port_fd, &zwave_rx_zwapi_callbacks);
@@ -216,7 +223,7 @@ sl_status_t zwave_rx_init(const char *serial_port,
216223
"Success setting Z-Wave module Max Long Range transmit power: %d",
217224
max_lr_tx_power_dbm);
218225
sl_log_info(LOG_TAG, "Applying soft reset of the Z-Wave Module\n");
219-
soft_reset_needed = true;
226+
soft_reset_needed = true;
220227
}
221228

222229
sl_log_debug(
@@ -226,9 +233,9 @@ sl_status_t zwave_rx_init(const char *serial_port,
226233
}
227234

228235
if (true == soft_reset_needed) {
229-
zwapi_soft_reset();
230-
// Wait for Z-Wave API started
231-
zwave_rx_wait_for_zwave_api_to_be_ready();
236+
zwapi_soft_reset();
237+
// Wait for Z-Wave API started
238+
zwave_rx_wait_for_zwave_api_to_be_ready();
232239
}
233240
// Try to set the node ID basetype to 16 bits disregarding the RF region
234241
command_status = zwapi_set_node_id_basetype(NODEID_16BITS);

0 commit comments

Comments
 (0)