[Bf-blender-cvs] [a0633e2484f] blender-v3.0-release: Fix crash when "HOME" environment variable isn't defined

Campbell Barton noreply at git.blender.org
Mon Nov 1 03:13:11 CET 2021


Commit: a0633e2484f21c9f80842c10b46b3af98b54d25a
Author: Campbell Barton
Date:   Mon Nov 1 13:09:02 2021 +1100
Branches: blender-v3.0-release
https://developer.blender.org/rBa0633e2484f21c9f80842c10b46b3af98b54d25a

Fix crash when "HOME" environment variable isn't defined

Accessing the default directory in the file selector
would crash if HOME was undefined.

Add BKE_appdir_folder_default_or_root which never returns NULL.

===================================================================

M	source/blender/blenkernel/BKE_appdir.h
M	source/blender/blenkernel/intern/appdir.c
M	source/blender/blenlib/BLI_winstuff.h
M	source/blender/blenlib/intern/winstuff.c
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/fsmenu.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_appdir.h b/source/blender/blenkernel/BKE_appdir.h
index 07132201e87..65485058dd7 100644
--- a/source/blender/blenkernel/BKE_appdir.h
+++ b/source/blender/blenkernel/BKE_appdir.h
@@ -21,6 +21,8 @@
 
 #include <stddef.h>
 
+#include "BLI_compiler_attrs.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -32,7 +34,9 @@ void BKE_appdir_exit(void);
 
 /* note on naming: typical _get() suffix is omitted here,
  * since its the main purpose of the API. */
-const char *BKE_appdir_folder_default(void);
+const char *BKE_appdir_folder_default(void) ATTR_WARN_UNUSED_RESULT;
+const char *BKE_appdir_folder_root(void) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
+const char *BKE_appdir_folder_default_or_root(void) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL;
 const char *BKE_appdir_folder_home(void);
 bool BKE_appdir_folder_documents(char *dir);
 bool BKE_appdir_folder_caches(char *r_path, size_t path_len);
diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
index ce4ab8a4ba1..08a3b7d0bbb 100644
--- a/source/blender/blenkernel/intern/appdir.c
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -170,6 +170,26 @@ const char *BKE_appdir_folder_default(void)
 #endif /* WIN32 */
 }
 
+const char *BKE_appdir_folder_root(void)
+{
+#ifndef WIN32
+  return "/";
+#else
+  static char root[4];
+  BLI_windows_get_default_root_dir(root);
+  return root;
+#endif
+}
+
+const char *BKE_appdir_folder_default_or_root(void)
+{
+  const char *path = BKE_appdir_folder_default();
+  if (path == NULL) {
+    path = BKE_appdir_folder_root();
+  }
+  return path;
+}
+
 /**
  * Get the user's home directory, i.e.
  * - Unix: `$HOME`
diff --git a/source/blender/blenlib/BLI_winstuff.h b/source/blender/blenlib/BLI_winstuff.h
index bf09b56c779..cbf1716602a 100644
--- a/source/blender/blenlib/BLI_winstuff.h
+++ b/source/blender/blenlib/BLI_winstuff.h
@@ -106,7 +106,7 @@ const char *dirname(char *path);
 
 /* Windows utility functions. */
 bool BLI_windows_register_blend_extension(const bool background);
-void BLI_windows_get_default_root_dir(char *root_dir);
+void BLI_windows_get_default_root_dir(char root_dir[4]);
 int BLI_windows_get_executable_dir(char *str);
 
 #ifdef __cplusplus
diff --git a/source/blender/blenlib/intern/winstuff.c b/source/blender/blenlib/intern/winstuff.c
index 3001b25bc1e..11345fc7242 100644
--- a/source/blender/blenlib/intern/winstuff.c
+++ b/source/blender/blenlib/intern/winstuff.c
@@ -193,7 +193,7 @@ bool BLI_windows_register_blend_extension(const bool background)
   return true;
 }
 
