GNOME Bugzilla – Bug 150007
file chooser crashes if g_get_home_dir() returns NULL
Last modified: 2011-02-04 16:16:53 UTC
Windows system is Win98. Currently using Tor Lillqvist's GTK packages, compiling with MinGW/msys, using gcc. When the program is called from the DOS window (the "DOS PROMPT", the program will segfault. I get four lines of errors relating to the path, and the filename, stating that the test "..filename != NULL.." failed. If this same program is called under msys, it seems to operate correctly. The same source compiled and run under Linux apparently works well. This program will crash on my system: --------------- cut ---------------------- #include <gtk/gtk.h> int main(int argc, char * argv[]) { GtkWidget *window; GtkWidget *dialog; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); dialog = gtk_file_chooser_dialog_new("File Chooser dialog", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),""); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } ------------------------- cut --------------------------
I've located the problem. In my DOS window, I didn't have the HOME variable set. When gtk_file_chooser failed to find this variable, it left the pointer NULL, which, of course, caused a segfault. This is not, in fact, a win32-specific bug. I tried it under msys after unsetting HOME and got the same crash. Didn't try it under Linux, but am convinced that the same would occur. It seems that in a case like this, although I suppose the omission of a HOME variable is a user error, the function should notify the user of the need via a Message box or the like. Perhaps a default could be assigned if HOME is undefined?
Do you have IE 5+ installed? gtk should work without it (I think the current policy is to support back to win95), but it would explain why no home dir is returned. The issue is the code to set g_home_dir in g_get_any_init() in gutils.c, which probably sets g_home_dir to NULL if none of the environment variables ar set. Should g_home_dir be set to c:\ if nothing else can be found? This would affect mainly older (95, 98, NT4) systems. We could also add direct api calls to get the info in case the environment is missing some of the values.
Regardless whether g_get_home_dir() always should return something on Windows, the fact is that it can return NULL, even on Unix. (And probably will stay that way on Unix, better to return NULL that start guessing. Single-user Windows machines are another thing, there you probably won't cause any security risks etc if you pretend %SYSTEMDRIVE%:\ is HOME.) gtkfilechooserdefault.c, for instance, doesn't take this into account. Will attach patch shortly. See bug #105887 for improving g_get_home_dir() on Windows, to return NULL less often.
Created attachment 30534 [details] [review] Suggested patch
Patch applied to gtk-2-4 and HEAD.