From c8bee66744f9510d22308729aa3255f8e71c3e4e Mon Sep 17 00:00:00 2001 From: flysand7 Date: Sat, 24 Jun 2023 15:55:21 +1100 Subject: [PATCH] Fix integer overflow on SetFilePointer --- src/os_win/file.c | 6 ++---- test.bat | 4 +++- test/crt_functions.c | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/os_win/file.c b/src/os_win/file.c index 68a4ae7..b40989c 100644 --- a/src/os_win/file.c +++ b/src/os_win/file.c @@ -594,8 +594,7 @@ int fgetpos(FILE *restrict stream, fpos_t *restrict pos) { int fseek(FILE *stream, long int offset, int whence) { // Note(bumbread): the SEEK_SET, SEEK_CUR and SEEK_END are defined to match // the Windows constants, so no conversion is requierd between them. - LONG pos_hi = 0; - DWORD pos_lo = SetFilePointer(stream->handle, offset, &pos_hi, whence); + DWORD pos_lo = SetFilePointer(stream->handle, offset, NULL, whence); if(pos_lo == INVALID_SET_FILE_POINTER) { return -1L; } @@ -603,9 +602,8 @@ int fseek(FILE *stream, long int offset, int whence) { } int fsetpos(FILE *stream, const fpos_t *pos) { - LONG pos_hi = pos->offset >> 32; LONG pos_lo = (LONG)(pos->offset & 0xffffffff); - DWORD status = SetFilePointer(stream->handle, pos_lo, &pos_hi, FILE_BEGIN); + DWORD status = SetFilePointer(stream->handle, pos_lo, NULL, FILE_BEGIN); if(status == INVALID_SET_FILE_POINTER) { return 1; } diff --git a/test.bat b/test.bat index f507604..0f9abcc 100644 --- a/test.bat +++ b/test.bat @@ -1,3 +1,5 @@ clang -g test\%1.c -I include utf8.obj -nostdlib -mfma -lciabatta.lib -"./a.exe" +if %errorlevel%==0 ( + "./a.exe" +) diff --git a/test/crt_functions.c b/test/crt_functions.c index ac093ea..5a4da8c 100644 --- a/test/crt_functions.c +++ b/test/crt_functions.c @@ -198,6 +198,7 @@ struct Test { }; static void print_test_results(Test_Feature *features_head) { + prints(":: Printing test results\n"); int total_test_count = 0; int total_success_count = 0; for(Test_Feature *feature = features_head; feature != NULL; feature = feature->next) { @@ -245,6 +246,7 @@ Test_Feature *current_feature = NULL; #define FEATURE_START__(NAME, NUMBER) \ { \ + print_fmt(":: Running tests for %s\n", NAME); \ Test_Feature *feature = mem_alloc(sizeof(Test_Feature)); \ feature->next = current_feature; \ current_feature = feature; \ @@ -577,6 +579,7 @@ int main(int argc, char **argv) { TEST(file != NULL, "Created file is NULL"); TEST(fwrite(numbers, sizeof(int), 10, file) == 10, "fwrite didn't write all 10 objects"); TEST(fseek(file, 4*sizeof(int), SEEK_SET) == 0, "fseek couldn't seek to offset 4"); + // TEST(fflush(file) == 0, "fflush failed"); int num; TEST(fread(&num, sizeof(int), 1, file) == 1, "fread didn't read the int"); TEST(num == 4, "Wrong value read at offset 4");