From 4951fd1801e94915e462c0fa96765f8ee2187b16 Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Tue, 23 Mar 2021 18:27:30 -0700 Subject: Better fix for issue #206. Albrecht caught a problem with my strncat use (see comments for 12d8da9c). I noticed we have fl_strlcpy() and fl_strlcat() which is what was really needed here. --- src/Fl_Native_File_Chooser_WIN32.cxx | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/Fl_Native_File_Chooser_WIN32.cxx b/src/Fl_Native_File_Chooser_WIN32.cxx index 717f8c013..9a60f4c19 100644 --- a/src/Fl_Native_File_Chooser_WIN32.cxx +++ b/src/Fl_Native_File_Chooser_WIN32.cxx @@ -30,6 +30,7 @@ #define FNFC_MAX_PATH 32768 // XXX: MAX_PATH under win32 is 260, too small for modern use #include // fl_strdup() +#include "flstring.h" // fl_strlcpy()/cat() #include # include # include // OPENFILENAMEW, GetOpenFileName() @@ -522,16 +523,11 @@ int Fl_WinAPI_Native_File_Chooser_Driver::showfile() { // eg. "/dir/name\0foo1\0foo2\0foo3\0\0" // char pathname[FNFC_MAX_PATH]; - for ( const WCHAR *s = dirname + dirlen + 1; - *s; s += (wcslen(s)+1)) { - // ISSUE #206 -- beware strncpy() vs. strncat(): - // > strncpy() doesn't guarantee null termination but strncat() does. - // > strncat() can write to n+1, whereas strncpy() only writes to n. - // fl_snprintf() can't be used here b/c wchartoutf8() returns a static str. - // - strncpy(pathname, wchartoutf8(dirname), FNFC_MAX_PATH); pathname[FNFC_MAX_PATH-1] = 0; - strncat(pathname, "\\", FNFC_MAX_PATH-1); - strncat(pathname, wchartoutf8(s), FNFC_MAX_PATH-1); + for ( const WCHAR *s = dirname + dirlen + 1; *s; s += (wcslen(s)+1)) { + // ISSUE #206: replace strncpy/cat with fl_strlcpy/cat + fl_strlcpy(pathname, wchartoutf8(dirname), FNFC_MAX_PATH); + fl_strlcat(pathname, "\\", FNFC_MAX_PATH); + fl_strlcat(pathname, wchartoutf8(s), FNFC_MAX_PATH); add_pathname(pathname); } } -- cgit v1.2.3