From 8beea92859b7c1251b8765f5c4b772330555b082 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Sun, 11 Oct 2015 16:15:19 +0000 Subject: [PATCH] Improve error handling when formatting error messages on Windows --- libtransmission/utils.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 68f0228cc..42c755671 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -1121,18 +1121,21 @@ tr_win32_format_message (uint32_t code) FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, 0, (LPWSTR)&wide_text, 0, NULL); + if (wide_size == 0) + return tr_strdup_printf ("Unknown error (0x%08x)", code); if (wide_size != 0 && wide_text != NULL) text = tr_win32_native_to_utf8 (wide_text, wide_size); LocalFree (wide_text); - /* Most (all?) messages contain "\r\n" in the end, chop it */ - text_size = strlen (text); - while (text_size > 0 && - text[text_size - 1] >= '\0' && - text[text_size - 1] <= ' ') - text[--text_size] = '\0'; + if (text != NULL) + { + /* Most (all?) messages contain "\r\n" in the end, chop it */ + text_size = strlen (text); + while (text_size > 0 && isspace ((uint8_t) text[text_size - 1])) + text[--text_size] = '\0'; + } return text; } -- 2.49.0