Switch to using snprintf instead of sprintf
This commit is contained in:
		| @@ -47,7 +47,7 @@ void dirup(char *dst, u64 levels, const char *path) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   char tmp[256]; |   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 |   // NOTE (Abdelrahman): Conditions stored in variables to silence MSVC warning C5045 | ||||||
|   bool insufficient_levels = sep_count < levels; |   bool insufficient_levels = sep_count < levels; | ||||||
|   bool path_to_copy_is_separator = strncmp(path, tmp, copy_count) != 0; |   bool path_to_copy_is_separator = strncmp(path, tmp, copy_count) != 0; | ||||||
|   | |||||||
| @@ -5,13 +5,16 @@ | |||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
| #include <stdbool.h> | #include <stdbool.h> | ||||||
|  |  | ||||||
| TestFuncResult test_cpath_join_path(void) { | #define MAIN_BUF_SIZE 4096 | ||||||
|   char expected[4096] = {0}; | #define TMP_BUF_SIZE  1024 | ||||||
|   char out[4096]      = {0}; |  | ||||||
|   char tmp[1024]      = {0}; |  | ||||||
|  |  | ||||||
|   sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); | TestFuncResult test_cpath_join_path(void) { | ||||||
|   sprintf(tmp, "%c", PATH_SEP); |   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"); |   wapp_cpath_join_path(out, tmp, "home", "abdelrahman", "Documents"); | ||||||
|  |  | ||||||
|   bool result = strcmp(out, expected) == 0; |   bool result = strcmp(out, expected) == 0; | ||||||
| @@ -20,7 +23,7 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   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"); |   wapp_cpath_join_path(out, "home", "abdelrahman", "Documents"); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -30,8 +33,8 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   memset(out, 0, strlen(out)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   sprintf(expected, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); | ||||||
|   sprintf(tmp, "%chome", PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); |   wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -41,8 +44,8 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   memset(out, 0, strlen(out)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   sprintf(expected, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "home%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP); | ||||||
|   sprintf(tmp, "home%c", PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "home%c", PATH_SEP); | ||||||
|   wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); |   wapp_cpath_join_path(out, tmp, "abdelrahman", "Documents"); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -52,8 +55,8 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   memset(out, 0, strlen(out)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   sprintf(expected, "%chome", PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   sprintf(tmp, "%chome", PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   wapp_cpath_join_path(out, tmp, ""); |   wapp_cpath_join_path(out, tmp, ""); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -62,7 +65,7 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   memset(out, 0, strlen(out)); | ||||||
|   sprintf(expected, ""); |   snprintf(expected, 1, ""); | ||||||
|   wapp_cpath_join_path(out, "", ""); |   wapp_cpath_join_path(out, "", ""); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -71,7 +74,7 @@ TestFuncResult test_cpath_join_path(void) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   memset(out, 0, strlen(out)); |   memset(out, 0, strlen(out)); | ||||||
|   sprintf(expected, "home"); |   snprintf(expected, MAIN_BUF_SIZE, "home"); | ||||||
|   wapp_cpath_join_path(out, "", "home"); |   wapp_cpath_join_path(out, "", "home"); | ||||||
|  |  | ||||||
|   result = result && strcmp(out, expected) == 0; |   result = result && strcmp(out, expected) == 0; | ||||||
| @@ -81,11 +84,11 @@ TEST_JOIN_PATH_EXIT: | |||||||
| } | } | ||||||
|  |  | ||||||
| TestFuncResult test_cpath_dirname(void) { | TestFuncResult test_cpath_dirname(void) { | ||||||
|   char dst[4096]      = {0}; |   char dst[MAIN_BUF_SIZE]      = {0}; | ||||||
|   char expected[4096] = {0}; |   char expected[MAIN_BUF_SIZE] = {0}; | ||||||
|   char tmp[1024]      = {0}; |   char tmp[TMP_BUF_SIZE]      = {0}; | ||||||
|  |  | ||||||
|   sprintf(tmp, "%c", PATH_SEP); |   snprintf(tmp, 2, "%c", PATH_SEP); | ||||||
|   wapp_cpath_dirname(dst, tmp); |   wapp_cpath_dirname(dst, tmp); | ||||||
|   bool result = strcmp(dst, tmp) == 0; |   bool result = strcmp(dst, tmp) == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -107,8 +110,8 @@ TestFuncResult test_cpath_dirname(void) { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   sprintf(tmp, "%chome%ctest", PATH_SEP, PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest", PATH_SEP, PATH_SEP); | ||||||
|   sprintf(expected, "%chome", PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   wapp_cpath_dirname(dst, tmp); |   wapp_cpath_dirname(dst, tmp); | ||||||
|   result = strcmp(dst, expected) == 0; |   result = strcmp(dst, expected) == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -118,8 +121,8 @@ TestFuncResult test_cpath_dirname(void) { | |||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   memset(expected, 0, strlen(expected)); |   memset(expected, 0, strlen(expected)); | ||||||
|   sprintf(tmp, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome%ctest%c", PATH_SEP, PATH_SEP, PATH_SEP); | ||||||
|   sprintf(expected, "%chome", PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   wapp_cpath_dirname(dst, tmp); |   wapp_cpath_dirname(dst, tmp); | ||||||
|   result = strcmp(dst, expected) == 0; |   result = strcmp(dst, expected) == 0; | ||||||
|  |  | ||||||
| @@ -128,11 +131,11 @@ TEST_DIRNAME_EXIT: | |||||||
| } | } | ||||||
|  |  | ||||||
| TestFuncResult test_cpath_dirup(void) { | TestFuncResult test_cpath_dirup(void) { | ||||||
|   char dst[4096]      = {0}; |   char dst[MAIN_BUF_SIZE]      = {0}; | ||||||
|   char expected[4096] = {0}; |   char expected[MAIN_BUF_SIZE] = {0}; | ||||||
|   char tmp[1024]      = {0}; |   char tmp[TMP_BUF_SIZE]      = {0}; | ||||||
|  |  | ||||||
|   sprintf(tmp, "%c", PATH_SEP); |   snprintf(tmp, 2, "%c", PATH_SEP); | ||||||
|   wapp_cpath_dirup(dst, 3, tmp); |   wapp_cpath_dirup(dst, 3, tmp); | ||||||
|   bool result = strcmp(dst, tmp) == 0; |   bool result = strcmp(dst, tmp) == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -141,8 +144,8 @@ TestFuncResult test_cpath_dirup(void) { | |||||||
|  |  | ||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); | ||||||
|   sprintf(expected, "%c", PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%c", PATH_SEP); | ||||||
|   wapp_cpath_dirup(dst, 3, tmp); |   wapp_cpath_dirup(dst, 3, tmp); | ||||||
|   result = strcmp(dst, expected) == 0; |   result = strcmp(dst, expected) == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -151,7 +154,7 @@ TestFuncResult test_cpath_dirup(void) { | |||||||
|  |  | ||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   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); |   wapp_cpath_dirup(dst, 3, tmp); | ||||||
|   result = strcmp(dst, ".") == 0; |   result = strcmp(dst, ".") == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -161,8 +164,8 @@ TestFuncResult test_cpath_dirup(void) { | |||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   memset(tmp, 0, strlen(tmp)); | ||||||
|   memset(expected, 0, strlen(expected)); |   memset(expected, 0, strlen(expected)); | ||||||
|   sprintf(tmp, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); |   snprintf(tmp, TMP_BUF_SIZE, "%chome%cabdelrahman%cDocuments", PATH_SEP, PATH_SEP, PATH_SEP); | ||||||
|   sprintf(expected, "%chome", PATH_SEP); |   snprintf(expected, MAIN_BUF_SIZE, "%chome", PATH_SEP); | ||||||
|   wapp_cpath_dirup(dst, 2, tmp); |   wapp_cpath_dirup(dst, 2, tmp); | ||||||
|   result = strcmp(dst, expected) == 0; |   result = strcmp(dst, expected) == 0; | ||||||
|   if (!result) { |   if (!result) { | ||||||
| @@ -171,7 +174,7 @@ TestFuncResult test_cpath_dirup(void) { | |||||||
|  |  | ||||||
|   memset(dst, 0, strlen(dst)); |   memset(dst, 0, strlen(dst)); | ||||||
|   memset(tmp, 0, strlen(tmp)); |   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); |   wapp_cpath_dirup(dst, 2, tmp); | ||||||
|   result = strcmp(dst, "home") == 0; |   result = strcmp(dst, "home") == 0; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,7 +27,7 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { | |||||||
|   char expected_output[64] = {0}; |   char expected_output[64] = {0}; | ||||||
|   const char *msg          = "hello world"; |   const char *msg          = "hello world"; | ||||||
|   u64 length               = strlen(msg); |   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); |   CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg); | ||||||
|   bool succeeded   = result.exited && result.exit_code == EXIT_SUCCESS && |   bool succeeded   = result.exited && result.exit_code == EXIT_SUCCESS && | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user