From fdf578d936abacafe2bfb8d08255a5093b1f1ea3 Mon Sep 17 00:00:00 2001 From: MatthiasWM Date: Fri, 15 Dec 2023 12:56:06 +0100 Subject: [PATCH] #840: Fixes fixed buffer size in Fl::args_to_utf8() (fl_call_main) --- src/fl_call_main.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/fl_call_main.c b/src/fl_call_main.c index de05b98d4..88049eec7 100644 --- a/src/fl_call_main.c +++ b/src/fl_call_main.c @@ -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