From d025484ae350f7d3a1da9fb69c162b84244e91fb Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 27 Feb 2023 12:34:19 +0100 Subject: [PATCH] get_win_error: drop trailing Some occurences of this call (net_udp.cpp, dlfunc.c) don't expect the string to be terminated so it is perhaps better to drop it at the source. --- src/utils/net.c | 2 +- src/utils/windows.c | 13 ++++++++++--- src/win32_gl_common.c | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/utils/net.c b/src/utils/net.c index c6ec3a075..99172e238 100644 --- a/src/utils/net.c +++ b/src/utils/net.c @@ -282,7 +282,7 @@ bool get_local_addresses(struct sockaddr_storage *addrs, size_t *len, int ip_ver if (dwRetVal == ERROR_NO_DATA) printf("\tNo addresses were found for the requested parameters\n"); else { - log_msg(LOG_LEVEL_ERROR, "Error: %s", get_win_error(dwRetVal)); + log_msg(LOG_LEVEL_ERROR, "Error: %s\n", get_win_error(dwRetVal)); if (pAddresses) free(pAddresses); return false; diff --git a/src/utils/windows.c b/src/utils/windows.c index f73abd121..6750bcc0f 100644 --- a/src/utils/windows.c +++ b/src/utils/windows.c @@ -122,7 +122,7 @@ const char *hresult_to_str(HRESULT res) { /** * @param error error code (typically GetLastError()) * - * @note returned error string is typically -terminated + * @note returned error string is _not_ -terminated (stripped from FormatMessage) */ const char *get_win_error(DWORD error) { _Thread_local static char buf[1024] = "(unknown)"; @@ -134,10 +134,17 @@ const char *get_win_error(DWORD error) { sizeof buf, // size of msgbuf, bytes NULL) // va_list of arguments ) { + char *end = buf + strlen(buf) - 1; + if (end > buf && *end == '\n') { // skip LF + *end-- = '\0'; + } + if (end > buf && *end == '\r') { // skip CR + *end-- = '\0'; + } return buf; } - snprintf(buf, sizeof buf, "[Could not find a description for error # %#lx.]\n", error); + snprintf(buf, sizeof buf, "[Could not find a description for error # %#lx.]", error); return buf; } @@ -153,7 +160,7 @@ const char *win_wstr_to_str(const wchar_t *wstr) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { log_msg(LOG_LEVEL_ERROR, "win_wstr_to_str: Insufficient buffer length %zd, please report to %s!\n", sizeof res, PACKAGE_BUGREPORT); } else { - log_msg(LOG_LEVEL_ERROR, "win_wstr_to_str: %s", get_win_error(GetLastError())); + log_msg(LOG_LEVEL_ERROR, "win_wstr_to_str: %s\n", get_win_error(GetLastError())); } } return res; diff --git a/src/win32_gl_common.c b/src/win32_gl_common.c index a9d96bb5f..1ff9f1d5a 100644 --- a/src/win32_gl_common.c +++ b/src/win32_gl_common.c @@ -193,7 +193,7 @@ failed: static void PrintError(DWORD err) { // Display the error message and exit the process - log_msg(LOG_LEVEL_FATAL, "win32_gl_common %ld: %s", err, get_win_error(err)); + log_msg(LOG_LEVEL_FATAL, "win32_gl_common %ld: %s\n", err, get_win_error(err)); } static BOOL SetGLFormat(HDC hdc)