From ffb99a36446083f1c38316c339986f9adbfb4305 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sat, 5 Oct 2024 20:07:48 +0100 Subject: [PATCH] Fix MSVC errors and warnings --- src/common/shell/commander/commander.c | 38 ++++++++++++------- src/common/shell/commander/commander.h | 10 ++++- .../shell/termcolour/win/termcolour_win.c | 33 ++++++++-------- src/common/shell/utils/shell_utils.h | 7 ++-- src/core/mem/arena/mem_arena.c | 1 + src/core/mem/libc/mem_libc_allocator.c | 9 +++++ tests/allocator/test_allocator.c | 2 +- tests/arena/test_arena.c | 22 +++++------ tests/shell_commander/test_shell_commander.c | 6 ++- 9 files changed, 77 insertions(+), 51 deletions(-) diff --git a/src/common/shell/commander/commander.c b/src/common/shell/commander/commander.c index 59caec3..3351dfd 100644 --- a/src/common/shell/commander/commander.c +++ b/src/common/shell/commander/commander.c @@ -10,13 +10,10 @@ #define CMD_BUF_LEN 8192 #define OUT_BUF_LEN 4096 -internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, - va_list args); -internal inline CMDResult execute_command(const char *cmd, - CMDOutHandling out_handling, +internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, va_list args); +internal inline CMDResult execute_command(const char *cmd, CMDOutHandling out_handling, char *out_buf, u64 buf_size); -internal inline CMDError get_command_output(FILE *fp, - CMDOutHandling out_handling, +internal inline CMDError get_command_output(FILE *fp, CMDOutHandling out_handling, char *out_buf, u64 buf_size); internal inline CMDError get_output_status(FILE *fp, i32 *status_out); @@ -42,8 +39,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, u64 size = 0; u64 arg_len = 0; - const char *arg; - while ((arg = va_arg(args, const char *))) { + const char *arg = va_arg(args, const char *); + while (arg) { arg_len = strlen(arg); if (arg_len >= buf_len - size) { return SHELL_ERR_CMD_BUF_FULL; @@ -53,6 +50,8 @@ internal inline CMDError build_command_from_args(char *cmd, u64 buf_len, cmd[size + arg_len] = ' '; size += arg_len + 1; + + arg = va_arg(args, const char *); } return SHELL_ERR_NO_ERROR; @@ -66,24 +65,35 @@ internal inline CMDResult execute_command(const char *cmd, return CMD_NO_EXIT(SHELL_ERR_PROC_START_FAIL); } + CMDResult output; + CMDError err = get_command_output(fp, out_handling, out_buf, buf_size); if (err > SHELL_ERR_NO_ERROR) { - // Ensure process is closed on failure - wapp_shell_utils_pclose(fp); - return CMD_NO_EXIT(err); + output = CMD_NO_EXIT(err); + goto EXECUTE_COMMAND_CLOSE; } i32 st = EXIT_SUCCESS; err = get_output_status(fp, &st); if (err > SHELL_ERR_NO_ERROR) { - return CMD_NO_EXIT(err); + output = CMD_NO_EXIT(err); + goto EXECUTE_COMMAND_CLOSE; } - return (CMDResult){ + // Process is already closed in get_output_status + fp = NULL; + + output = (CMDResult){ .exited = true, .exit_code = st, .error = SHELL_ERR_NO_ERROR, }; + +EXECUTE_COMMAND_CLOSE: + if (fp) { + wapp_shell_utils_pclose(fp); + } + return output; } internal inline CMDError get_command_output(FILE *fp, @@ -93,7 +103,7 @@ internal inline CMDError get_command_output(FILE *fp, u64 max_out_length = OUT_BUF_LEN - 1; u64 buf_filled = 0; - while (fgets(out, max_out_length, fp)) { + while (fgets(out, (i32)max_out_length, fp)) { if (out_handling == SHELL_OUTPUT_CAPTURE && out_buf != NULL) { buf_filled += strlen(out); if (buf_filled >= buf_size) { diff --git a/src/common/shell/commander/commander.h b/src/common/shell/commander/commander.h index 729153f..2ed1c4f 100644 --- a/src/common/shell/commander/commander.h +++ b/src/common/shell/commander/commander.h @@ -2,6 +2,7 @@ #define COMMANDER_H #include "aliases.h" +#include "platform.h" #include #include @@ -30,9 +31,14 @@ typedef enum { typedef struct commander_result CMDResult; struct commander_result { - bool exited; - int exit_code; + i32 exit_code; CMDError error; + bool exited; + +#ifdef WAPP_PLATFORM_WINDOWS + #include "misc_utils.h" + wapp_misc_utils_padding_size(sizeof(bool) + sizeof(i32) + sizeof(CMDError)); +#endif // !WAPP_PLATFORM_WINDOWS }; // clang-format off diff --git a/src/common/shell/termcolour/win/termcolour_win.c b/src/common/shell/termcolour/win/termcolour_win.c index 8506e93..e20cd83 100644 --- a/src/common/shell/termcolour/win/termcolour_win.c +++ b/src/common/shell/termcolour/win/termcolour_win.c @@ -20,7 +20,7 @@ struct termcolour_data { wapp_misc_utils_padding_size(sizeof(HANDLE) + sizeof(WORD) + sizeof(WORD)); }; -internal TermcolourData init_data(void); +internal void init_data(TermcolourData *data); internal WORD colours[COUNT_TERM_COLOUR] = { [WAPP_TERM_COLOUR_FG_BLACK] = 0, @@ -42,31 +42,30 @@ internal WORD colours[COUNT_TERM_COLOUR] = { }; void print_coloured_text(const char *text, TerminalColour colour) { - persistent TermcolourData data = init_data(); - - if (color == WAPP_TERM_COLOUR_CLEAR) { - data->current_colour = data->default_colour; - } else { - data->current_colour = colours[colour]; + persistent TermcolourData data = {0}; + if (data.handle == 0) { + init_data(&data); } - SetConsoleTextAttribute(data->handle, data->current_colour); + if (colour == WAPP_TERM_COLOUR_CLEAR) { + data.current_colour = data.default_colour; + } else { + data.current_colour = colours[colour]; + } + + SetConsoleTextAttribute(data.handle, data.current_colour); printf("%s", text); } -internal TermcolourData init_data(void) { - TermcolourData data = {0}; - +internal void init_data(TermcolourData *data) { // create handle - data.handle = GetStdHandle(STD_OUTPUT_HANDLE); + data->handle = GetStdHandle(STD_OUTPUT_HANDLE); // get console colour information CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(data.handle, &csbi); - data.default_colour = csbi.wAttributes; - data.current_colour = data.default_colour; - - return data; + GetConsoleScreenBufferInfo(data->handle, &csbi); + data->default_colour = csbi.wAttributes; + data->current_colour = data->default_colour; } #endif // !WAPP_PLATFORM_WINDOWS diff --git a/src/common/shell/utils/shell_utils.h b/src/common/shell/utils/shell_utils.h index 718e2b0..a460fc9 100644 --- a/src/common/shell/utils/shell_utils.h +++ b/src/common/shell/utils/shell_utils.h @@ -2,15 +2,14 @@ #define SHELL_UTILS_H #include "platform.h" +#include -// clang-format off #ifdef WAPP_PLATFORM_WINDOWS - #define wapp_shell_utils_popen _popen + #define wapp_shell_utils_popen _popen #define wapp_shell_utils_pclose _pclose #else - #define wapp_shell_utils_popen popen + #define wapp_shell_utils_popen popen #define wapp_shell_utils_pclose pclose #endif /* ifdef WAPP_PLATFORM_WINDOWS */ -// clang-format on #endif // !SHELL_UTILS_H diff --git a/src/core/mem/arena/mem_arena.c b/src/core/mem/arena/mem_arena.c index 8d115c3..84a927b 100644 --- a/src/core/mem/arena/mem_arena.c +++ b/src/core/mem/arena/mem_arena.c @@ -1,6 +1,7 @@ #include "mem_arena.h" #include "aliases.h" #include "mem_utils.h" +#include "misc_utils.h" #include #include #include diff --git a/src/core/mem/libc/mem_libc_allocator.c b/src/core/mem/libc/mem_libc_allocator.c index 07ebea4..88b4e82 100644 --- a/src/core/mem/libc/mem_libc_allocator.c +++ b/src/core/mem/libc/mem_libc_allocator.c @@ -24,12 +24,15 @@ Allocator wapp_mem_libc_allocator(void) { } internal inline void *mem_libc_alloc(u64 size, void *alloc_obj) { + (void)alloc_obj; // Silence unused warnings return calloc(1, size); } #if 0 internal inline void *mem_libc_alloc_aligned(u64 size, u64 alignment, void *alloc_obj) { + (void)alloc_obj; // Silence unused warnings + void *output = aligned_alloc(alignment, size); if (output) { memset(output, 0, size); @@ -40,10 +43,16 @@ internal inline void *mem_libc_alloc_aligned(u64 size, u64 alignment, #endif internal inline void *mem_libc_realloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj) { + // Silence unused warnings + (void)alloc_obj; + (void)new_size; + return realloc(ptr, old_size); } internal inline void mem_libc_free(void **ptr, void *alloc_obj) { + (void)alloc_obj; // Silence unused warnings + if (!ptr || !(*ptr)) { return; } diff --git a/tests/allocator/test_allocator.c b/tests/allocator/test_allocator.c index 043eda7..3ad304d 100644 --- a/tests/allocator/test_allocator.c +++ b/tests/allocator/test_allocator.c @@ -26,7 +26,7 @@ TestFuncResult test_arena_allocator(void) { Allocator allocator = wapp_mem_arena_allocator_init(4096); bool result = allocator.obj != NULL && allocator.alloc != NULL && allocator.alloc_aligned != NULL && - allocator.realloc != NULL & allocator.realloc_aligned != NULL && + allocator.realloc != NULL && allocator.realloc_aligned != NULL && allocator.free == NULL; void *ptr = wapp_mem_allocator_alloc(&allocator, 20); result = result && (ptr != NULL); diff --git a/tests/arena/test_arena.c b/tests/arena/test_arena.c index 5aaef41..aba9d3d 100644 --- a/tests/arena/test_arena.c +++ b/tests/arena/test_arena.c @@ -48,21 +48,21 @@ TestFuncResult test_arena_alloc_fails_when_over_capacity(void) { } TestFuncResult test_arena_realloc_bigger_size(void) { - u64 count = 10; + u64 old_count = 10; u64 new_count = 20; - i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32)); + i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32)); - for (u64 i = 0; i < count; ++i) { - bytes[i] = i; + for (u64 i = 0; i < old_count; ++i) { + bytes[i] = (i32)i; } - i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32)); + i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32)); if (!new_bytes) { return wapp_tester_result(false); } for (u64 i = 0; i < new_count; ++i) { - if (i < count && new_bytes[i] != bytes[i]) { + if (i < old_count && new_bytes[i] != bytes[i]) { return wapp_tester_result(false); } } @@ -71,15 +71,15 @@ TestFuncResult test_arena_realloc_bigger_size(void) { } TestFuncResult test_arena_realloc_smaller_size(void) { - u64 count = 10; + u64 old_count = 10; u64 new_count = 5; - i32 *bytes = wapp_mem_arena_alloc(arena, count * sizeof(i32)); + i32 *bytes = wapp_mem_arena_alloc(arena, old_count * sizeof(i32)); - for (u64 i = 0; i < count; ++i) { - bytes[i] = i; + for (u64 i = 0; i < old_count; ++i) { + bytes[i] = (i32)i; } - i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, count * sizeof(i32), new_count * sizeof(i32)); + i32 *new_bytes = wapp_mem_arena_realloc(arena, bytes, old_count * sizeof(i32), new_count * sizeof(i32)); if (!new_bytes) { return wapp_tester_result(false); } diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c index 07dfea8..0df9655 100644 --- a/tests/shell_commander/test_shell_commander.c +++ b/tests/shell_commander/test_shell_commander.c @@ -26,12 +26,13 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { char buf[64] = {0}; char expected_output[64] = {0}; const char *msg = "hello world"; + u64 length = strlen(msg); sprintf(expected_output, "%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 && result.error == SHELL_ERR_NO_ERROR && - strcmp(buf, expected_output) == 0; + strncmp(buf, expected_output, length) == 0; return wapp_tester_result(succeeded); } @@ -39,9 +40,10 @@ TestFuncResult test_commander_cmd_out_buf_success(void) { TestFuncResult test_commander_cmd_out_buf_failure(void) { char buf[4] = {0}; const char *msg = "hello world"; + u64 length = strlen(msg); CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg); bool failed = !result.exited && result.exit_code != EXIT_SUCCESS && - result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0; + result.error == SHELL_ERR_OUT_BUF_FULL && strncmp(buf, msg, length) != 0; return wapp_tester_result(failed); }