[PATCH v2 2/5] add QSORT_S
- From
René Scharfe <l.s.r@web.de>
- Date
- Jan 22, 2017, 17:52 UTC
- Message-ID
- <57d9277e-9ed1-40bf-43a7-ff23377cdf77@web.de>
- In-Reply-To
- <67ac53cd-3fc0-8bd0-30f4-129281c3090f@web.de>
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> --- git-compat-util.h | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h index f706744e6a..f46d40e4a4 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -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
-- 2.11.0