Skip to content

Commit

Permalink
Do not try to sort an empty array
Browse files Browse the repository at this point in the history
While at it, turn ARRAY_SORT into an expression that return whether the
array was non-empty.

Apparently, glibc's qsort has a __nonnull attribute on qsort's first
argument, which makes `qsort(NULL, 0, ...)` invalid. This is in my
opinion a false positive.

Fixes neomutt#3774
  • Loading branch information
gahr authored and flatcap committed Mar 23, 2023
1 parent a957af3 commit 611ec32
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mutt/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
* @param fn Sort function, see ::sort_t
*/
#define ARRAY_SORT(head, fn) \
qsort((head)->entries, ARRAY_SIZE(head), ARRAY_ELEM_SIZE(head), (fn))
((head)->entries && (qsort((head)->entries, ARRAY_SIZE(head), ARRAY_ELEM_SIZE(head), (fn)), true))

/******************************************************************************
* Internal APIs
Expand Down
13 changes: 12 additions & 1 deletion test/array/mutt_array_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,18 @@ void test_mutt_array_api(void)

/* Sorting */
{
ARRAY_SORT(&d, gt);
struct Dummies empty = ARRAY_HEAD_INITIALIZER;
if (!TEST_CHECK(!ARRAY_SORT(&empty, gt)))
{
TEST_MSG("Expected: false");
TEST_MSG("Actual : true");
}

if (!TEST_CHECK(ARRAY_SORT(&d, gt)))
{
TEST_MSG("Expected: true");
TEST_MSG("Actual : false");
}
struct Dummy *elem = NULL;
ARRAY_FOREACH_FROM(elem, &d, 1)
{
Expand Down

0 comments on commit 611ec32

Please sign in to comment.