git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[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
Previous: René ScharfeNext: René Scharfe
Message 6 of 18 in “string-list: make string_list_sort() reentrant”
  1. 0/5 string-list: make string_list_sort() reentrantRené Scharfe, Jan 22, 2017
  2. 1/5 compat: add qsort_s()René Scharfe, Jan 22, 2017
  3. 3/5 perf: add basic sort performance testRené Scharfe, Jan 22, 2017
  4. 4/5 string-list: use QSORT_S in string_list_sort()René Scharfe, Jan 22, 2017
  5. 5/5 ref-filter: use QSORT_S in ref_array_sort()René Scharfe, Jan 22, 2017
  6. 2/5 add QSORT_SRené Scharfe, Jan 22, 2017
  7. [DEMO][PATCH v2 6/5] compat: add a qsort_s() implementation based on GNU's qsort_r(1)René Scharfe, Jan 22, 2017
  8. Junio C HamanoJan 23, 2017
  9. René ScharfeJan 24, 2017
  10. Jeff KingJan 24, 2017
  11. René ScharfeJan 25, 2017
  12. Jeff KingJan 25, 2017
  13. Johannes SchindelinJan 26, 2017
  14. Jeff KingJan 23, 2017
  15. Johannes SchindelinJan 24, 2017
  16. Jeff KingJan 24, 2017
  17. René ScharfeJan 24, 2017
  18. Jeff KingJan 24, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.