Blame SOURCES/0004-appchooserdialog-improve-safety-of-ensure_default-fu.patch

32b021
From 8f01a166ec2097f913f4e69379954a96a38d0d84 Mon Sep 17 00:00:00 2001
32b021
From: Michael Catanzaro <mcatanzaro@gnome.org>
32b021
Date: Wed, 6 May 2020 10:57:41 -0500
32b021
Subject: [PATCH 4/4] appchooserdialog: improve safety of ensure_default
32b021
 function
32b021
32b021
We can calculate the bounds ourselves, instead of passing them in. This
32b021
way we don't need to rely on the caller to avoid buffer overflow. This
32b021
would have prevented #302.
32b021
32b021
(cherry picked from commit 1f30f6c730cef5152e09ded897ec0d6e54e87820)
32b021
---
32b021
 src/appchooserdialog.c | 20 ++++++++++++--------
32b021
 1 file changed, 12 insertions(+), 8 deletions(-)
32b021
32b021
diff --git a/src/appchooserdialog.c b/src/appchooserdialog.c
32b021
index c0eb4ca..eb7181a 100644
32b021
--- a/src/appchooserdialog.c
32b021
+++ b/src/appchooserdialog.c
32b021
@@ -316,22 +316,26 @@ shorten_location (const char *location)
32b021
 }
32b021
 
32b021
 static void
32b021
-ensure_default_is_below (const char **choices,
32b021
-                         const char  *default_id,
32b021
-                         int          num)
32b021
+ensure_default_in_initial_list (const char **choices,
32b021
+                                const char  *default_id)
32b021
 {
32b021
   int i;
32b021
+  guint n_choices;
32b021
 
32b021
   if (default_id == NULL)
32b021
     return;
32b021
 
32b021
-  for (i = 0; i < num && choices[i]; i++)
32b021
+  n_choices = g_strv_length ((char **)choices);
32b021
+  if (n_choices <= INITIAL_LIST_SIZE)
32b021
+    return;
32b021
+
32b021
+  for (i = 0; i < INITIAL_LIST_SIZE; i++)
32b021
     {
32b021
       if (strcmp (choices[i], default_id) == 0)
32b021
         return;
32b021
     }
32b021
 
32b021
-  for (i = num; choices[i]; i++)
32b021
+  for (i = INITIAL_LIST_SIZE; i < n_choices; i++)
32b021
     {
32b021
       if (strcmp (choices[i], default_id) == 0)
32b021
         {
32b021
@@ -386,11 +390,11 @@ app_chooser_dialog_new (const char **choices,
32b021
       gtk_label_set_label (GTK_LABEL (dialog->heading), _("Choose an application."));
32b021
     }
32b021
 
32b021
-  dialog->choices = g_strdupv ((char **)choices);
32b021
-  n_choices = g_strv_length ((char **)choices);
32b021
+  ensure_default_in_initial_list (choices, default_id);
32b021
 
32b021
-  ensure_default_is_below (dialog->choices, default_id, MIN (n_choices, INITIAL_LIST_SIZE));
32b021
+  dialog->choices = g_strdupv ((char **)choices);
32b021
 
32b021
+  n_choices = g_strv_length ((char **)choices);
32b021
   if (n_choices == 0)
32b021
     {
32b021
       gtk_widget_show (dialog->empty_box);
32b021
-- 
32b021
2.26.2
32b021