From 89427039c3b8204e7e86a5caa1522a2756872d4f Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Mon, 19 Dec 2022 00:20:06 -0500 Subject: [PATCH] [WINDOWS32] Remove CRNL from FormatMessage() result string Sometimes the error message is expected to contain a newline, other times it is not. Also the result of FormatMessage() sometimes ends in CRNL already: when printed via fprintf() the final newline is converted to CRNL, giving an output of CRCRNL which causes tests to fail to match. Remove any CR/NL chars from the result string. * src/job.c (reap_children): Add \n to the error message fprintf. (exec_command): Ditto. * src/w32/subproc/w32err.c (map_windows32_error_to_string): Remove any trailing CR or NL from the string before returning. --- src/job.c | 4 +-- src/w32/subproc/sub_proc.c | 2 +- src/w32/subproc/w32err.c | 72 +++++++++++++++++++------------------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/job.c b/src/job.c index 876898c8..1ac64ebc 100644 --- a/src/job.c +++ b/src/job.c @@ -861,7 +861,7 @@ reap_children (int block, int err) map_windows32_error_to_string from calling 'fatal', which will then call reap_children again */ if (werr && exit_code > 0 && exit_code < WSABASEERR) - fprintf (stderr, "make (e=%d): %s", exit_code, + fprintf (stderr, "make (e=%d): %s\n", exit_code, map_windows32_error_to_string (exit_code)); /* signal */ @@ -2552,7 +2552,7 @@ exec_command (char **argv, char **envp) exit_code = process_exit_code (hWaitPID); if (err) - fprintf (stderr, "make (e=%d, rc=%d): %s", + fprintf (stderr, "make (e=%d, rc=%d): %s\n", err, exit_code, map_windows32_error_to_string (err)); /* cleanup process */ diff --git a/src/w32/subproc/sub_proc.c b/src/w32/subproc/sub_proc.c index 501afc3e..5352bf4a 100644 --- a/src/w32/subproc/sub_proc.c +++ b/src/w32/subproc/sub_proc.c @@ -887,7 +887,7 @@ proc_stderr_thread(sub_process *pproc) for (;;) { if (ReadFile( (HANDLE)pproc->sv_stderr[0], &c, 1, &nread, NULL) == FALSE) { - map_windows32_error_to_string(GetLastError()); +/* map_windows32_error_to_string(GetLastError());*/ _endthreadex(0); } if (nread == 0) diff --git a/src/w32/subproc/w32err.c b/src/w32/subproc/w32err.c index eb201034..ad9049cd 100644 --- a/src/w32/subproc/w32err.c +++ b/src/w32/subproc/w32err.c @@ -44,42 +44,42 @@ map_windows32_error_to_string (DWORD ercode) { * static. (If and when we do need it to be in thread-local storage, * the corresponding GCC qualifier is '__thread'.) */ - static char szMessageBuffer[128]; - /* Fill message buffer with a default message in - * case FormatMessage fails - */ - wsprintf (szMessageBuffer, "Error %ld\n", ercode); + static char szMessageBuffer[128]; + DWORD ret; - /* - * Special code for winsock error handling. - */ - if (ercode > WSABASEERR) { -#if 0 - HMODULE hModule = GetModuleHandle("wsock32"); - if (hModule != NULL) { - FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, - hModule, - ercode, - LANG_NEUTRAL, - szMessageBuffer, - sizeof(szMessageBuffer), - NULL); - FreeLibrary(hModule); - } -#else - O (fatal, NILF, szMessageBuffer); -#endif - } else { - /* - * Default system message handling - */ - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - ercode, - LANG_NEUTRAL, - szMessageBuffer, - sizeof(szMessageBuffer), - NULL); + /* Fill message buffer with a default message in + * case FormatMessage fails + */ + wsprintf (szMessageBuffer, "Error %ld", ercode); + + /* + * Special code for winsock error handling. + */ + if (ercode > WSABASEERR) { + O (fatal, NILF, szMessageBuffer); + } + + /* + * Default system message handling + */ + ret = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + ercode, + LANG_NEUTRAL, + szMessageBuffer, + sizeof(szMessageBuffer), + NULL); + + if (ret) + { + char *cp; + for (cp = szMessageBuffer + ret - 1; cp >= szMessageBuffer; --cp) + { + if (*cp != '\r' && *cp != '\n') + break; + *cp = '\0'; } - return szMessageBuffer; + } + + return szMessageBuffer; }