From b0dc81f055d4eb523c92bc859d5641e72d343b00 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 16:17:34 +0100 Subject: 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 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/errors.c') 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); -- cgit v1.2.1 From 89ad1c57a351809d3173e22a26c84e7a16adbe6b Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:14:52 +0100 Subject: Get utf8_size from WideCharToMultiByte instead of guessing it Signed-off-by: Sven Strickroth --- src/errors.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/errors.c') 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); -- cgit v1.2.1 From bd25a302d3232dea375f226602fdcdb3a83abcdc Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:22:26 +0100 Subject: Improved error handling Signed-off-by: Sven Strickroth --- src/errors.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/errors.c') 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); -- cgit v1.2.1 From c70455c75e30bf2a4cc5abdf4229d12d0e6cf159 Mon Sep 17 00:00:00 2001 From: Sven Strickroth Date: Fri, 1 Feb 2013 22:53:51 +0100 Subject: Deduplicate FormatMessage UTF-16 to UTF-8 conversion code Signed-off-by: Sven Strickroth --- src/errors.c | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'src/errors.c') 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); } -- cgit v1.2.1