Commit
toUnicode is just expanding bytes to short, and re-interpret them UTF16, not regarding original encoding. This works well for pure ASCII, but is not really defined for other encodings. We should prefer using UTF8 by default for all the image-VM string transfer.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,10 @@ void *ioLoadModule(char *pluginName) | |
TCHAR *name; | ||
|
||
#ifdef UNICODE | ||
name = toUnicode(pluginName); | ||
int len = MultiByteToWideChar(CP_UTF8, 0, pluginName, -1, NULL, 0); | ||
if (len <= 0) return 0; /* invalid UTF8 ? */ | ||
name = alloca(len); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
krono
Member
|
||
if (MultiByteToWideChar(CP_UTF8, 0, pluginName, -1, name, len) == 0) return 0; | ||
#else | ||
name = pluginName; | ||
#endif | ||
|
2 comments
on commit 98fc85d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, no, the terminating NULL should be copied when we pass -1 for original string length.
I think I had to re-read the docs a few times before I convince myself. This part is clear:
MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character. To null-terminate an output string for this function, the application should pass in -1 or explicitly count the terminating null character for the input string.
Then the result, less so:
Returns the number of characters written to the buffer indicated by lpWideCharStr if successful. If the function succeeds and cchWideChar is 0, the return value is the required size, in characters, for the buffer indicated by lpWideCharStr.
It's the required size for the buffer, in characters, not the number of characters for the string (wcslen), so I understand it is including the terminating NULL, if ever we asked for it (with -1 or with strlen()+1).
To be sure:
#include <stdio.h>
#include <windows.h>
int main() {
char *foo="foo";
int len = MultiByteToWideChar(CP_UTF8,0,foo,-1,NULL,0);
printf("len=%d\n",len);
return 0;
}
Then
$ i686-w64-mingw32-gcc test.c
$ ./a.exe
$ len=4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good!
Err!!! It should be
alloca(len*sizeof(WCHAR))