[glib: 3/8] tools: Fix handling of empty argv in various minor GLib tools




commit 3ea4ba31a14a9c0790a5a71cdda1527930c250a6
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Jan 31 14:47:57 2022 +0000

    tools: Fix handling of empty argv in various minor GLib tools
    
    This won’t really affect anything, but we might as well fix them to not
    crash if called with an empty `argv` by someone (ab)using `execve()`.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 gio/gdbus-tool.c              | 3 ++-
 gio/gio-querymodules.c        | 2 +-
 glib/deprecated/gcompletion.c | 3 ++-
 glib/win_iconv.c              | 3 ++-
 gobject/gobject-query.c       | 2 +-
 5 files changed, 8 insertions(+), 5 deletions(-)
---
diff --git a/gio/gdbus-tool.c b/gio/gdbus-tool.c
index 42991886a..476056b9f 100644
--- a/gio/gdbus-tool.c
+++ b/gio/gdbus-tool.c
@@ -107,7 +107,7 @@ usage (gint *argc, gchar **argv[], gboolean use_stdout)
   g_option_context_set_help_enabled (o, FALSE);
   /* Ignore parsing result */
   g_option_context_parse (o, argc, argv, NULL);
-  program_name = g_path_get_basename ((*argv)[0]);
+  program_name = (*argc > 0) ? g_path_get_basename ((*argv)[0]) : g_strdup ("gdbus-tool");
   s = g_strdup_printf (_("Commands:\n"
                          "  help         Shows this information\n"
                          "  introspect   Introspect a remote object\n"
@@ -141,6 +141,7 @@ modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command)
    *  2. save old argv[0] and restore later
    */
 
+  g_assert (*argc > 1);
   g_assert (g_strcmp0 ((*argv)[1], command) == 0);
   remove_arg (1, argc, argv);
 
diff --git a/gio/gio-querymodules.c b/gio/gio-querymodules.c
index cbeb9758e..aff194b73 100644
--- a/gio/gio-querymodules.c
+++ b/gio/gio-querymodules.c
@@ -161,7 +161,7 @@ main (gint   argc,
 {
   int i;
 
-  if (argc == 1)
+  if (argc <= 1)
     {
       g_print ("Usage: gio-querymodules <directory1> [<directory2> ...]\n");
       g_print ("Will update giomodule.cache in the listed directories\n");
diff --git a/glib/deprecated/gcompletion.c b/glib/deprecated/gcompletion.c
index d09c1f1fa..5f0979bb0 100644
--- a/glib/deprecated/gcompletion.c
+++ b/glib/deprecated/gcompletion.c
@@ -463,7 +463,8 @@ main (int   argc,
   
   if (argc < 3)
     {
-      g_warning ("Usage: %s filename prefix1 [prefix2 ...]", argv[0]);
+      g_warning ("Usage: %s filename prefix1 [prefix2 ...]",
+                 (argc > 0) ? argv[0] : "gcompletion");
       return 1;
     }
   
diff --git a/glib/win_iconv.c b/glib/win_iconv.c
index cc2ce53b6..7c78e78fc 100644
--- a/glib/win_iconv.c
+++ b/glib/win_iconv.c
@@ -2034,7 +2034,8 @@ main(int argc, char **argv)
 
     if (fromcode == NULL || tocode == NULL)
     {
-        printf("usage: %s [-c] -f from-enc -t to-enc [file]\n", argv[0]);
+        printf("usage: %s [-c] -f from-enc -t to-enc [file]\n",
+               (argc > 0) ? argv[0] : "win_iconv");
         return 0;
     }
 
diff --git a/gobject/gobject-query.c b/gobject/gobject-query.c
index fd45fba82..5c3c7dc42 100644
--- a/gobject/gobject-query.c
+++ b/gobject/gobject-query.c
@@ -196,7 +196,7 @@ main (gint   argc,
     }
   
   if (!gen_froots && !gen_tree)
-    return help (argv[i-1]);
+    return help ((argc > 0) ? argv[i-1] : NULL);
   
   if (!indent_inc)
     {


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