From 462b681679d6c78b695c5394c21b5140b4fb7bd2 Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Fri, 3 Jan 2020 17:42:22 +0100 Subject: [PATCH] Linux/MSWindows: improved Fl_Preferences file path checking --- .../WinAPI/Fl_WinAPI_System_Driver.cxx | 26 +++++++++---- src/drivers/X11/Fl_X11_System_Driver.cxx | 37 ++++++++++++++----- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx index dc7960e62..f28802ba0 100644 --- a/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx +++ b/src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx @@ -827,10 +827,13 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre { # define FLPREFS_RESOURCE "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" # define FLPREFS_RESOURCEW L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders" - static char filename[ FL_PATH_MAX ]; + static char *filename = 0L; + // make enough room for a UTF16 pathname + if (!filename) filename = (char*)::malloc(2*FL_PATH_MAX); filename[0] = 0; + filename[1] = 0; size_t appDataLen = strlen(vendor) + strlen(application) + 8; - DWORD type, nn; + DWORD nn; LONG err; HKEY key; @@ -839,9 +842,9 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre err = RegOpenKeyW( HKEY_LOCAL_MACHINE, FLPREFS_RESOURCEW, &key ); if (err == ERROR_SUCCESS) { nn = (DWORD) (FL_PATH_MAX - appDataLen); - err = RegQueryValueExW( key, L"Common AppData", 0L, &type, + err = RegQueryValueExW( key, L"Common AppData", 0L, 0L, (BYTE*)filename, &nn ); - if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { + if ( err != ERROR_SUCCESS ) { filename[0] = 0; filename[1] = 0; } @@ -852,9 +855,9 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre err = RegOpenKeyW( HKEY_CURRENT_USER, FLPREFS_RESOURCEW, &key ); if (err == ERROR_SUCCESS) { nn = (DWORD) (FL_PATH_MAX - appDataLen); - err = RegQueryValueExW( key, L"AppData", 0L, &type, + err = RegQueryValueExW( key, L"AppData", 0L,0L, (BYTE*)filename, &nn ); - if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { + if ( err != ERROR_SUCCESS ) { filename[0] = 0; filename[1] = 0; } @@ -863,7 +866,9 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre break; } if (!filename[1] && !filename[0]) { - strcpy(filename, "C:\\FLTK"); + // Don't write data into some arbitrary directory! Just return NULL. + //strcpy(filename, "C:\\FLTK"); + return 0L; } else { #if 0 wchar_t *b = (wchar_t*)_wcsdup((wchar_t *)filename); @@ -879,6 +884,13 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre filename[len] = 0; free(b); } + + // Make sure that the parameters are not NULL + if ( (vendor==0L) || (vendor[0]==0) ) + vendor = "unknown"; + if ( (application==0L) || (application[0]==0) ) + application = "unknown"; + snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), "/%s/%s.prefs", vendor, application); for (char *s = filename; *s; s++) if (*s == '\\') *s = '/'; diff --git a/src/drivers/X11/Fl_X11_System_Driver.cxx b/src/drivers/X11/Fl_X11_System_Driver.cxx index b84aba8ea..11e56de23 100644 --- a/src/drivers/X11/Fl_X11_System_Driver.cxx +++ b/src/drivers/X11/Fl_X11_System_Driver.cxx @@ -24,6 +24,10 @@ #include #include #include +#include +#include +#include + #if defined(_AIX) extern "C" { @@ -397,24 +401,37 @@ void Fl_X11_System_Driver::newUUID(char *uuidBuffer) char *Fl_X11_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor, const char *application) { - static char filename[ FL_PATH_MAX ]; filename[0] = 0; + static char *filename = 0L; + if (!filename) filename = (char*)::calloc(1, FL_PATH_MAX); const char *e; switch (root&Fl_Preferences::ROOT_MASK) { case Fl_Preferences::USER: - if ((e = getenv("HOME")) != NULL) { - strlcpy(filename, e, sizeof(filename)); - - if (filename[strlen(filename)-1] != '/') { - strlcat(filename, "/.fltk/", sizeof(filename)); - } else { - strlcat(filename, ".fltk/", sizeof(filename)); - } - break; + e = getenv("HOME"); + // make sure that $HOME is set to an existing directory + if ( (e==0L) || (e[0]=0) || (::access(e, F_OK)==-1) ) ) { + struct passwd *pw = getpwuid(getuid()); + e = pw->pw_dir; } + if ( (e==0L) || (e[0]=0) || (::access(e, F_OK)==-1) ) ) { + return 0L; + } else { + strlcpy(filename, e, sizeof(filename)); + if (filename[strlen(filename)-1] != '/') + strlcat(filename, "/", sizeof(filename)); + strlcat(filename, ".fltk/", sizeof(filename)); + } + break; case Fl_Preferences::SYSTEM: strcpy(filename, "/etc/fltk/"); break; } + + // Make sure that the parameters are not NULL + if ( (vendor==0L) || (vendor[0]==0) ) + vendor = "unknown"; + if ( (application==0L) || (application[0]==0) ) + application = "unknown"; + snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename), "%s/%s.prefs", vendor, application); return filename;