Guarantee that g_get_tmp_dir () doesn't return an empty string
authorMatthias Clasen <mclasen@redhat.com>
Thu, 26 Aug 2010 00:04:45 +0000 (20:04 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 26 Aug 2010 00:04:45 +0000 (20:04 -0400)
If it does, g_file_open_tmp() would be in trouble. Pointed
out by Morten Welinder in bug 627969.

glib/gutils.c
glib/tests/utils.c

index 406b4b1..7dc889d 100644 (file)
@@ -1525,17 +1525,17 @@ g_get_any_init_do (void)
   gchar hostname[100];
 
   g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
-  if (!g_tmp_dir)
+  if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
     g_tmp_dir = g_strdup (g_getenv ("TMP"));
-  if (!g_tmp_dir)
+  if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
     g_tmp_dir = g_strdup (g_getenv ("TEMP"));
 
 #ifdef G_OS_WIN32
-  if (!g_tmp_dir)
+  if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
     g_tmp_dir = get_windows_directory_root ();
 #else  
 #ifdef P_tmpdir
-  if (!g_tmp_dir)
+  if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
     {
       gsize k;    
       g_tmp_dir = g_strdup (P_tmpdir);
@@ -1545,7 +1545,7 @@ g_get_any_init_do (void)
     }
 #endif
   
-  if (!g_tmp_dir)
+  if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
     {
       g_tmp_dir = g_strdup ("/tmp");
     }
@@ -1868,7 +1868,7 @@ g_get_home_dir (void)
  * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none 
  * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows. 
  * The encoding of the returned string is system-defined. On Windows, 
- * it is always UTF-8. The return value is never %NULL.
+ * it is always UTF-8. The return value is never %NULL or the empty string.
  *
  * Returns: the directory to use for temporary files.
  */
index 104a51b..6b8da90 100644 (file)
@@ -123,17 +123,31 @@ test_appname (void)
   g_assert_cmpstr (appname, ==, "appname");
 }
 
+static void
+test_tmpdir (void)
+{
+  g_test_bug ("627969");
+  g_assert_cmpstr (g_get_tmp_dir (), !=, "");
+}
+
 int
 main (int   argc,
       char *argv[])
 {
   argv0 = argv[0];
 
+  /* for tmpdir test, need to do this early before g_get_any_init */
+  g_setenv ("TMPDIR", "", TRUE);
+  g_unsetenv ("TMP");
+  g_unsetenv ("TEMP");
+
   g_test_init (&argc, &argv, NULL);
+  g_test_bug_base ("http://bugzilla.gnome.org/");
 
   g_test_add_func ("/utils/language-names", test_language_names);
   g_test_add_func ("/utils/version", test_version);
   g_test_add_func ("/utils/appname", test_appname);
+  g_test_add_func ("/utils/tmpdir", test_tmpdir);
 
   return g_test_run();
 }