From 1862a900b1419f77f8b965cfbc517e244e471eb9 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 9 Dec 2020 11:41:38 +0000 Subject: [PATCH 1/3] gosxappinfo: Fix some const-correctness issues This is technically an API break, as the following assignment may now raise warnings in user code: ``` gchar *filename = g_osx_app_info_get_filename (app_info); ``` However, from code search it seems like the number of users of that function is zero. Signed-off-by: Philip Withnall --- gio/gosxappinfo.h | 2 +- gio/gosxappinfo.m | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/gio/gosxappinfo.h b/gio/gosxappinfo.h index 7beeaad16..793ce1fcd 100644 --- a/gio/gosxappinfo.h +++ b/gio/gosxappinfo.h @@ -43,7 +43,7 @@ GLIB_AVAILABLE_IN_2_52 GType g_osx_app_info_get_type (void) G_GNUC_CONST; GLIB_AVAILABLE_IN_2_52 -char * g_osx_app_info_get_filename (GOsxAppInfo *info); +const char *g_osx_app_info_get_filename (GOsxAppInfo *info); GLIB_AVAILABLE_IN_2_52 GList * g_osx_app_info_get_all_for_scheme (const gchar *scheme); diff --git a/gio/gosxappinfo.m b/gio/gosxappinfo.m index 849d63575..0a9cd0501 100644 --- a/gio/gosxappinfo.m +++ b/gio/gosxappinfo.m @@ -226,8 +226,8 @@ url_escape_hostname (const char *url) } static CFURLRef -create_url_from_cstr (gchar *cstr, - gboolean is_file) +create_url_from_cstr (const gchar *cstr, + gboolean is_file) { gchar *puny_cstr; CFStringRef str; @@ -280,7 +280,7 @@ create_urlspec_for_appinfo (GOsxAppInfo *info, gboolean are_files) { LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1); - gchar *app_cstr = g_osx_app_info_get_filename (info); + const gchar *app_cstr = g_osx_app_info_get_filename (info); /* Strip file:// from app url but ensure filesystem url */ urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE); @@ -402,7 +402,7 @@ g_osx_app_info_get_executable (GAppInfo *appinfo) return info->executable; } -char * +const char * g_osx_app_info_get_filename (GOsxAppInfo *info) { g_return_val_if_fail (info != NULL, NULL); @@ -431,7 +431,8 @@ g_osx_app_info_get_icon (GAppInfo *appinfo) if (!info->icon) { - gchar *icon_name, *app_uri, *icon_uri; + const gchar *app_uri; + gchar *icon_name, *icon_uri; GFile *file; icon_name = get_bundle_string_value (info->bundle, @"CFBundleIconFile"); From 2a629b3b4b63f03bd7d2d6da0e5d48ed36b20536 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 9 Dec 2020 11:44:14 +0000 Subject: [PATCH 2/3] gosxappinfo: Use strlen() instead of some magic constants This is equivalent, but makes the code a bit more readable. Signed-off-by: Philip Withnall --- gio/gosxappinfo.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gio/gosxappinfo.m b/gio/gosxappinfo.m index 0a9cd0501..bcb1f5648 100644 --- a/gio/gosxappinfo.m +++ b/gio/gosxappinfo.m @@ -283,7 +283,7 @@ create_urlspec_for_appinfo (GOsxAppInfo *info, const gchar *app_cstr = g_osx_app_info_get_filename (info); /* Strip file:// from app url but ensure filesystem url */ - urlspec->appURL = create_url_from_cstr (app_cstr + 7, TRUE); + urlspec->appURL = create_url_from_cstr (app_cstr + strlen ("file://"), TRUE); urlspec->launchFlags = kLSLaunchDefaults; urlspec->itemURLs = create_url_list_from_glist (uris, are_files); @@ -440,7 +440,7 @@ g_osx_app_info_get_icon (GAppInfo *appinfo) return NULL; app_uri = g_osx_app_info_get_filename (info); - icon_uri = g_strconcat (app_uri + 7, "/Contents/Resources/", icon_name, + icon_uri = g_strconcat (app_uri + strlen ("file://"), "/Contents/Resources/", icon_name, g_str_has_suffix (icon_name, ".icns") ? NULL : ".icns", NULL); g_free (icon_name); From a9fc7e59357233771f1b137b6ebec382371b6e54 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Wed, 9 Dec 2020 11:44:37 +0000 Subject: [PATCH 3/3] gosxappinfo: Add some more precondition checks These might help catch the problem in #2119 earlier on, and provide more information about its root cause. They should not affect behaviour in normal application usage. Signed-off-by: Philip Withnall Helps: #2119 --- gio/gosxappinfo.m | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/gio/gosxappinfo.m b/gio/gosxappinfo.m index bcb1f5648..03c373778 100644 --- a/gio/gosxappinfo.m +++ b/gio/gosxappinfo.m @@ -168,6 +168,7 @@ get_bundle_string_value (NSBundle *bundle, static CFStringRef create_cfstring_from_cstr (const gchar *cstr) { + g_return_val_if_fail (cstr != NULL, NULL); return CFStringCreateWithCString (NULL, cstr, kCFStringEncodingUTF8); } @@ -279,8 +280,14 @@ create_urlspec_for_appinfo (GOsxAppInfo *info, GList *uris, gboolean are_files) { - LSLaunchURLSpec *urlspec = g_new0 (LSLaunchURLSpec, 1); - const gchar *app_cstr = g_osx_app_info_get_filename (info); + LSLaunchURLSpec *urlspec = NULL; + const gchar *app_cstr; + + g_return_val_if_fail (G_IS_OSX_APP_INFO (info), NULL); + + urlspec = g_new0 (LSLaunchURLSpec, 1); + app_cstr = g_osx_app_info_get_filename (info); + g_assert (app_cstr != NULL); /* Strip file:// from app url but ensure filesystem url */ urlspec->appURL = create_url_from_cstr (app_cstr + strlen ("file://"), TRUE); @@ -460,9 +467,14 @@ g_osx_app_info_launch_internal (GAppInfo *appinfo, GError **error) { GOsxAppInfo *info = G_OSX_APP_INFO (appinfo); - LSLaunchURLSpec *urlspec = create_urlspec_for_appinfo (info, uris, are_files); + LSLaunchURLSpec *urlspec; gint ret, success = TRUE; + g_return_val_if_fail (G_IS_OSX_APP_INFO (appinfo), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + urlspec = create_urlspec_for_appinfo (info, uris, are_files); + if ((ret = LSOpenFromURLSpec (urlspec, NULL))) { /* TODO: Better error codes */