[vlc-devel] [PATCH] compat: work-around busted FreeBSD qsort_r()

Marvin Scholz epirat07 at gmail.com
Fri Jan 4 00:13:04 CET 2019


Co-authored-by: Rémi Denis-Courmont <remi at remlab.net>
---

This patch is based on a previous patch by Rémi, see:
https://mailman.videolan.org/pipermail/vlc-devel/2018-November/121891.html

I am not sure what the best way is to indicate that this patch has
multiple authors, so for now I just went with the "Co-authored-by"
approach. If there is a better way, or you prefer a different way,
please let me know.

---
 compat/qsort_r.c     |  2 +-
 configure.ac         | 20 +++++++++++++++++++-
 include/vlc_fixups.h |  6 ++++--
 src/playlist/sort.c  |  4 ++--
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/compat/qsort_r.c b/compat/qsort_r.c
index 1f68b68ba0..818e02d0eb 100644
--- a/compat/qsort_r.c
+++ b/compat/qsort_r.c
@@ -38,7 +38,7 @@ static int compar_wrapper(const void *a, const void *b)
 /* Follow the upcoming POSIX prototype, coming from GNU/libc.
  * Note that this differs from the BSD prototype. */
 
-void qsort_r(void *base, size_t nmemb, size_t size,
+void vlc_qsort_r(void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *, void *),
              void *arg)
 {
diff --git a/configure.ac b/configure.ac
index d3df6e254e..dc1000c0df 100644
--- a/configure.ac
+++ b/configure.ac
@@ -581,7 +581,7 @@ need_libc=false
 
 dnl Check for usual libc functions
 AC_CHECK_FUNCS([accept4 daemon fcntl flock fstatvfs fork getenv getpwuid_r isatty memalign mkostemp mmap open_memstream newlocale openat pipe2 pread posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp strptime uselocale])
-AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll qsort_r recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp pathconf])
+AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp pathconf])
 AC_REPLACE_FUNCS([gettimeofday])
 AC_CHECK_FUNC(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
@@ -742,6 +742,24 @@ AC_CHECK_LIB([m], [sincos], [
   AC_LIBOBJ([sincos])
 ])
 
+AC_CHECK_FUNC([qsort_r], [
+    AC_MSG_CHECKING([broken qsort_r prototype])
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <stdlib.h>
+        _Static_assert(_Generic((qsort_r),
+            void (*)(void *, size_t, size_t, void *,
+                int (*)(void *, const void *, const void *)) : 1, default: 0),
+            "Bad prototype not matched");
+    ])], [
+        AC_MSG_RESULT([broken])
+        AC_LIBOBJ([qsort_r])
+    ], [
+        AC_MSG_RESULT([no])
+        AC_DEFINE([HAVE_POSIX_QSORT_R], 1, [Defined to 1 if the qsort_r() prototype matches the upcoming POSIX standard.])
+    ])
+  ], [
+    AC_LIBOBJ([qsort_r])
+])
+
 AC_MSG_CHECKING([_Thread_local support])
 AC_COMPILE_IFELSE([AC_LANG_SOURCE([_Thread_local int foo = 0;
                    int main() {}])], [
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 0263286efc..94fd0546e3 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -224,9 +224,11 @@ long long atoll (const char *);
 lldiv_t lldiv (long long, long long);
 #endif
 
-#ifndef HAVE_QSORT_R
-void (qsort_r)(void *, size_t, size_t,
+#ifndef HAVE_POSIX_QSORT_R
+void (vlc_qsort_r)(void *, size_t, size_t,
                int (*)(const void *, const void *, void *), void *);
+#else
+# define vlc_qsort_r qsort_r
 #endif
 
 #ifndef HAVE_STRTOF
diff --git a/src/playlist/sort.c b/src/playlist/sort.c
index b44f81a168..1864321efe 100644
--- a/src/playlist/sort.c
+++ b/src/playlist/sort.c
@@ -289,7 +289,7 @@ CompareMetaByKey(const struct vlc_playlist_item_meta *a,
      }
 }
 
-/* context for qsort_r() */
+/* context for vlc_qsort_r() */
 struct sort_request
 {
     const struct vlc_playlist_sort_criterion *criteria;
@@ -377,7 +377,7 @@ vlc_playlist_Sort(vlc_playlist_t *playlist,
 
     struct sort_request req = { criteria, count };
 
-    qsort_r(array, playlist->items.size, sizeof(*array), compare_meta, &req);
+    vlc_qsort_r(array, playlist->items.size, sizeof(*array), compare_meta, &req);
 
     /* apply the sorting result to the playlist */
     for (size_t i = 0; i < playlist->items.size; ++i)
-- 
2.17.1 (Apple Git-112)



More information about the vlc-devel mailing list