Browse Source

fix aliasing violations in tsearch functions

patch by nsz. the actual object the caller has storing the tree root
has type void *, so accessing it as struct node * is not valid.
instead, simply access the value, move it to a temporary of the
appropriate type and work from there, then move the result back.
rs-1.0
Rich Felker 13 years ago
parent
commit
2d2da648f6
  1. 12
      src/search/tsearch_avl.c

12
src/search/tsearch_avl.c

@ -138,9 +138,13 @@ 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 *))
{ {
struct node *n = *rootp;
struct node *ret;
/* last argument is arbitrary non-null pointer /* last argument is arbitrary non-null pointer
which is returned when the root node is deleted */ which is returned when the root node is deleted */
return remove((void*)rootp, key, compar, *rootp); ret = remove(&n, key, compar, n);
*rootp = n;
return ret;
} }
void *tfind(const void *key, void *const *rootp, void *tfind(const void *key, void *const *rootp,
@ -153,7 +157,11 @@ void *tsearch(const void *key, void **rootp,
int (*compar)(const void *, const void *)) int (*compar)(const void *, const void *))
{ {
int new = 0; int new = 0;
return insert((void*)rootp, key, compar, &new); struct node *n = *rootp;
struct node *ret;
ret = insert(&n, key, compar, &new);
*rootp = n;
return ret;
} }
static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d) static void walk(const struct node *r, void (*action)(const void *, VISIT, int), int d)

Loading…
Cancel
Save