summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthiasWM <visualc.git@matthiasm.com>2023-12-13 20:00:49 +0100
committerMatthiasWM <visualc.git@matthiasm.com>2023-12-13 20:00:49 +0100
commit6ac3e8e2303f95fb7dfb844e7c3192c627156205 (patch)
tree35bc493998bedf01e5964757265c654772ebf260
parent0c712e50bc182bbda5a87704473459ccb2a67c6d (diff)
#840: Fixes fixed buffer size in Fl::args_to_utf8()
-rw-r--r--src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
index d7b01465a..1985d23e0 100644
--- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
+++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
@@ -302,24 +302,40 @@ int Fl_WinAPI_System_Driver::rename(const char *fnam, const char *newnam) {
// See Fl::args_to_utf8()
int Fl_WinAPI_System_Driver::args_to_utf8(int argc, char ** &argv) {
int i;
- char strbuf[2048]; // FIXME: allocate argv and strings dynamically
// Convert the command line arguments to UTF-8
LPWSTR *wideArgv = CommandLineToArgvW(GetCommandLineW(), &argc);
argv = (char **)malloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++) {
- int ret = WideCharToMultiByte(CP_UTF8, // CodePage
+ // find the required size of the buffer
+ int u8size = WideCharToMultiByte(CP_UTF8, // CodePage
0, // dwFlags
wideArgv[i], // lpWideCharStr
-1, // cchWideChar
- strbuf, // lpMultiByteStr
- sizeof(strbuf), // cbMultiByte
+ NULL, // lpMultiByteStr
+ 0, // cbMultiByte
NULL, // lpDefaultChar
NULL); // lpUsedDefaultChar
-
- if (!ret)
- strbuf[0] = '\0'; // return empty string
- argv[i] = fl_strdup(strbuf);
+ 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 NULL pointer at end of list