summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOleg Oshmyan <chortos@inbox.lv>2022-11-16 15:17:30 +0200
committerOleg Oshmyan <chortos@inbox.lv>2022-11-16 15:45:18 +0200
commit0199434b80bd1602dc0ef61fa206d2c95676028d (patch)
treeaed10c7ed19d68ff7eaf827359dc5581cb3c4f7d
parentb5cdc695e3049b71985be3a0ce14a4b804ace8f1 (diff)
downloadlibass-coverity_scan.tar.bz2
libass-coverity_scan.tar.xz
compare: don't call qsort on NULL arraycoverity_scan
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).
-rw-r--r--compare/compare.c3
1 files changed, 2 insertions, 1 deletions
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;