From ca9e48d741ad4c1e6a004137fdda530bce22407a Mon Sep 17 00:00:00 2001 From: chooyy Date: Thu, 18 Jun 2026 15:24:43 +0800 Subject: use FormatMessageW for Windows system error messages --- core/src/xmake/os/strerror.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/xmake/os/strerror.c b/core/src/xmake/os/strerror.c index c1ae41039..d54671fef 100644 --- a/core/src/xmake/os/strerror.c +++ b/core/src/xmake/os/strerror.c @@ -64,8 +64,16 @@ tb_int_t xm_os_strerror(lua_State *lua) { lua_pushstring(lua, strerr); } else { #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - tb_char_t strerr[128] = { 0 }; - tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)GetLastError()); + DWORD error_code = GetLastError(); + tb_char_t strerr[128 * 2] = { 0 }; + tb_wchar_t wstrerr[128] = { 0 }; + if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + wstrerr, tb_arrayn(wstrerr), NULL)) { + WideCharToMultiByte(CP_UTF8, 0, wstrerr, (tb_int_t)tb_wcslen(wstrerr), strerr, sizeof(strerr), tb_null, tb_null); + } else { + tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)error_code); + } lua_pushstring(lua, strerr); #else lua_pushstring(lua, strerror(errno)); -- cgit v1.3.1 From 115e8042a9811b1248b339b2661b457a24e7bd9f Mon Sep 17 00:00:00 2001 From: chooyy Date: Thu, 18 Jun 2026 22:47:52 +0800 Subject: improve strerror fallback --- core/src/xmake/os/strerror.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/src/xmake/os/strerror.c b/core/src/xmake/os/strerror.c index d54671fef..a55ff6d2f 100644 --- a/core/src/xmake/os/strerror.c +++ b/core/src/xmake/os/strerror.c @@ -69,9 +69,11 @@ tb_int_t xm_os_strerror(lua_State *lua) { tb_wchar_t wstrerr[128] = { 0 }; if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - wstrerr, tb_arrayn(wstrerr), NULL)) { - WideCharToMultiByte(CP_UTF8, 0, wstrerr, (tb_int_t)tb_wcslen(wstrerr), strerr, sizeof(strerr), tb_null, tb_null); - } else { + wstrerr, tb_arrayn(wstrerr), NULL) && + tb_wcslen(wstrerr) > 0) { + WideCharToMultiByte(CP_UTF8, 0, wstrerr, -1, strerr, sizeof(strerr), tb_null, tb_null); + } + if (strerr[0] == '\0') { tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)error_code); } lua_pushstring(lua, strerr); -- cgit v1.3.1 From 62fa217c932307158cce7534c96de6fdb80735de Mon Sep 17 00:00:00 2001 From: chooyy Date: Thu, 18 Jun 2026 22:59:49 +0800 Subject: strip trailing \r\n --- core/src/xmake/os/strerror.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/xmake/os/strerror.c b/core/src/xmake/os/strerror.c index a55ff6d2f..e8c461ea7 100644 --- a/core/src/xmake/os/strerror.c +++ b/core/src/xmake/os/strerror.c @@ -67,10 +67,14 @@ tb_int_t xm_os_strerror(lua_State *lua) { DWORD error_code = GetLastError(); tb_char_t strerr[128 * 2] = { 0 }; tb_wchar_t wstrerr[128] = { 0 }; + tb_size_t len = 0; if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), wstrerr, tb_arrayn(wstrerr), NULL) && - tb_wcslen(wstrerr) > 0) { + (len = tb_wcslen(wstrerr)) > 0) { + while (len > 0 && (wstrerr[len - 1] == L'\r' || wstrerr[len - 1] == L'\n')) { + wstrerr[--len] = L'\0'; + } WideCharToMultiByte(CP_UTF8, 0, wstrerr, -1, strerr, sizeof(strerr), tb_null, tb_null); } if (strerr[0] == '\0') { -- cgit v1.3.1