-void BLI_windows_get_default_root_dir(char *root)
+void BLI_windows_get_default_root_dir(char root[4])
 {
   char str[MAX_PATH + 1];
 
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index f647e1d4e4f..a83e1974baf 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -2463,12 +2463,14 @@ static void file_expand_directory(bContext *C)
     if (BLI_path_is_rel(params->dir)) {
       /* Use of 'default' folder here is just to avoid an error message on '//' prefix. */
       BLI_path_abs(params->dir,
-                   G.relbase_valid ? BKE_main_blendfile_path(bmain) : BKE_appdir_folder_default());
+                   G.relbase_valid ? BKE_main_blendfile_path(bmain) :
+                                     BKE_appdir_folder_default_or_root());
     }
     else if (params->dir[0] == '~') {
       char tmpstr[sizeof(params->dir) - 1];
       BLI_strncpy(tmpstr, params->dir + 1, sizeof(tmpstr));
-      BLI_join_dirfile(params->dir, sizeof(params->dir), BKE_appdir_folder_default(), tmpstr);
+      BLI_path_join(
+          params->dir, sizeof(params->dir), BKE_appdir_folder_default_or_root(), tmpstr, NULL);
     }
 
     else if (params->dir[0] == '\0')
diff --git a/source/blender/editors/space_file/fsmenu.c b/source/blender/editors/space_file/fsmenu.c
index 091c2d5f434..97f22ca7d89 100644
--- a/source/blender/editors/space_file/fsmenu.c
+++ b/source/blender/editors/space_file/fsmenu.c
@@ -769,21 +769,22 @@ void fsmenu_read_system(struct FSMenu *fsmenu, int read_bookmarks)
                         FS_INSERT_LAST);
 
     const char *home = BLI_getenv("HOME");
-
+    if (home) {
 #  define FS_MACOS_PATH(path, name, icon) \
     BLI_snprintf(line, sizeof(line), path, home); \
     fsmenu_insert_entry(fsmenu, FS_CATEGORY_OTHER, line, name, icon, FS_INSERT_LAST);
 
-    FS_MACOS_PATH("%s/", NULL, ICON_HOME)
-    FS_MACOS_PATH("%s/Desktop/", N_("Desktop"), ICON_DESKTOP)
-    FS_MACOS_PATH("%s/Documents/", N_("Documents"), ICON_DOCUMENTS)
-    FS_MACOS_PATH("%s/Downloads/", N_("Downloads"), ICON_IMPORT)
-    FS_MACOS_PATH("%s/Movies/", N_("Movies"), ICON_FILE_MOVIE)
-    FS_MACOS_PATH("%s/Music/", N_("Music"), ICON_FILE_SOUND)
-    FS_MACOS_PATH("%s/Pictures/", N_("Pictures"), ICON_FILE_IMAGE)
-    FS_MACOS_PATH("%s/Library/Fonts/", N_("Fonts"), ICON_FILE_FONT)
+      FS_MACOS_PATH("%s/", NULL, ICON_HOME)
+      FS_MACOS_PATH("%s/Desktop/", N_("Desktop"), ICON_DESKTOP)
+      FS_MACOS_PATH("%s/Documents/", N_("Documents"), ICON_DOCUMENTS)
+      FS_MACOS_PATH("%s/Downloads/", N_("Downloads"), ICON_IMPORT)
+      FS_MACOS_PATH("%s/Movies/", N_("Movies"), ICON_FILE_MOVIE)
+      FS_MACOS_PATH("%s/Music/", N_("Music"), ICON_FILE_SOUND)
+      FS_MACOS_PATH("%s/Pictures/", N_("Pictures"), ICON_FILE_IMAGE)
+      FS_MACOS_PATH("%s/Library/Fonts/", N_("Fonts"), ICON_FILE_FONT)
 
 #  undef FS_MACOS_PATH
+    }
 
     /* Get mounted volumes better method OSX 10.6 and higher, see:
      * https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFURLRef/Reference/reference.html



More information about the Bf-blender-cvs mailing list