]> granicus.if.org Git - transmission/commitdiff
Improve error handling when formatting error messages on Windows
authorMike Gelfand <mikedld@mikedld.com>
Sun, 11 Oct 2015 16:15:19 +0000 (16:15 +0000)
committerMike Gelfand <mikedld@mikedld.com>
Sun, 11 Oct 2015 16:15:19 +0000 (16:15 +0000)
libtransmission/utils.c

index 68f0228cc75c533faf107a9d6a118b8d716d0809..42c7556714aad9f1d01144e9009eb6e6641de5d0 100644 (file)
@@ -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;
 }