[Bf-blender-cvs] [ef3c49de818] master: Cleanup: early return when directories can't be found

Campbell Barton noreply at git.blender.org
Sat Sep 10 06:19:11 CEST 2022


Commit: ef3c49de8186278d57d0319ab4bf4fe923b7016d
Author: Campbell Barton
Date:   Sat Sep 10 14:09:42 2022 +1000
Branches: master
https://developer.blender.org/rBef3c49de8186278d57d0319ab4bf4fe923b7016d

Cleanup: early return when directories can't be found

Also reduce variable scope and assert when an invalid argument
is passed to BKE_appdir_folder_id_create.

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

M	source/blender/blenkernel/intern/appdir.c
M	source/blender/blenkernel/intern/studiolight.c
M	source/blender/windowmanager/intern/wm_files.c
M	source/blender/windowmanager/intern/wm_platform_support.c

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

diff --git a/source/blender/blenkernel/intern/appdir.c b/source/blender/blenkernel/intern/appdir.c
index f4dec0aecd7..24e4305d916 100644
--- a/source/blender/blenkernel/intern/appdir.c
+++ b/source/blender/blenkernel/intern/appdir.c
@@ -734,6 +734,7 @@ const char *BKE_appdir_folder_id_create(const int folder_id, const char *subfold
             BLENDER_USER_CONFIG,
             BLENDER_USER_SCRIPTS,
             BLENDER_USER_AUTOSAVE)) {
+    BLI_assert_unreachable();
     return NULL;
   }
 
diff --git a/source/blender/blenkernel/intern/studiolight.c b/source/blender/blenkernel/intern/studiolight.c
index f17450ac3f4..64f998ea67f 100644
--- a/source/blender/blenkernel/intern/studiolight.c
+++ b/source/blender/blenkernel/intern/studiolight.c
@@ -1166,19 +1166,21 @@ static void studiolight_add_files_from_datafolder(const int folder_id,
                                                   const char *subfolder,
                                                   int flag)
 {
-  struct direntry *dirs;
   const char *folder = BKE_appdir_folder_id(folder_id, subfolder);
-  if (folder) {
-    const uint dirs_num = BLI_filelist_dir_contents(folder, &dirs);
-    int i;
-    for (i = 0; i < dirs_num; i++) {
-      if (dirs[i].type & S_IFREG) {
-        studiolight_add_file(dirs[i].path, flag);
-      }
+  if (!folder) {
+    return;
+  }
+
+  struct direntry *dirs;
+  const uint dirs_num = BLI_filelist_dir_contents(folder, &dirs);
+  int i;
+  for (i = 0; i < dirs_num; i++) {
+    if (dirs[i].type & S_IFREG) {
+      studiolight_add_file(dirs[i].path, flag);
     }
-    BLI_filelist_free(dirs, dirs_num);
-    dirs = NULL;
   }
+  BLI_filelist_free(dirs, dirs_num);
+  dirs = NULL;
 }
 
 static int studiolight_flag_cmp_order(const StudioLight *sl)
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 186edfe68d6..6d2248ba354 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -1397,29 +1397,27 @@ void wm_homefile_read_post(struct bContext *C,
 
 void wm_history_file_read(void)
 {
-  char name[FILE_MAX];
-  LinkNode *l, *lines;
-  struct RecentFile *recent;
-  const char *line;
-  int num;
   const char *const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
-
   if (!cfgdir) {
     return;
   }
 
+  char name[FILE_MAX];
+  LinkNode *l;
+  int num;
+
   BLI_join_dirfile(name, sizeof(name), cfgdir, BLENDER_HISTORY_FILE);
 
-  lines = BLI_file_read_as_lines(name);
+  LinkNode *lines = BLI_file_read_as_lines(name);
 
   wm_history_files_free();
 
   /* read list of recent opened files from recent-files.txt to memory */
   for (l = lines, num = 0; l && (num < U.recent_files); l = l->next) {
-    line = l->link;
+    const char *line = l->link;
     /* don't check if files exist, causes slow startup for remote/external drives */
     if (line[0]) {
-      recent = (RecentFile *)MEM_mallocN(sizeof(RecentFile), "RecentFile");
+      struct RecentFile *recent = (RecentFile *)MEM_mallocN(sizeof(RecentFile), "RecentFile");
       BLI_addtail(&(G.recent_files), recent);
       recent->filepath = BLI_strdup(line);
       num++;
diff --git a/source/blender/windowmanager/intern/wm_platform_support.c b/source/blender/windowmanager/intern/wm_platform_support.c
index becc2d896d0..a0519506d29 100644
--- a/source/blender/windowmanager/intern/wm_platform_support.c
+++ b/source/blender/windowmanager/intern/wm_platform_support.c
@@ -32,35 +32,35 @@
  */
 static bool wm_platform_support_check_approval(const char *platform_support_key, bool update)
 {
-  const char *const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
-  bool result = false;
-
   if (G.factory_startup) {
-    return result;
+    return false;
+  }
+  const char *const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
+  if (!cfgdir) {
+    return false;
   }
 
-  if (cfgdir) {
-    char filepath[FILE_MAX];
-    BLI_join_dirfile(filepath, sizeof(filepath), cfgdir, BLENDER_PLATFORM_SUPPORT_FILE);
-    LinkNode *lines = BLI_file_read_as_lines(filepath);
-    for (LinkNode *line_node = lines; line_node; line_node = line_node->next) {
-      char *line = line_node->link;
-      if (STREQ(line, platform_support_key)) {
-        result = true;
-        break;
-      }
+  bool result = false;
+  char filepath[FILE_MAX];
+  BLI_join_dirfile(filepath, sizeof(filepath), cfgdir, BLENDER_PLATFORM_SUPPORT_FILE);
+  LinkNode *lines = BLI_file_read_as_lines(filepath);
+  for (LinkNode *line_node = lines; line_node; line_node = line_node->next) {
+    char *line = line_node->link;
+    if (STREQ(line, platform_support_key)) {
+      result = true;
+      break;
     }
+  }
 
-    if (!result && update) {
-      FILE *fp = BLI_fopen(filepath, "a");
-      if (fp) {
-        fprintf(fp, "%s\n", platform_support_key);
-        fclose(fp);
-      }
+  if (!result && update) {
+    FILE *fp = BLI_fopen(filepath, "a");
+    if (fp) {
+      fprintf(fp, "%s\n", platform_support_key);
+      fclose(fp);
     }
-
-    BLI_file_free_lines(lines);
   }
+
+  BLI_file_free_lines(lines);
   return result;
 }



More information about the Bf-blender-cvs mailing list