[gimp/gimp-2-10] app: do not use PATH_MAX and realpath() on macOS.



commit 566be5e986a51731203cbacd44252c973481fcb5
Author: Jehan <jehan girinstud io>
Date:   Mon Mar 22 22:28:05 2021 +0100

    app: do not use PATH_MAX and realpath() on macOS.
    
    See the comments in MR!424.
    Basically realpath relies on false assumptions (probably ones which used
    to be true when the API got created) on the max size of a path. Actually
    nowadays paths can be much bigger than what the macro advertizes or can
    even be unbounded.
    The Linux version of realpath() allows the second parameter to be NULL,
    in which case it would allocate the buffer, exactly for this reason
    (written in the BUGS section on the man). Unfortunately this behavior is
    not standardized in POSIX and the man from Apple I found does not
    indicate it will do this.
    
    So let's use g_canonicalize_filename() instead, which seems to do the
    right thing. Similarly use g_strdup_printf instead of g_snprintf().
    
    Cherry-picked from commit 6f4fac7715eb66a90727f931246c93cd8e13a819 with
    some conflict resolution.

 app/main.c | 68 ++++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 24 deletions(-)
---
diff --git a/app/main.c b/app/main.c
index 0d4e257b07..ba38758766 100644
--- a/app/main.c
+++ b/app/main.c
@@ -39,7 +39,6 @@
 #endif /* G_OS_WIN32 */
 
 #if defined(ENABLE_RELOCATABLE_RESOURCES) && defined(__APPLE__)
-#include <sys/param.h> /* PATH_MAX */
 #include <libgen.h> /* dirname */
 #include <sys/stat.h>
 #endif /* __APPLE__ */
@@ -312,13 +311,15 @@ gimp_macos_setenv (const char * progname)
    * Due to the latest changes it is not recommended to set it in the shell
    * wrapper anymore.
    */
-  gchar resolved_path[PATH_MAX];
+  gchar  *resolved_path;
   /* on some OSX installations open file limit is 256 and GIMP needs more */
-  struct rlimit limit;
+  struct  rlimit limit;
+
   limit.rlim_cur = 10000;
   limit.rlim_max = 10000;
   setrlimit (RLIMIT_NOFILE, &limit);
-  if (realpath (progname, resolved_path) && !g_getenv ("GIMP_NO_WRAPPER"))
+  resolved_path = g_canonicalize_filename (progname, NULL);
+  if (resolved_path && ! g_getenv ("GIMP_NO_WRAPPER"))
     {
       /* set path to the app folder to make sure that our python is called
        * instead of system one
@@ -326,18 +327,25 @@ gimp_macos_setenv (const char * progname)
       static gboolean            show_playground   = TRUE;
 
       gchar *path;
-      gchar  tmp[PATH_MAX];
+      gchar *tmp;
       gchar *app_dir;
-      gchar  res_dir[PATH_MAX];
+      gchar *res_dir;
       size_t path_len;
       struct stat sb;
 
       app_dir = g_path_get_dirname (resolved_path);
-      g_snprintf (tmp, sizeof(tmp), "%s/../Resources", app_dir);
-      if (realpath (tmp, res_dir) && !stat (res_dir,&sb) && S_ISDIR (sb.st_mode))
-        g_print ("GIMP is started as MacOS application\n");
+      tmp = g_strdup_printf ("%s/../Resources", app_dir);
+      res_dir = g_canonicalize_filename (tmp, NULL);
+      g_free (tmp);
+      if (res_dir && !stat (res_dir, &sb) && S_ISDIR (sb.st_mode))
+        {
+          g_print ("GIMP is started as MacOS application\n");
+        }
       else
-        return;
+        {
+          g_free (res_dir);
+          return;
+        }
 
       path_len = strlen (g_getenv ("PATH") ? g_getenv ("PATH") : "") + strlen (app_dir) + 2;
       path = g_try_malloc (path_len);
@@ -353,34 +361,46 @@ gimp_macos_setenv (const char * progname)
       g_free (app_dir);
       g_setenv ("PATH", path, TRUE);
       g_free (path);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gtk-2.0/2.10.0", res_dir);
+      tmp = g_strdup_printf ("%s/lib/gtk-2.0/2.10.0", res_dir);
       g_setenv ("GTK_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/etc/gtk-2.0/gtk.immodules", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/etc/gtk-2.0/gtk.immodules", res_dir);
       g_setenv ("GTK_IM_MODULE_FILE", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gegl-0.4", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/lib/gegl-0.4", res_dir);
       g_setenv ("GEGL_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/babl-0.1", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/lib/babl-0.1", res_dir);
       g_setenv ("BABL_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache", res_dir);
       g_setenv ("GDK_PIXBUF_MODULE_FILE", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/etc/fonts", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/etc/fonts", res_dir);
       g_setenv ("FONTCONFIG_PATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s", res_dir);
       g_setenv ("PYTHONHOME", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/python2.7:%s/lib/gimp/2.0/python", res_dir, res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/lib/python2.7:%s/lib/gimp/2.0/python", res_dir, res_dir);
       g_setenv ("PYTHONPATH", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/lib/gio/modules", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/lib/gio/modules", res_dir);
       g_setenv ("GIO_MODULE_DIR", tmp, TRUE);
-      g_snprintf (tmp, sizeof(tmp), "%s/share/libwmf/fonts", res_dir);
+      g_free (tmp);
+      tmp = g_strdup_printf ("%s/share/libwmf/fonts", res_dir);
       g_setenv ("WMF_FONTDIR", tmp, TRUE);
-      if (g_getenv ("HOME")!=NULL)
+      g_free (tmp);
+      if (g_getenv ("HOME") != NULL)
         {
-          g_snprintf (tmp, sizeof(tmp),
-                      "%s/Library/Application Support/GIMP/2.10/cache",
-                      g_getenv("HOME"));
+          tmp = g_strdup_printf ("%s/Library/Application Support/GIMP/2.10/cache",
+                                 g_getenv ("HOME"));
           g_setenv ("XDG_CACHE_HOME", tmp, TRUE);
+          g_free (tmp);
         }
+      g_free (res_dir);
     }
+  g_free (resolved_path);
 }
 #endif
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]