]> git.proxmox.com Git - libgit2.git/commitdiff
Win32: Make sure error messages are consistently UTF-8 encoded
authorSven Strickroth <email@cs-ware.de>
Fri, 1 Feb 2013 15:17:34 +0000 (16:17 +0100)
committerSven Strickroth <email@cs-ware.de>
Fri, 1 Feb 2013 20:54:32 +0000 (21:54 +0100)
W/o this a libgit2 error message could have a mixed encoding:
e.g. a filename in UTF-8 combined with a native Windows error message
encoded with the local code page.

Signed-off-by: Sven Strickroth <email@cs-ware.de>
src/errors.c
src/netops.c

index d9827fb2b184681e7f9f91a2f252c54f93f26e93..c64db7b8587458e522fa272fd78e19ded42cb5b0 100644 (file)
@@ -52,16 +52,25 @@ void giterr_set(int error_class, const char *string, ...)
        if (error_class == GITERR_OS) {
 #ifdef GIT_WIN32
                if (win32_error_code) {
-                       char *lpMsgBuf;
-
-                       if (FormatMessageA(
+                       LPWSTR lpMsgBuf = NULL;
+                       int size = FormatMessageW(
                                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                        FORMAT_MESSAGE_FROM_SYSTEM |
                                        FORMAT_MESSAGE_IGNORE_INSERTS,
-                                       NULL, win32_error_code, 0, (LPSTR)&lpMsgBuf, 0, NULL)) {
+                                       NULL, win32_error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+                                       (LPWSTR)&lpMsgBuf, 0, NULL);
+
+                       if (size) {
+                               int utf8_size = size * 4 + 1;
+
+                               char *lpMsgBuf_utf8 = git__calloc(utf8_size, sizeof(char));
+                               GITERR_CHECK_ALLOC(lpMsgBuf_utf8);
+                               WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, size, lpMsgBuf_utf8, utf8_size, NULL, NULL);
+
                                git_buf_PUTS(&buf, ": ");
-                               git_buf_puts(&buf, lpMsgBuf);
+                               git_buf_puts(&buf, lpMsgBuf_utf8);
                                LocalFree(lpMsgBuf);
+                               git__free(lpMsgBuf_utf8);
                        }
 
                        SetLastError(0);
index 59e6bda1e0ceab01039ecfac431d0cb3a03cf074..851ed42190b53abd45fb8bd59c4acff9e28d70f0 100644 (file)
 #ifdef GIT_WIN32
 static void net_set_error(const char *str)
 {
-       int size, error = WSAGetLastError();
-       LPSTR err_str = NULL;
+       int error = WSAGetLastError();
+       LPWSTR err_str = NULL;
 
-       size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
-                            0, error, 0, (LPSTR)&err_str, 0, 0);
+       int size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+                                                         0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&err_str, 0, 0);
 
-       GIT_UNUSED(size);
+       int utf8_size = size * 4 + 1;
+       char * err_str_utf8 = git__calloc(utf8_size, sizeof(char));
+       GITERR_CHECK_ALLOC(err_str_utf8);
+       WideCharToMultiByte(CP_UTF8, 0, err_str, size, err_str_utf8, utf8_size, NULL, NULL);
 
-       giterr_set(GITERR_NET, "%s: %s", str, err_str);
+       giterr_set(GITERR_NET, "%s: %s", str, err_str_utf8);
        LocalFree(err_str);
+       git__free(err_str_utf8);
 }
 #else
 static void net_set_error(const char *str)