Ensured calls to bsearch() have a length of at least 1; UndefinedBehaviorSanitizer will warn otherwise. Also included qsort(), to be safe.

This commit is contained in:
Pietro Gagliardi 2019-06-02 14:26:25 -04:00
parent 6d6cd66046
commit 12c01d0d42
1 changed files with 4 additions and 1 deletions

View File

@ -77,11 +77,14 @@ void sharedbitsPrefixName(ArrayDeleteItem)(sharedbitsPrefixName(Array) *arr, voi
void *sharedbitsPrefixName(ArrayBsearch)(const sharedbitsPrefixName(Array) *arr, const void *key, int (*compare)(const void *, const void *)) void *sharedbitsPrefixName(ArrayBsearch)(const sharedbitsPrefixName(Array) *arr, const void *key, int (*compare)(const void *, const void *))
{ {
if (arr->len == 0)
return NULL;
return bsearch(key, arr->buf, arr->len, arr->elemsize, compare); return bsearch(key, arr->buf, arr->len, arr->elemsize, compare);
} }
void sharedbitsPrefixName(ArrayQsort)(sharedbitsPrefixName(Array) *arr, int (*compare)(const void *, const void *)) void sharedbitsPrefixName(ArrayQsort)(sharedbitsPrefixName(Array) *arr, int (*compare)(const void *, const void *))
{ {
if (arr->len != 0)
qsort(arr->buf, arr->len, arr->elemsize, compare); qsort(arr->buf, arr->len, arr->elemsize, compare);
} }