fix tsearch, tfind, tdelete to handle null pointer input

POSIX specifies the behaviour for null rootp input, but it
was not implemented correctly.
This commit is contained in:
Szabolcs Nagy 2015-12-05 21:53:59 +01:00 committed by Rich Felker
parent 8994908b19
commit 3abb094d19
1 changed files with 6 additions and 0 deletions

View File

@ -151,6 +151,8 @@ static struct node *remove(struct node **n, const void *k,
void *tdelete(const void *restrict key, void **restrict rootp, void *tdelete(const void *restrict key, void **restrict rootp,
int(*compar)(const void *, const void *)) int(*compar)(const void *, const void *))
{ {
if (!rootp)
return 0;
struct node *n = *rootp; struct node *n = *rootp;
struct node *ret; struct node *ret;
/* last argument is arbitrary non-null pointer /* last argument is arbitrary non-null pointer
@ -163,6 +165,8 @@ void *tdelete(const void *restrict key, void **restrict rootp,
void *tfind(const void *key, void *const *rootp, void *tfind(const void *key, void *const *rootp,
int(*compar)(const void *, const void *)) int(*compar)(const void *, const void *))
{ {
if (!rootp)
return 0;
return find(*rootp, key, compar); return find(*rootp, key, compar);
} }
@ -171,6 +175,8 @@ void *tsearch(const void *key, void **rootp,
{ {
struct node *update; struct node *update;
struct node *ret; struct node *ret;
if (!rootp)
return 0;
update = insert(*rootp, key, compar, &ret); update = insert(*rootp, key, compar, &ret);
if (update) if (update)
*rootp = update; *rootp = update;