From 0199434b80bd1602dc0ef61fa206d2c95676028d Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Wed, 16 Nov 2022 15:17:30 +0200 Subject: compare: don't call qsort on NULL array This works in practice but is explicitly forbidden in C99, C11 and C17 alike: > 7.20.5/7.22.5 Searching and sorting utilities > > [...] Where an argument declared as size_t nmemb specifies the length > of the array for a function, nmemb can have the value zero on a call to > that function; the comparison function is not called, [...] and sorting > performs no rearrangement. Pointer arguments on such a call shall still > have valid values, as described in 7.1.4. > 7.1.4 Use of library functions > > [...] If an argument to a function has an invalid value (such as [...] > a null pointer [...]) [...], the behavior is undefined. If a function > argument is described as being an array, the pointer actually passed > to the function shall have a value such that all address computations > and accesses to objects (that would be valid if the pointer did point > to the first element of such an array) are in fact valid. In contrast, qsort_s explicitly allows the array pointer argument to be NULL when nmemb == 0 (see C11/C17 K.3.6.3 Searching and sorting utilities). --- compare/compare.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compare/compare.c b/compare/compare.c index 3cb3cff..4abf512 100644 --- a/compare/compare.c +++ b/compare/compare.c @@ -653,7 +653,8 @@ int main(int argc, char *argv[]) const char *prev = ""; ASS_Track *track = NULL; unsigned total = 0, good = 0; - qsort(list.items, list.n_items, sizeof(Item), item_compare); + if (list.n_items) + qsort(list.items, list.n_items, sizeof(Item), item_compare); for (size_t i = 0; i < list.n_items; i++) { char *name = list.items[i].name; size_t len = list.items[i].prefix; -- cgit v1.2.3