mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-03 00:43:41 +00:00
Merge pull request #1303 from csware/win32_consistent_error_encoding
Win32: Make sure error messages are consistently UTF-8 encoded
This commit is contained in:
commit
e963166019
@ -28,6 +28,7 @@
|
||||
# include <windows.h>
|
||||
# include "win32/msvc-compat.h"
|
||||
# include "win32/mingw-compat.h"
|
||||
# include "win32/error.h"
|
||||
# ifdef GIT_THREADS
|
||||
# include "win32/pthread.h"
|
||||
#endif
|
||||
|
17
src/errors.c
17
src/errors.c
@ -51,18 +51,11 @@ 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(
|
||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
NULL, win32_error_code, 0, (LPSTR)&lpMsgBuf, 0, NULL)) {
|
||||
git_buf_PUTS(&buf, ": ");
|
||||
git_buf_puts(&buf, lpMsgBuf);
|
||||
LocalFree(lpMsgBuf);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
17
src/netops.c
17
src/netops.c
@ -40,16 +40,15 @@
|
||||
#ifdef GIT_WIN32
|
||||
static void net_set_error(const char *str)
|
||||
{
|
||||
int size, error = WSAGetLastError();
|
||||
LPSTR err_str = NULL;
|
||||
int error = WSAGetLastError();
|
||||
char * win32_error = git_win32_get_error_message(error);
|
||||
|
||||
size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
0, error, 0, (LPSTR)&err_str, 0, 0);
|
||||
|
||||
GIT_UNUSED(size);
|
||||
|
||||
giterr_set(GITERR_NET, "%s: %s", str, err_str);
|
||||
LocalFree(err_str);
|
||||
if (win32_error) {
|
||||
giterr_set(GITERR_NET, "%s: %s", str, win32_error);
|
||||
git__free(win32_error);
|
||||
} else {
|
||||
giterr_set(GITERR_NET, str);
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void net_set_error(const char *str)
|
||||
|
41
src/win32/error.c
Normal file
41
src/win32/error.c
Normal file
@ -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;
|
||||
}
|
13
src/win32/error.h
Normal file
13
src/win32/error.h
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user