From 1ced76cdd1078066a3c95f78c5f33d05a3892737 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 19 Aug 2024 04:35:55 -0400 Subject: [PATCH] Fix broken cases of `Seek` usage in `_file_stream_proc` Handles `EINVAL`, among other fixes. --- core/os/file_windows.odin | 2 ++ core/os/os_windows.odin | 4 ++++ core/os/stream.odin | 8 ++++++++ core/sys/windows/winerror.odin | 1 + 4 files changed, 15 insertions(+) diff --git a/core/os/file_windows.odin b/core/os/file_windows.odin index 3f6f781aa..2a00b44b1 100644 --- a/core/os/file_windows.odin +++ b/core/os/file_windows.odin @@ -192,6 +192,8 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) { case 0: w = win32.FILE_BEGIN case 1: w = win32.FILE_CURRENT case 2: w = win32.FILE_END + case: + return 0, .Invalid_Whence } hi := i32(offset>>32) lo := i32(offset) diff --git a/core/os/os_windows.odin b/core/os/os_windows.odin index 273fe5af0..6fb0631cd 100644 --- a/core/os/os_windows.odin +++ b/core/os/os_windows.odin @@ -43,6 +43,7 @@ ERROR_BUFFER_OVERFLOW :: _Platform_Error(111) ERROR_INSUFFICIENT_BUFFER :: _Platform_Error(122) ERROR_MOD_NOT_FOUND :: _Platform_Error(126) ERROR_PROC_NOT_FOUND :: _Platform_Error(127) +ERROR_NEGATIVE_SEEK :: _Platform_Error(131) ERROR_DIR_NOT_EMPTY :: _Platform_Error(145) ERROR_ALREADY_EXISTS :: _Platform_Error(183) ERROR_ENVVAR_NOT_FOUND :: _Platform_Error(203) @@ -91,6 +92,9 @@ get_last_error :: proc "contextless" () -> Error { case win32.ERROR_INVALID_HANDLE: return .Invalid_File + case win32.ERROR_NEGATIVE_SEEK: + return .Invalid_Offset + case win32.ERROR_BAD_ARGUMENTS, win32.ERROR_INVALID_PARAMETER, diff --git a/core/os/stream.odin b/core/os/stream.odin index 8acbee489..61c7c3582 100644 --- a/core/os/stream.odin +++ b/core/os/stream.odin @@ -47,6 +47,14 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, } case .Seek: n, os_err = seek(fd, offset, int(whence)) + if os_err != nil { + switch whence { + case .Start, .Current, .End: + return 0, .Invalid_Offset + case: + return 0, .Invalid_Whence + } + } case .Size: n, os_err = file_size(fd) case .Destroy: diff --git a/core/sys/windows/winerror.odin b/core/sys/windows/winerror.odin index 8882dad71..d3df3b815 100644 --- a/core/sys/windows/winerror.odin +++ b/core/sys/windows/winerror.odin @@ -213,6 +213,7 @@ ERROR_BROKEN_PIPE : DWORD : 109 ERROR_CALL_NOT_IMPLEMENTED : DWORD : 120 ERROR_INSUFFICIENT_BUFFER : DWORD : 122 ERROR_INVALID_NAME : DWORD : 123 +ERROR_NEGATIVE_SEEK : DWORD : 131 ERROR_BAD_ARGUMENTS : DWORD : 160 ERROR_LOCK_FAILED : DWORD : 167 ERROR_ALREADY_EXISTS : DWORD : 183