]> git.proxmox.com Git - libgit2.git/commitdiff
Get utf8_size from WideCharToMultiByte instead of guessing it
authorSven Strickroth <email@cs-ware.de>
Fri, 1 Feb 2013 21:14:52 +0000 (22:14 +0100)
committerSven Strickroth <email@cs-ware.de>
Fri, 1 Feb 2013 21:17:34 +0000 (22:17 +0100)
Signed-off-by: Sven Strickroth <email@cs-ware.de>
src/errors.c
src/netops.c

index c64db7b8587458e522fa272fd78e19ded42cb5b0..1ab2894edd16a9daf9f153ad7355858118c04954 100644 (file)
@@ -61,11 +61,11 @@ void giterr_set(int error_class, const char *string, ...)
                                        (LPWSTR)&lpMsgBuf, 0, NULL);
 
                        if (size) {
-                               int utf8_size = size * 4 + 1;
+                               int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL);
 
-                               char *lpMsgBuf_utf8 = git__calloc(utf8_size, sizeof(char));
+                               char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char));
                                GITERR_CHECK_ALLOC(lpMsgBuf_utf8);
-                               WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, size, lpMsgBuf_utf8, utf8_size, NULL, NULL);
+                               WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL);
 
                                git_buf_PUTS(&buf, ": ");
                                git_buf_puts(&buf, lpMsgBuf_utf8);
index 851ed42190b53abd45fb8bd59c4acff9e28d70f0..c5554ef869bf986a13f5edde84ba3937fa9e7822 100644 (file)
@@ -46,10 +46,10 @@ static void net_set_error(const char *str)
        int size = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                                                          0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&err_str, 0, 0);
 
-       int utf8_size = size * 4 + 1;
-       char * err_str_utf8 = git__calloc(utf8_size, sizeof(char));
+       int utf8_size = WideCharToMultiByte(CP_UTF8, 0, err_str, -1, NULL, 0, NULL, NULL);
+       char * err_str_utf8 = git__malloc(utf8_size * sizeof(char));
        GITERR_CHECK_ALLOC(err_str_utf8);
-       WideCharToMultiByte(CP_UTF8, 0, err_str, size, err_str_utf8, utf8_size, NULL, NULL);
+       WideCharToMultiByte(CP_UTF8, 0, err_str, -1, err_str_utf8, utf8_size, NULL, NULL);
 
        giterr_set(GITERR_NET, "%s: %s", str, err_str_utf8);
        LocalFree(err_str);