From 6eb2eaa7538155a2aed987930de2e7f2b5377ba1 Mon Sep 17 00:00:00 2001 From: Marc Schlaich Date: Thu, 11 Dec 2014 10:26:15 +0100 Subject: [PATCH] win: set fallback message if FormatMessage fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FormatMessage can fail, e.g. with ERROR_MUI_FILE_NOT_FOUND. In this case no error message was set and uv_dlerror wrongly returned "no error". With this patch the fallback error message "error: " is generated when FormatMessage fails. PR-URL: https://github.com/libuv/libuv/pull/59 Reviewed-By: Bert Belder Reviewed-By: Saúl Ibarra Corretgé --- src/win/dl.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/win/dl.c b/src/win/dl.c index d5b8f7c7d..059cef80a 100644 --- a/src/win/dl.c +++ b/src/win/dl.c @@ -69,17 +69,37 @@ const char* uv_dlerror(uv_lib_t* lib) { } +static void uv__format_fallback_error(uv_lib_t* lib, int errorno){ + DWORD_PTR args[1] = { (DWORD_PTR) errorno }; + LPSTR fallback_error = "error: %1!d!"; + + FormatMessageA(FORMAT_MESSAGE_FROM_STRING | + FORMAT_MESSAGE_ARGUMENT_ARRAY | + FORMAT_MESSAGE_ALLOCATE_BUFFER, + fallback_error, 0, 0, + (LPSTR) &lib->errmsg, + 0, (va_list*) args); +} + + + static int uv__dlerror(uv_lib_t* lib, int errorno) { + DWORD res; + if (lib->errmsg) { LocalFree((void*)lib->errmsg); lib->errmsg = NULL; } if (errorno) { - FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno, - MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), - (LPSTR)&lib->errmsg, 0, NULL); + res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), + (LPSTR) &lib->errmsg, 0, NULL); + if (!res) { + uv__format_fallback_error(lib, errorno); + } } return errorno ? -1 : 0;