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_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 = '/';

View File

@ -24,6 +24,10 @@
#include <X11/Xlib.h>
#include <locale.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#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;