Make mk_ancestry () / mk_ancestry_iter () work without fixed-size buffers
authorLuca Bacci <luca.bacci982@gmail.com>
Thu, 29 Dec 2022 17:02:19 +0000 (18:02 +0100)
committerØyvind "pippin" Kolås <pippin@gimp.org>
Tue, 3 Jan 2023 10:33:37 +0000 (10:33 +0000)
babl/babl-cache.c

index 211bea5fdeb91ca02528999464225dcc43241adc..9e5ff969a3a2825f485f83c9da553c06ea93c807 100644 (file)
 static int
 mk_ancestry_iter (const char *path)
 {
-  char copy[4096];
-  strncpy (copy, path, 4096);
-  copy[sizeof (copy) - 1] = '\0';
-  if (strrchr (copy, '/'))
+  char *copy = babl_strdup (path);
+  char *rchr = NULL;
+  int result = 0;
+
+  if (!copy)
+    return -1;
+
+  rchr = strrchr (copy, '/');
+  if (rchr)
     {
-      *strrchr (copy, '/') = '\0';
+      *rchr = '\0';
+
       if (copy[0])
         {
           BablStat stat_buf;
           if ( ! (_babl_stat (copy, &stat_buf)==0 && S_ISDIR(stat_buf.st_mode)))
             {
               if (mk_ancestry_iter (copy) != 0)
-                return -1;
+                {
+                  result = -1;
+                }
+              else
+                {
 #ifndef _WIN32
-              return _babl_mkdir (copy, S_IRWXU);
+                  result = _babl_mkdir (copy, S_IRWXU);
 #else
-              return _babl_mkdir (copy);
+                  result = _babl_mkdir (copy);
 #endif
+                }
             }
         }
     }
-  return 0;
+
+  babl_free (copy);
+  return result;
 }
 
 static int
 mk_ancestry (const char *path)
 {
-  char copy[4096];
-  strncpy (copy, path, 4096);
-  copy[sizeof (copy) - 1] = '\0';
+  char *copy = babl_strdup (path);
+  int result = 0;
+
+  if (!copy)
+    return -1;
+
 #ifdef _WIN32
   for (char *c = copy; *c; c++)
     if (*c == '\\')
       *c = '/';
 #endif
-  return mk_ancestry_iter (copy);
+
+  result = mk_ancestry_iter (copy);
+
+  babl_free (copy);
+  return result;
 }
 
 static char *