Skip to content

Commit 2c0640f

Browse files
committed
Windows CP_UTF8 conv improved error handling
improve err handling for MultiByteToWideChar and WideCharToMultiByte: 1. print the error obtained by GetLastError() rather then retval, because: 2. the functions return 0 on error, so 3. check equality to zero, not <=0 3. pass {MB,WC}_ERR_INVALID_CHARS to avoid silently replacing inconvertible chars with a placeholder (perhaps not a good idea for filenames, although it should not occur for valid UTF-8)
1 parent 91de28c commit 2c0640f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/gpujpeg_common.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@
112112
static wchar_t*
113113
mbs_to_wstr_helper(const char* mbstr, wchar_t* wstr_buf, size_t wstr_len)
114114
{
115-
const int size_needed = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
116-
if (size_needed <= 0) {
117-
ERROR_MSG("MultiByteToWideChar returned: %d (0x%x)!\n", size_needed, size_needed);
115+
const int size_needed = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
116+
if ( size_needed == 0 ) {
117+
ERROR_MSG("MultiByteToWideChar error: %d (0x%x)!\n", GetLastError(), GetLastError());
118118
return NULL;
119119
}
120120
if (size_needed > (int) wstr_len) {
121121
ERROR_MSG("buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, wstr_len);
122122
return NULL;
123123
}
124-
MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wstr_buf, size_needed);
124+
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, wstr_buf, size_needed);
125125
return wstr_buf;
126126
}
127127
#define mbs_to_wstr(tstr) mbs_to_wstr_helper(tstr, (wchar_t[1024]){0}, 1024)

src/main.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ static char*
6666
tstr_to_mbs_helper(const TCHAR* tstr, char* mbs_buf, size_t mbs_len)
6767
{
6868
#if _UNICODE
69-
const int size_needed = WideCharToMultiByte(CP_UTF8, 0, tstr, -1, NULL, 0, NULL, NULL);
70-
if (size_needed <= 0) {
71-
fprintf(stderr, "MultiByteToWideChar returned: %d (0x%x)!\n", size_needed, size_needed);
69+
const int size_needed = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, tstr, -1, NULL, 0, NULL, NULL);
70+
if ( size_needed == 0 ) {
71+
fprintf(stderr, "WideCharToMultiByte error: %d (0x%x)!\n", GetLastError(), GetLastError());
7272
return NULL;
7373
}
7474
if (size_needed > (int) mbs_len) {
7575
fprintf(stderr, "buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, mbs_len);
7676
return NULL;
7777
}
78-
WideCharToMultiByte(CP_UTF8, 0, tstr, -1, mbs_buf, size_needed, NULL, NULL);
78+
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, tstr, -1, mbs_buf, size_needed, NULL, NULL);
7979
return mbs_buf;
8080
#else
8181
(void) mbs_buf, (void) mbs_len;
@@ -88,16 +88,16 @@ tstr_to_mbs_helper(const TCHAR* tstr, char* mbs_buf, size_t mbs_len)
8888
static wchar_t*
8989
mbs_to_wstr_helper(const char* mbstr, wchar_t* wstr_buf, size_t wstr_len)
9090
{
91-
const int size_needed = MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, NULL, 0);
92-
if (size_needed <= 0) {
93-
fprintf(stderr, "MultiByteToWideChar returned: %d (0x%x)!\n", size_needed, size_needed);
91+
const int size_needed = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
92+
if (size_needed == 0) {
93+
fprintf(stderr, "MultiByteToWideChar error: %d (0x%x)!\n", GetLastError(), GetLastError());
9494
return NULL;
9595
}
9696
if (size_needed > (int) wstr_len) {
9797
fprintf(stderr, "buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, wstr_len);
9898
return NULL;
9999
}
100-
MultiByteToWideChar(CP_UTF8, 0, mbstr, -1, wstr_buf, size_needed);
100+
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, wstr_buf, size_needed);
101101
return wstr_buf;
102102
}
103103
#define mbs_to_wstr(tstr) mbs_to_wstr_helper(tstr, (wchar_t[1024]){0}, 1024)

0 commit comments

Comments
 (0)