[gnome-builder] flatpak: Replace app id regex's matching by GLib's utility function



commit 3ff6c11c43bbfeecfea87def57337ab1526ef468
Author: vanadiae <vanadiae35 gmail com>
Date:   Wed Dec 30 21:12:10 2020 +0100

    flatpak: Replace app id regex's matching by GLib's utility function
    
    Currently when an app id is checked for validity, a regex is used. The
    approach of using a regex for simple (and even complex) matching is
    almost always wrong.
    
    Here there's a very simple function that handles that in GLib, so let's
    use it instead.

 .../flatpak/gbp-flatpak-build-system-discovery.c      | 19 ++++++++++++-------
 src/plugins/flatpak/gbp-flatpak-manifest.c            | 13 +------------
 2 files changed, 13 insertions(+), 19 deletions(-)
---
diff --git a/src/plugins/flatpak/gbp-flatpak-build-system-discovery.c 
b/src/plugins/flatpak/gbp-flatpak-build-system-discovery.c
index 334360559..9179e2a9d 100644
--- a/src/plugins/flatpak/gbp-flatpak-build-system-discovery.c
+++ b/src/plugins/flatpak/gbp-flatpak-build-system-discovery.c
@@ -35,7 +35,12 @@ struct _GbpFlatpakBuildSystemDiscovery
   GObject parent_instance;
 };
 
-static GRegex *filename_regex;
+/* Returns whether @filename seems to be a JSON file, naively detected. */
+static gboolean
+maybe_is_json_file (const char *filename)
+{
+  return strlen (filename) >= strlen (".json") && g_str_has_suffix (filename, ".json");
+}
 
 static void
 gbp_flatpak_build_system_discovery_find_manifests (GFile        *directory,
@@ -68,10 +73,10 @@ gbp_flatpak_build_system_discovery_find_manifests (GFile        *directory,
   while (NULL != (infoptr = g_file_enumerator_next_file (enumerator, cancellable, NULL)))
     {
       g_autoptr(GFileInfo) info = infoptr;
-      g_autoptr(GMatchInfo) match_info = NULL;
       g_autoptr(GFile) file = NULL;
       GFileType file_type;
       const gchar *name;
+      g_autofree gchar *app_id = NULL;
 
       if (g_file_info_get_is_symlink (info))
         continue;
@@ -97,8 +102,11 @@ gbp_flatpak_build_system_discovery_find_manifests (GFile        *directory,
             }
         }
 
-      g_regex_match (filename_regex, name, 0, &match_info);
-      if (!g_match_info_matches (match_info))
+      if (!maybe_is_json_file (name))
+        continue;
+
+      app_id = g_strndup (name, strlen (name) - strlen (".json"));
+      if (!g_application_id_is_valid (app_id))
         continue;
 
       g_ptr_array_add (results, g_steal_pointer (&file));
@@ -226,9 +234,6 @@ G_DEFINE_TYPE_WITH_CODE (GbpFlatpakBuildSystemDiscovery,
 static void
 gbp_flatpak_build_system_discovery_class_init (GbpFlatpakBuildSystemDiscoveryClass *klass)
 {
-  /* This regex is based on https://wiki.gnome.org/HowDoI/ChooseApplicationID */
-  filename_regex = g_regex_new ("^[[:alnum:]-_]+\\.[[:alnum:]-_]+(\\.[[:alnum:]-_]+)*\\.json$",
-                                G_REGEX_OPTIMIZE, 0, NULL);
 }
 
 static void
diff --git a/src/plugins/flatpak/gbp-flatpak-manifest.c b/src/plugins/flatpak/gbp-flatpak-manifest.c
index 282103149..8ab336a6b 100644
--- a/src/plugins/flatpak/gbp-flatpak-manifest.c
+++ b/src/plugins/flatpak/gbp-flatpak-manifest.c
@@ -76,13 +76,6 @@ enum {
 
 static GParamSpec *properties [N_PROPS];
 static guint signals [N_SIGNALS];
-static GRegex *app_id_regex;
-
-static gboolean
-is_valid_app_id (const gchar *str)
-{
-  return g_regex_match (app_id_regex, str, 0, NULL);
-}
 
 static gboolean
 validate_properties (GbpFlatpakManifest  *self,
@@ -397,7 +390,7 @@ gbp_flatpak_manifest_initable_init (GInitable     *initable,
       json_object_has_member (root_obj, "id"))
     app_id_field = "id";
 
-  if (!discover_string_field (root_obj, app_id_field, &app_id) || !is_valid_app_id (app_id))
+  if (!discover_string_field (root_obj, app_id_field, &app_id) || !g_application_id_is_valid (app_id))
     {
       g_set_error (error,
                    G_IO_ERROR,
@@ -693,10 +686,6 @@ gbp_flatpak_manifest_class_init (GbpFlatpakManifestClass *klass)
                   0, NULL, NULL,
                   g_cclosure_marshal_VOID__VOID,
                   G_TYPE_NONE, 0);
-
-  /* This regex is based on https://wiki.gnome.org/HowDoI/ChooseApplicationID */
-  app_id_regex = g_regex_new ("^[[:alnum:]-_]+\\.[[:alnum:]-_]+(\\.[[:alnum:]-_]+)*$",
-                              G_REGEX_OPTIMIZE, 0, NULL);
 }
 
 static void


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