From b0dc81f055d4eb523c92bc859d5641e72d343b00 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 16:17:34 +0100 Subject: [PATCH 1/4] Win32: Make sure error messages are consistently UTF-8 encoded 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 --- src/errors.c | 19 ++++++++++++++----- src/netops.c | 16 ++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/errors.c b/src/errors.c index d9827fb2b..c64db7b85 100644 --- a/src/errors.c +++ b/src/errors.c @@ -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); diff --git a/src/netops.c b/src/netops.c index 59e6bda1e..851ed4219 100644 --- a/src/netops.c +++ b/src/netops.c @@ -40,16 +40,20 @@ #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) From 89ad1c57a351809d3173e22a26c84e7a16adbe6b Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:14:52 +0100 Subject: [PATCH 2/4] Get utf8_size from WideCharToMultiByte instead of guessing it Signed-off-by: Sven Strickroth --- src/errors.c | 6 +++--- src/netops.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/errors.c b/src/errors.c index c64db7b85..1ab2894ed 100644 --- a/src/errors.c +++ b/src/errors.c @@ -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); diff --git a/src/netops.c b/src/netops.c index 851ed4219..c5554ef86 100644 --- a/src/netops.c +++ b/src/netops.c @@ -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); From bd25a302d3232dea375f226602fdcdb3a83abcdc Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:22:26 +0100 Subject: [PATCH 3/4] Improved error handling Signed-off-by: Sven Strickroth --- src/errors.c | 11 +++++++++-- src/netops.c | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/errors.c b/src/errors.c index 1ab2894ed..56f07b624 100644 --- a/src/errors.c +++ b/src/errors.c @@ -64,8 +64,15 @@ void giterr_set(int error_class, const char *string, ...) int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL); char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char)); - GITERR_CHECK_ALLOC(lpMsgBuf_utf8); - WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL); + if (lpMsgBuf_utf8 == NULL) { + LocalFree(lpMsgBuf); + return; + } + if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) { + LocalFree(lpMsgBuf); + git__free(lpMsgBuf_utf8); + return; + } git_buf_PUTS(&buf, ": "); git_buf_puts(&buf, lpMsgBuf_utf8); diff --git a/src/netops.c b/src/netops.c index c5554ef86..86579c7f2 100644 --- a/src/netops.c +++ b/src/netops.c @@ -48,8 +48,16 @@ static void net_set_error(const char *str) 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, -1, err_str_utf8, utf8_size, NULL, NULL); + if (err_str_utf8 == NULL) { + LocalFree(err_str); + return; + } + + if (!WideCharToMultiByte(CP_UTF8, 0, err_str, -1, err_str_utf8, utf8_size, NULL, NULL)) { + LocalFree(err_str); + git__free(err_str_utf8); + return; + } giterr_set(GITERR_NET, "%s: %s", str, err_str_utf8); LocalFree(err_str); From c70455c75e30bf2a4cc5abdf4229d12d0e6cf159 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:53:51 +0100 Subject: [PATCH 4/4] Deduplicate FormatMessage UTF-16 to UTF-8 conversion code Signed-off-by: Sven Strickroth --- src/common.h | 1 + src/errors.c | 33 +++++---------------------------- src/netops.c | 25 ++++++------------------- src/win32/error.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/win32/error.h | 13 +++++++++++++ 5 files changed, 66 insertions(+), 47 deletions(-) create mode 100644 src/win32/error.c create mode 100644 src/win32/error.h diff --git a/src/common.h b/src/common.h index 23a1e250a..ca203ee5c 100644 --- a/src/common.h +++ b/src/common.h @@ -28,6 +28,7 @@ # include # include "win32/msvc-compat.h" # include "win32/mingw-compat.h" +# include "win32/error.h" # ifdef GIT_THREADS # include "win32/pthread.h" #endif diff --git a/src/errors.c b/src/errors.c index 56f07b624..3aa1757b2 100644 --- a/src/errors.c +++ b/src/errors.c @@ -51,34 +51,11 @@ void giterr_set(int error_class, const char *string, ...) if (error_class == GITERR_OS) { #ifdef GIT_WIN32 - if (win32_error_code) { - LPWSTR lpMsgBuf = NULL; - int size = FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, win32_error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPWSTR)&lpMsgBuf, 0, NULL); - - if (size) { - int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL); - - char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char)); - if (lpMsgBuf_utf8 == NULL) { - LocalFree(lpMsgBuf); - return; - } - if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) { - LocalFree(lpMsgBuf); - git__free(lpMsgBuf_utf8); - return; - } - - git_buf_PUTS(&buf, ": "); - git_buf_puts(&buf, lpMsgBuf_utf8); - LocalFree(lpMsgBuf); - git__free(lpMsgBuf_utf8); - } + char * win32_error = git_win32_get_error_message(win32_error_code); + if (win32_error) { + git_buf_PUTS(&buf, ": "); + git_buf_puts(&buf, win32_error); + git__free(win32_error); SetLastError(0); } diff --git a/src/netops.c b/src/netops.c index 86579c7f2..fd83a1cc3 100644 --- a/src/netops.c +++ b/src/netops.c @@ -41,27 +41,14 @@ static void net_set_error(const char *str) { int error = WSAGetLastError(); - LPWSTR err_str = NULL; + char * win32_error = git_win32_get_error_message(error); - 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 = WideCharToMultiByte(CP_UTF8, 0, err_str, -1, NULL, 0, NULL, NULL); - char * err_str_utf8 = git__malloc(utf8_size * sizeof(char)); - if (err_str_utf8 == NULL) { - LocalFree(err_str); - return; + if (win32_error) { + giterr_set(GITERR_NET, "%s: %s", str, win32_error); + git__free(win32_error); + } else { + giterr_set(GITERR_NET, str); } - - if (!WideCharToMultiByte(CP_UTF8, 0, err_str, -1, err_str_utf8, utf8_size, NULL, NULL)) { - LocalFree(err_str); - git__free(err_str_utf8); - return; - } - - 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) diff --git a/src/win32/error.c b/src/win32/error.c new file mode 100644 index 000000000..3851ff099 --- /dev/null +++ b/src/win32/error.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "common.h" +#include "error.h" + +char *git_win32_get_error_message(DWORD error_code) +{ + LPWSTR lpMsgBuf = NULL; + + if (!error_code) + return NULL; + + if (FormatMessageW( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPWSTR)&lpMsgBuf, 0, NULL)) { + int utf8_size = WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, NULL, 0, NULL, NULL); + + char *lpMsgBuf_utf8 = git__malloc(utf8_size * sizeof(char)); + if (lpMsgBuf_utf8 == NULL) { + LocalFree(lpMsgBuf); + return NULL; + } + if (!WideCharToMultiByte(CP_UTF8, 0, lpMsgBuf, -1, lpMsgBuf_utf8, utf8_size, NULL, NULL)) { + LocalFree(lpMsgBuf); + git__free(lpMsgBuf_utf8); + return NULL; + } + + LocalFree(lpMsgBuf); + return lpMsgBuf_utf8; + } + return NULL; +} diff --git a/src/win32/error.h b/src/win32/error.h new file mode 100644 index 000000000..12947a2e6 --- /dev/null +++ b/src/win32/error.h @@ -0,0 +1,13 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#ifndef INCLUDE_git_win32_error_h__ +#define INCLUDE_git_win32_error_h__ + +extern char *git_win32_get_error_message(DWORD error_code); + +#endif