Skip to content

Commit 948078a

Browse files
zwave_rx: Harden zwave_rx_print_protocol_version in zwave_rx.c
Checking snprintf results, 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 Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Relate-to: SiliconLabsSoftware#100 Signed-off-by: Philippe Coval <[email protected]>
1 parent 8bc24a8 commit 948078a

File tree

1 file changed

+13
-5
lines changed
  • applications/zpc/components/zwave/zwave_rx/src

1 file changed

+13
-5
lines changed

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

Lines changed: 13 additions & 5 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,11 +90,18 @@ 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]);
96-
}
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, "Error in zwave_rx_print_protocol_version");
99+
assert(false);
100+
// Stop processing if snprintf fails or would overflow the buffer
101+
break;
102+
}
103+
index += written;
104+
}
97105

98106
sl_log_info(LOG_TAG,
99107
"Z-Wave API protocol git commit: %s\n",

0 commit comments

Comments
 (0)