From 71bff213ef585426925dc19508e8b2dae0515d67 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 17 Oct 2022 20:23:33 +0300 Subject: [PATCH] builtin: make the C. WIN32 API declarations more precise, to catch errors earlier (#16090) --- vlib/builtin/cfns.c.v | 33 ++++++++++++++++----------------- vlib/os/os.c.v | 14 +++++++------- vlib/term/ui/input_windows.c.v | 6 +++--- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index 6902f65f9f..3495e82b6d 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -226,15 +226,15 @@ fn C._fileno(int) int fn C._get_osfhandle(fd int) C.intptr_t -fn C.GetModuleFileName(hModule voidptr, lpFilename &u16, nSize u32) int +fn C.GetModuleFileName(hModule voidptr, lpFilename &u16, nSize u32) u32 fn C.GetModuleFileNameW(hModule voidptr, lpFilename &u16, nSize u32) u32 fn C.CreateFile(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32, dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr -fn C.CreateFileW(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32, dwFlagsAndAttributes u32, hTemplateFile voidptr) u32 +fn C.CreateFileW(lpFilename &u16, dwDesiredAccess u32, dwShareMode u32, lpSecurityAttributes &u16, dwCreationDisposition u32, dwFlagsAndAttributes u32, hTemplateFile voidptr) voidptr -fn C.GetFinalPathNameByHandleW(hFile voidptr, lpFilePath &u16, nSize u32, dwFlags u32) int +fn C.GetFinalPathNameByHandleW(hFile voidptr, lpFilePath &u16, nSize u32, dwFlags u32) u32 fn C.CreatePipe(hReadPipe &voidptr, hWritePipe &voidptr, lpPipeAttributes voidptr, nSize u32) bool @@ -247,9 +247,9 @@ fn C.GetComputerNameW(&u16, &u32) bool fn C.GetUserNameW(&u16, &u32) bool [trusted] -fn C.SendMessageTimeout() u32 +fn C.SendMessageTimeout() isize -fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) u32 +fn C.SendMessageTimeoutW(hWnd voidptr, msg u32, wParam &u16, lParam &u32, fuFlags u32, uTimeout u32, lpdwResult &u64) isize fn C.CreateProcessW(lpApplicationName &u16, lpCommandLine &u16, lpProcessAttributes voidptr, lpThreadAttributes voidptr, bInheritHandles bool, dwCreationFlags u32, lpEnvironment voidptr, lpCurrentDirectory &u16, lpStartupInfo voidptr, lpProcessInformation voidptr) bool @@ -257,33 +257,32 @@ fn C.ReadFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToRead u32, lpNumbe fn C.GetFileAttributesW(lpFileName &u8) u32 -fn C.RegQueryValueEx(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) voidptr +fn C.RegQueryValueEx(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) int fn C.RegQueryValueExW(hKey voidptr, lpValueName &u16, lp_reserved &u32, lpType &u32, lpData &u8, lpcbData &u32) int -fn C.RegOpenKeyEx(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) voidptr +fn C.RegOpenKeyEx(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) int fn C.RegOpenKeyExW(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) int -fn C.RegSetValueEx() voidptr +fn C.RegSetValueEx(hKey voidptr, lpValueName &u16, dwType u32, lpData &u16, cbData u32) int -fn C.RegSetValueExW(hKey voidptr, lpValueName &u16, reserved u32, dwType u32, lpData &u8, lpcbData u32) int +fn C.RegSetValueExW(hKey voidptr, lpValueName &u16, reserved u32, dwType u32, const_lpData &u8, cbData u32) int -fn C.RegCloseKey(hKey voidptr) +fn C.RegCloseKey(hKey voidptr) int -fn C.RemoveDirectory(lpPathName &u16) int +fn C.RemoveDirectory(lpPathName &u16) bool + +fn C.RemoveDirectoryW(lpPathName &u16) bool -// fn C.GetStdHandle() voidptr fn C.GetStdHandle(u32) voidptr -// fn C.SetConsoleMode() -fn C.SetConsoleMode(voidptr, u32) int +fn C.SetConsoleMode(voidptr, u32) bool -// fn C.GetConsoleMode() int -fn C.GetConsoleMode(voidptr, &u32) int +fn C.GetConsoleMode(voidptr, &u32) bool [trusted] -fn C.GetCurrentProcessId() int +fn C.GetCurrentProcessId() u32 fn C.wprintf() diff --git a/vlib/os/os.c.v b/vlib/os/os.c.v index 10eb44ba2d..a6d3bc3b47 100644 --- a/vlib/os/os.c.v +++ b/vlib/os/os.c.v @@ -496,8 +496,8 @@ pub fn rm(path string) ! { pub fn rmdir(path string) ! { $if windows { rc := C.RemoveDirectory(path.to_wide()) - if rc == 0 { - // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectorya - 0 is failure + if !rc { + // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectorya - 0 == false, is failure return error('Failed to remove "$path": ' + posix_get_error_msg(C.errno)) } } $else { @@ -651,8 +651,8 @@ pub fn executable() string { // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew final_len := C.GetFinalPathNameByHandleW(file, unsafe { &u16(&final_path[0]) }, max_path_buffer_size, 0) - if final_len < max_path_buffer_size { - sret := unsafe { string_from_wide2(&u16(&final_path[0]), final_len) } + if final_len < u32(max_path_buffer_size) { + sret := unsafe { string_from_wide2(&u16(&final_path[0]), int(final_len)) } defer { unsafe { sret.free() } } @@ -665,7 +665,7 @@ pub fn executable() string { } } } - res := unsafe { string_from_wide2(pu16_result, len) } + res := unsafe { string_from_wide2(pu16_result, int(len)) } return res } $if macos { @@ -852,8 +852,8 @@ pub fn real_path(fpath string) string { // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew final_len := C.GetFinalPathNameByHandleW(file, pu16_fullpath, max_path_buffer_size, 0) - if final_len < max_path_buffer_size { - rt := unsafe { string_from_wide2(pu16_fullpath, final_len) } + if final_len < u32(max_path_buffer_size) { + rt := unsafe { string_from_wide2(pu16_fullpath, int(final_len)) } srt := rt[4..] unsafe { res.free() } res = srt.clone() diff --git a/vlib/term/ui/input_windows.c.v b/vlib/term/ui/input_windows.c.v index b610c718c2..b6c2a5dbdf 100644 --- a/vlib/term/ui/input_windows.c.v +++ b/vlib/term/ui/input_windows.c.v @@ -45,17 +45,17 @@ pub fn init(cfg Config) &Context { panic('could not get stdin handle') } // save the current input mode, to be restored on exit - if C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) == 0 { + if !C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) { panic('could not get stdin console mode') } // enable extended input flags (see https://stackoverflow.com/a/46802726) // 0x80 == C.ENABLE_EXTENDED_FLAGS - if C.SetConsoleMode(stdin_handle, 0x80) == 0 { + if !C.SetConsoleMode(stdin_handle, 0x80) { panic('could not set raw input mode') } // enable window and mouse input events. - if C.SetConsoleMode(stdin_handle, C.ENABLE_WINDOW_INPUT | C.ENABLE_MOUSE_INPUT) == 0 { + if !C.SetConsoleMode(stdin_handle, C.ENABLE_WINDOW_INPUT | C.ENABLE_MOUSE_INPUT) { panic('could not set raw input mode') } // store the current title, so restore_terminal_state can get it back