[glib: 1/3] gmodule: Improve error handling for invalid .la files




commit 9b02e58e88f3c5eaff9723cf351075d558957319
Author: Philip Withnall <pwithnall endlessos org>
Date:   Tue Jul 5 11:16:59 2022 +0100

    gmodule: Improve error handling for invalid .la files
    
    If a `.la` file is empty, `lt_libdir` and/or `lt_dlname` won’t be set,
    but will then still be used in `g_strconcat()`, leading to invalid
    output.
    
    Detect that and return an error.
    
    Adds a unit test.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Coverity CID: #1474756

 gmodule/gmodule.c           | 14 ++++++++++++--
 gmodule/tests/module-test.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 2 deletions(-)
---
diff --git a/gmodule/gmodule.c b/gmodule/gmodule.c
index c722c44275..9f5c53f217 100644
--- a/gmodule/gmodule.c
+++ b/gmodule/gmodule.c
@@ -427,12 +427,22 @@ parse_libtool_archive (const gchar* libtool_name)
       g_free (dir);
     }
 
+  g_clear_pointer (&scanner, g_scanner_destroy);
+  close (g_steal_fd (&fd));
+
+  if (lt_libdir == NULL || lt_dlname == NULL)
+    {
+      gchar *display_libtool_name = g_filename_display_name (libtool_name);
+      g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", 
display_libtool_name));
+      g_free (display_libtool_name);
+
+      return NULL;
+    }
+
   name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
   
   g_free (lt_dlname);
   g_free (lt_libdir);
-  g_scanner_destroy (scanner);
-  close (fd);
 
   return name;
 }
diff --git a/gmodule/tests/module-test.c b/gmodule/tests/module-test.c
index e6cd69ac18..1f82d1ceff 100644
--- a/gmodule/tests/module-test.c
+++ b/gmodule/tests/module-test.c
@@ -23,6 +23,7 @@
  */
 
 #include <gmodule.h>
+#include <glib/gstdio.h>
 
 #ifdef _MSC_VER
 # define MODULE_FILENAME_PREFIX ""
@@ -203,12 +204,40 @@ test_module_basics (void)
   g_module_close (module_self);
 }
 
+static void
+test_module_invalid_libtool_archive (void)
+{
+  int la_fd;
+  gchar *la_filename = NULL;
+  GModule *module = NULL;
+  GError *local_error = NULL;
+
+  g_test_summary ("Test that opening an invalid .la file fails");
+
+  /* Create an empty temporary file ending in `.la` */
+  la_fd = g_file_open_tmp ("gmodule-invalid-XXXXXX.la", &la_filename, &local_error);
+  g_assert_no_error (local_error);
+  g_assert_true (g_str_has_suffix (la_filename, ".la"));
+  g_close (la_fd, NULL);
+
+  /* Try loading it */
+  module = g_module_open_full (la_filename, 0, &local_error);
+  g_assert_error (local_error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED);
+  g_assert_null (module);
+  g_clear_error (&local_error);
+
+  (void) g_unlink (la_filename);
+
+  g_free (la_filename);
+}
+
 int
 main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/module/basics", test_module_basics);
+  g_test_add_func ("/module/invalid-libtool-archive", test_module_invalid_libtool_archive);
 
   return g_test_run ();
 }


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