Linux/MSWindows: improved Fl_Preferences file path checking

This commit is contained in:
Matthias Melcher 2020-01-03 17:42:22 +01:00
parent 8147199a41
commit 462b681679
2 changed files with 46 additions and 17 deletions

View File

@ -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_RESOURCE "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
# define FLPREFS_RESOURCEW L"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[0] = 0;
filename[1] = 0;
size_t appDataLen = strlen(vendor) + strlen(application) + 8; size_t appDataLen = strlen(vendor) + strlen(application) + 8;
DWORD type, nn; DWORD nn;
LONG err; LONG err;
HKEY key; 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 ); err = RegOpenKeyW( HKEY_LOCAL_MACHINE, FLPREFS_RESOURCEW, &key );
if (err == ERROR_SUCCESS) { if (err == ERROR_SUCCESS) {
nn = (DWORD) (FL_PATH_MAX - appDataLen); 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 ); (BYTE*)filename, &nn );
if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { if ( err != ERROR_SUCCESS ) {
filename[0] = 0; filename[0] = 0;
filename[1] = 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 ); err = RegOpenKeyW( HKEY_CURRENT_USER, FLPREFS_RESOURCEW, &key );
if (err == ERROR_SUCCESS) { if (err == ERROR_SUCCESS) {
nn = (DWORD) (FL_PATH_MAX - appDataLen); nn = (DWORD) (FL_PATH_MAX - appDataLen);
err = RegQueryValueExW( key, L"AppData", 0L, &type, err = RegQueryValueExW( key, L"AppData", 0L,0L,
(BYTE*)filename, &nn ); (BYTE*)filename, &nn );
if ( ( err != ERROR_SUCCESS ) && ( type == REG_SZ ) ) { if ( err != ERROR_SUCCESS ) {
filename[0] = 0; filename[0] = 0;
filename[1] = 0; filename[1] = 0;
} }
@ -863,7 +866,9 @@ char *Fl_WinAPI_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Pre
break; break;
} }
if (!filename[1] && !filename[0]) { 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 { } else {
#if 0 #if 0
wchar_t *b = (wchar_t*)_wcsdup((wchar_t *)filename); 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; filename[len] = 0;
free(b); 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), snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename),
"/%s/%s.prefs", vendor, application); "/%s/%s.prefs", vendor, application);
for (char *s = filename; *s; s++) if (*s == '\\') *s = '/'; for (char *s = filename; *s; s++) if (*s == '\\') *s = '/';

View File

@ -24,6 +24,10 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <locale.h> #include <locale.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#if defined(_AIX) #if defined(_AIX)
extern "C" { 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, char *Fl_X11_System_Driver::preference_rootnode(Fl_Preferences *prefs, Fl_Preferences::Root root, const char *vendor,
const char *application) 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; const char *e;
switch (root&Fl_Preferences::ROOT_MASK) { switch (root&Fl_Preferences::ROOT_MASK) {
case Fl_Preferences::USER: case Fl_Preferences::USER:
if ((e = getenv("HOME")) != NULL) { e = getenv("HOME");
strlcpy(filename, e, sizeof(filename)); // make sure that $HOME is set to an existing directory
if ( (e==0L) || (e[0]=0) || (::access(e, F_OK)==-1) ) ) {
if (filename[strlen(filename)-1] != '/') { struct passwd *pw = getpwuid(getuid());
strlcat(filename, "/.fltk/", sizeof(filename)); e = pw->pw_dir;
} else {
strlcat(filename, ".fltk/", sizeof(filename));
}
break;
} }
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: case Fl_Preferences::SYSTEM:
strcpy(filename, "/etc/fltk/"); strcpy(filename, "/etc/fltk/");
break; 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), snprintf(filename + strlen(filename), sizeof(filename) - strlen(filename),
"%s/%s.prefs", vendor, application); "%s/%s.prefs", vendor, application);
return filename; return filename;