add QSORT_S
authorRené Scharfe <l.s.r@web.de>
Sun, 22 Jan 2017 17:52:13 +0000 (18:52 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Jan 2017 19:02:36 +0000 (11:02 -0800)
Add the macro QSORT_S, a convenient wrapper for qsort_s() that infers
the size of the array elements and dies on error.

Basically all possible errors are programming mistakes (passing NULL as
base of a non-empty array, passing NULL as comparison function,
out-of-bounds accesses), so terminating the program should be acceptable
for most callers.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h
index f706744e6a9e8acdea5241518d7b990fb3121296..f46d40e4a475c9bd35ff02373101e8431e722b7d 100644 (file)
@@ -994,6 +994,11 @@ int git_qsort_s(void *base, size_t nmemb, size_t size,
 #define qsort_s git_qsort_s
 #endif
 
+#define QSORT_S(base, n, compar, ctx) do {                     \
+       if (qsort_s((base), (n), sizeof(*(base)), compar, ctx)) \
+               die("BUG: qsort_s() failed");                   \
+} while (0)
+
 #ifndef REG_STARTEND
 #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
 #endif