From 8bd5657a815d1a5a2a6ea4894595ee8ae1d34d55 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Tue, 8 Sep 2020 12:03:21 +0100
Subject: [PATCH] inspect-library: Use glibc argz(3) to list hidden
 dependencies

This makes it easier for scan-build to understand that we are not doing
anything wrong here: parsing the command-line options twice should
result in the same number of items being put in hidden_deps[], but
scan-build can't know that.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 helpers/inspect-library.c | 35 ++++++++---------------------------
 1 file changed, 8 insertions(+), 27 deletions(-)

diff --git a/helpers/inspect-library.c b/helpers/inspect-library.c
index 12912e939..22c8c9e97 100644
--- a/helpers/inspect-library.c
+++ b/helpers/inspect-library.c
@@ -202,15 +202,17 @@ main (int argc,
   ssize_t chars;
   bool first;
   bool deb_symbols = false;
-  size_t dependency_counter = 0;
   int opt;
+  autofree char *hidden_deps = NULL;
+  size_t hidden_deps_len = 0;
+  const char *hidden_dep;
 
   while ((opt = getopt_long (argc, argv, "", long_options, NULL)) != -1)
     {
       switch (opt)
         {
           case OPTION_HIDDEN_DEPENDENCY:
-            dependency_counter++;
+            argz_add_or_die (&hidden_deps, &hidden_deps_len, optarg);
             break;
 
           case OPTION_DEB_SYMBOLS:
@@ -243,40 +245,19 @@ main (int argc,
       usage (1);
     }
 
-  const char *hidden_deps[dependency_counter + 1];
-
-  if (dependency_counter > 0)
-    {
-      /* Reset getopt */
-      optind = 1;
-
-      size_t i = 0;
-      while ((opt = getopt_long (argc, argv, "", long_options, NULL)) != -1)
-        {
-          switch (opt)
-            {
-              case OPTION_HIDDEN_DEPENDENCY:
-                hidden_deps[i] = optarg;
-                i++;
-                break;
-
-              default:
-                break;
-            }
-        }
-    }
-
   soname = argv[optind];
   printf ("{\n");
   printf ("  \"");
   print_json_string_content (soname);
   printf ("\": {");
 
-  for (size_t i = 0; i < dependency_counter; i++)
+  for (hidden_dep = argz_next (hidden_deps, hidden_deps_len, NULL);
+       hidden_dep != NULL;
+       hidden_dep = argz_next (hidden_deps, hidden_deps_len, hidden_dep))
     {
       /* We don't call "dlclose" on global hidden dependencies, otherwise ubsan
        * will report an indirect memory leak */
-      if (dlopen (hidden_deps[i], RTLD_NOW|RTLD_GLOBAL) == NULL)
+      if (dlopen (hidden_dep, RTLD_NOW|RTLD_GLOBAL) == NULL)
         {
           fprintf (stderr, "Unable to find the dependency library: %s\n", dlerror ());
           return 1;
-- 
GitLab