#840: Fixes fixed buffer size in Fl::args_to_utf8() (fl_call_main)

This commit is contained in:
MatthiasWM 2023-12-15 12:56:06 +01:00
parent a118930d95
commit fdf578d936

View File

@ -70,7 +70,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
int i;
int argc = 0;
char** argv = NULL;
char strbuf[2048];
/*
* If we are compiling in debug mode, open a console window so
@ -97,15 +96,34 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
/* Convert the command line arguments to UTF-8 */
for (i = 0; i < argc; i++) {
int ret = WideCharToMultiByte(CP_UTF8, /* CodePage */
0, /* dwFlags */
wideArgv[i], /* lpWideCharStr */
-1, /* cchWideChar */
strbuf, /* lpMultiByteStr */
sizeof(strbuf), /* cbMultiByte */
NULL, /* lpDefaultChar */
NULL); /* lpUsedDefaultChar */
argv[i] = _strdup(strbuf);
// find the required size of the buffer
int u8size = WideCharToMultiByte(CP_UTF8, // CodePage
0, // dwFlags
wideArgv[i], // lpWideCharStr
-1, // cchWideChar
NULL, // lpMultiByteStr
0, // cbMultiByte
NULL, // lpDefaultChar
NULL); // lpUsedDefaultChar
if (u8size > 0) {
char *strbuf = (char *)malloc(u8size);
int ret = WideCharToMultiByte(CP_UTF8, // CodePage
0, // dwFlags
wideArgv[i], // lpWideCharStr
-1, // cchWideChar
strbuf, // lpMultiByteStr
u8size, // cbMultiByte
NULL, // lpDefaultChar
NULL); // lpUsedDefaultChar
if (ret) {
argv[i] = strbuf;
} else {
argv[i] = _strdup("");
free(strbuf);
}
} else {
argv[i] = _strdup("");
}
}
argv[argc] = NULL; // required by C standard at end of list