diff options
author | Sven Strickroth <email@cs-ware.de> | 2013-02-01 16:17:34 +0100 |
---|---|---|
committer | Sven Strickroth <email@cs-ware.de> | 2013-02-01 21:54:32 +0100 |
commit | b0dc81f055d4eb523c92bc859d5641e72d343b00 (patch) | |
tree | 0981338d512b39cb4e199769965a496abcde7ad6 /src/errors.c | |
parent | aa3bf89df21c44f22fe70b4aac9109646fd06b48 (diff) | |
download | libgit2-b0dc81f055d4eb523c92bc859d5641e72d343b00.tar.gz |
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 <email@cs-ware.de>
Diffstat (limited to 'src/errors.c')
-rw-r--r-- | src/errors.c | 19 |
1 files changed, 14 insertions, 5 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); |