summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-09 07:49:04 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-09 07:49:04 +0000
commit8e9a8c043ef5b16a2f13999bd2c5d59d61dd00d1 (patch)
treee50a9121b5d647750a3797c311f62eb4c3401123 /util.c
parent7671126dfb38255d7dbb74d3a5e40520ca94158b (diff)
util.c: qsort_s in C11
* configure.ac: macro for C11 to use qsort_s. * util.c (ruby_qsort): fix for C11 qsort_s. the comparison function for MSVCRT qsort_s is compatible with BSD qsort_r, but not with C11 qsort_s, in spite of its name. note that mingw defines __STDC_VERSION__ but uses qsort_s in MSVCRT, so the MSVCRT block needs to preced the C11 block. [ruby-core:88899] [Bug #15091] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64661 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'util.c')
-rw-r--r--util.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/util.c b/util.c
index 2c1ae86..a713086 100644
--- a/util.c
+++ b/util.c
@@ -195,14 +195,32 @@ ruby_strtoul(const char *str, char **endptr, int base)
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
-#if !defined HAVE_BSD_QSORT_R && defined HAVE_QSORT_S
+typedef int (cmpfunc_t)(const void*, const void*, void*);
+
+#if defined HAVE_QSORT_S
+# if defined __MSVCRT__
# define qsort_r(base, nel, size, arg, cmp) qsort_s(base, nel, size, cmp, arg)
# define cmp_bsd_qsort cmp_ms_qsort
# define HAVE_BSD_QSORT_R 1
+# elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
+/* C11 qsort_s has the same arguments as ours */
+void
+ruby_qsort(void* base, const size_t nel, const size_t size, cmpfunc_t *cmp, void *d)
+{
+ if (!nel || !size) return; /* nothing to sort */
+
+ /* get rid of runtime-constraints handler for MT-safeness */
+ if (!base || !cmp) return;
+ if (nel > RSIZE_MAX || size > RSIZE_MAX) return;
+
+ qsort_s(base, nel, size, cmp, d);
+}
+# else
+# error Unknown version qsort_s
+# endif
#endif
-#if defined HAVE_BSD_QSORT_R
-typedef int (cmpfunc_t)(const void*, const void*, void*);
+#if defined HAVE_BSD_QSORT_R
struct bsd_qsort_r_args {
cmpfunc_t *cmp;
void *arg;
@@ -339,7 +357,6 @@ typedef struct { char *LL, *RR; } stack_node; /* Stack structure for L,l,R,r */
((*cmp)((b),(c),d)<0 ? (b) : ((*cmp)((a),(c),d)<0 ? (c) : (a))) : \
((*cmp)((b),(c),d)>0 ? (b) : ((*cmp)((a),(c),d)<0 ? (a) : (c))))
-typedef int (cmpfunc_t)(const void*, const void*, void*);
void
ruby_qsort(void* base, const size_t nel, const size_t size, cmpfunc_t *cmp, void *d)
{