Fix integer overflow on SetFilePointer

This commit is contained in:
flysand7 2023-06-24 15:55:21 +11:00
parent 1b19084f16
commit c8bee66744
3 changed files with 8 additions and 5 deletions

View File

@ -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;
}

View File

@ -1,3 +1,5 @@
clang -g test\%1.c -I include utf8.obj -nostdlib -mfma -lciabatta.lib
"./a.exe"
if %errorlevel%==0 (
"./a.exe"
)

View File

@ -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");