[PATCH] src/readelf: use qsort instead of qsort_r.

Érico Nogueira ericonr@disroot.org
Wed Dec 16 20:40:16 GMT 2020


From: Érico Rolim <erico.erc@gmail.com>

This program is single threaded, so using qsort with a global variable
isn't a danger. The interface for qsort_r isn't standardized (and
diverges between glibc and FreeBSD, for example), which makes usage of
qsort, where possible, preferrable.
---

FreeBSD's qsort_r can be seen in:

http://man.bsd.lv/FreeBSD-12.0/qsort

 src/ChangeLog |  4 ++++
 src/readelf.c | 14 ++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index a7b227db..9bb90732 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-16  Érico Nogueira  <ericonr@disroot.org>
+
+	* readelf.c (qsort_r): Use qsort for improved portability.
+
 2020-12-16  Dmitry V. Levin  <ldv@altlinux.org>
 
 	* *.c: Replace gettext(...) with _(...).
diff --git a/src/readelf.c b/src/readelf.c
index 824ab31b..f2ebd206 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -4829,10 +4829,13 @@ listptr_base (struct listptr *p)
   return cudie_base (&cu);
 }
 
+/* To store the name used in compare_listptr */
+static const char *sort_listptr_name;
+
 static int
-compare_listptr (const void *a, const void *b, void *arg)
+compare_listptr (const void *a, const void *b)
 {
-  const char *name = arg;
+  const char *name = sort_listptr_name;
   struct listptr *p1 = (void *) a;
   struct listptr *p2 = (void *) b;
 
@@ -4942,8 +4945,11 @@ static void
 sort_listptr (struct listptr_table *table, const char *name)
 {
   if (table->n > 0)
-    qsort_r (table->table, table->n, sizeof table->table[0],
-	     &compare_listptr, (void *) name);
+    {
+      sort_listptr_name = name;
+      qsort (table->table, table->n, sizeof table->table[0],
+	     &compare_listptr);
+    }
 }
 
 static bool
-- 
2.29.2



More information about the Elfutils-devel mailing list