From 1108ed686eda54aeb60cd5881426e99dd08fdaab Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Mon, 7 Oct 2024 08:01:33 +0100 Subject: [PATCH] Switch to using snprintf instead of sprintf --- src/os/cpath/cpath.c | 2 +- tests/cpath/test_cpath.c | 69 ++++++++++---------- tests/shell_commander/test_shell_commander.c | 2 +- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/os/cpath/cpath.c b/src/os/cpath/cpath.c index 6629d1c..02057c4 100644 --- a/src/os/cpath/cpath.c +++ b/src/os/cpath/cpath.c @@ -47,7 +47,7 @@ void dirup(char *dst, u64 levels, const char *path) { } char tmp[256]; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); // NOTE (Abdelrahman): Conditions stored in variables to silence MSVC warning C5045 bool insufficient_levels = sep_count < levels; bool path_to_copy_is_separator = strncmp(path, tmp, copy_count) != 0; diff --git a/tests/cpath/test_cpath.c b/tests/cpath/test_cpath.c index 91f6970..4fd2928 100644 --- a/tests/cpath/test_cpath.c +++ b/tests/cpath/test_cpath.c @@ -5,13 +5,16 @@ #include #include -TestFuncResult test_cpath_join_path(void) { - char expected[4096] = {0}; - char out[4096] = {0}; - char tmp[1024] = {0}; +#define MAIN_BUF_SIZE 4096 +#define TMP_BUF_SIZE 1024 - sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(tmp, "%c", PATH_SEP); +TestFuncResult test_cpath_join_path(void) { + char expected[MAIN_BUF_SIZE] = {0}; + char out[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; + + snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_join_path(out, tmp, "home", "abdelrahman", "Documents"); bool result = strcmp(out, expected) == 0; @@ -20,7 +23,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_join_path(out, "home", "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -30,8 +33,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(tmp, "%chome", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -41,8 +44,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); - sprintf(tmp, "home%c", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%c", PATH_SEP); wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); result = result && strcmp(out, expected) == 0; @@ -52,8 +55,8 @@ TestFuncResult test_cpath_join_path(void) { memset(out, 0, strlen(out)); memset(tmp, 0, strlen(tmp)); - sprintf(expected, "%chome", PATH_SEP); - sprintf(tmp, "%chome", PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_join_path(out, tmp, ""); result = result && strcmp(out, expected) == 0; @@ -62,7 +65,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, ""); + snprintf(expected, 1, ""); wapp_cpath_join_path(out, "", ""); result = result && strcmp(out, expected) == 0; @@ -71,7 +74,7 @@ TestFuncResult test_cpath_join_path(void) { } memset(out, 0, strlen(out)); - sprintf(expected, "home"); + snprintf(expected, MAIN_BUF_SIZE, "home"); wapp_cpath_join_path(out, "", "home"); result = result && strcmp(out, expected) == 0; @@ -81,11 +84,11 @@ TEST_JOIN_PATH_EXIT: } TestFuncResult test_cpath_dirname(void) { - char dst[4096] = {0}; - char expected[4096] = {0}; - char tmp[1024] = {0}; + char dst[MAIN_BUF_SIZE] = {0}; + char expected[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_dirname(dst, tmp); bool result = strcmp(dst, tmp) == 0; if (!result) { @@ -107,8 +110,8 @@ TestFuncResult test_cpath_dirname(void) { } memset(dst, 0, strlen(dst)); - sprintf(tmp, "%chome%ctest", PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest", PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirname(dst, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -118,8 +121,8 @@ TestFuncResult test_cpath_dirname(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); memset(expected, 0, strlen(expected)); - sprintf(tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirname(dst, tmp); result = strcmp(dst, expected) == 0; @@ -128,11 +131,11 @@ TEST_DIRNAME_EXIT: } TestFuncResult test_cpath_dirup(void) { - char dst[4096] = {0}; - char expected[4096] = {0}; - char tmp[1024] = {0}; + char dst[MAIN_BUF_SIZE] = {0}; + char expected[MAIN_BUF_SIZE] = {0}; + char tmp[TMP_BUF_SIZE] = {0}; - sprintf(tmp, "%c", PATH_SEP); + snprintf(tmp, 2, "%c", PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); bool result = strcmp(dst, tmp) == 0; if (!result) { @@ -141,8 +144,8 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%c", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%c", PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -151,7 +154,7 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_dirup(dst, 3, tmp); result = strcmp(dst, ".") == 0; if (!result) { @@ -161,8 +164,8 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); memset(expected, 0, strlen(expected)); - sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); - sprintf(expected, "%chome", PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); + snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); wapp_cpath_dirup(dst, 2, tmp); result = strcmp(dst, expected) == 0; if (!result) { @@ -171,7 +174,7 @@ TestFuncResult test_cpath_dirup(void) { memset(dst, 0, strlen(dst)); memset(tmp, 0, strlen(tmp)); - sprintf(tmp, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); + snprintf(tmp, TMP_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); wapp_cpath_dirup(dst, 2, tmp); result = strcmp(dst, "home") == 0; diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c index 0df9655..84e4060 100644 --- a/tests/shell_commander/test_shell_commander.c +++ b/tests/shell_commander/test_shell_commander.c @@ -27,7 +27,7 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { char expected_output[64] = {0}; const char *msg = "hello world"; u64 length = strlen(msg); - sprintf(expected_output, "%s\n", msg); + snprintf(expected_output, length + 2, "%s\n", msg); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg); bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&