Commit 71acd8b9 by Alexander Monakov Committed by Alexander Monakov

qsort_chk: call from gcc_qsort instead of wrapping it

	* sort.cc (gcc_qsort) [CHECKING_P]: Call qsort_chk.
	* system.h (qsort): Always redirect to gcc_qsort.  Update comment.
	* vec.c (qsort_chk): Do not call gcc_qsort.  Update comment.

From-SVN: r264065
parent 158985b1
2018-09-03 Alexander Monakov <amonakov@ispras.ru>
* sort.cc (gcc_qsort) [CHECKING_P]: Call qsort_chk.
* system.h (qsort): Always redirect to gcc_qsort. Update comment.
* vec.c (qsort_chk): Do not call gcc_qsort. Update comment.
2018-09-03 Segher Boessenkool <segher@kernel.crashing.org> 2018-09-03 Segher Boessenkool <segher@kernel.crashing.org>
* config/rs6000/rs6000.md (*mov<mode>_hardfloat32): Remove %U from the * config/rs6000/rs6000.md (*mov<mode>_hardfloat32): Remove %U from the
......
...@@ -229,4 +229,7 @@ gcc_qsort (void *vbase, size_t n, size_t size, cmp_fn *cmp) ...@@ -229,4 +229,7 @@ gcc_qsort (void *vbase, size_t n, size_t size, cmp_fn *cmp)
mergesort (base, &c, n, base, (char *)buf); mergesort (base, &c, n, base, (char *)buf);
if (buf != scratch) if (buf != scratch)
free (buf); free (buf);
#if CHECKING_P
qsort_chk (vbase, n, size, cmp);
#endif
} }
...@@ -1197,17 +1197,13 @@ helper_const_non_const_cast (const char *p) ...@@ -1197,17 +1197,13 @@ helper_const_non_const_cast (const char *p)
/* Get definitions of HOST_WIDE_INT. */ /* Get definitions of HOST_WIDE_INT. */
#include "hwint.h" #include "hwint.h"
/* qsort comparator consistency checking: except in release-checking compilers, /* GCC qsort API-compatible functions: except in release-checking compilers,
redirect 4-argument qsort calls to qsort_chk; keep 1-argument invocations redirect 4-argument qsort calls to gcc_qsort; keep 1-argument invocations
corresponding to vec::qsort (cmp): they use C qsort internally anyway. */ corresponding to vec::qsort (cmp): they use C qsort internally anyway. */
void qsort_chk (void *, size_t, size_t, int (*)(const void *, const void *)); void qsort_chk (void *, size_t, size_t, int (*)(const void *, const void *));
void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *)); void gcc_qsort (void *, size_t, size_t, int (*)(const void *, const void *));
#define PP_5th(a1, a2, a3, a4, a5, ...) a5 #define PP_5th(a1, a2, a3, a4, a5, ...) a5
#undef qsort #undef qsort
#if CHECKING_P
#define qsort(...) PP_5th (__VA_ARGS__, qsort_chk, 3, 2, qsort, 0) (__VA_ARGS__)
#else
#define qsort(...) PP_5th (__VA_ARGS__, gcc_qsort, 3, 2, qsort, 0) (__VA_ARGS__) #define qsort(...) PP_5th (__VA_ARGS__, gcc_qsort, 3, 2, qsort, 0) (__VA_ARGS__)
#endif
#endif /* ! GCC_SYSTEM_H */ #endif /* ! GCC_SYSTEM_H */
...@@ -201,21 +201,12 @@ qsort_chk_error (const void *p1, const void *p2, const void *p3, ...@@ -201,21 +201,12 @@ qsort_chk_error (const void *p1, const void *p2, const void *p3,
internal_error ("qsort checking failed"); internal_error ("qsort checking failed");
} }
/* Wrapper around qsort with checking that CMP is consistent on given input. /* Verify anti-symmetry and transitivity for comparator CMP on sorted array
of N SIZE-sized elements pointed to by BASE. */
Strictly speaking, passing invalid (non-transitive, non-anti-commutative)
comparators to libc qsort can result in undefined behavior. Therefore we
should ideally perform consistency checks prior to invoking qsort, but in
order to do that optimally we'd need to sort the array ourselves beforehand
with a sorting routine known to be "safe". Instead, we expect that most
implementations in practice will still produce some permutation of input
array even for invalid comparators, which enables us to perform checks on
the output array. */
void void
qsort_chk (void *base, size_t n, size_t size, qsort_chk (void *base, size_t n, size_t size,
int (*cmp)(const void *, const void *)) int (*cmp)(const void *, const void *))
{ {
gcc_qsort (base, n, size, cmp);
#if 0 #if 0
#define LIM(n) (n) #define LIM(n) (n)
#else #else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment