C standard function 'tdelete()' has a memory bug since its inception
cee.studio
cee.studio
The quote in TFA is incomplete. The design of tdelete is quite bad, but I would argue that it is not a memory bug. A bug would occur when the caller of tdelete ignores the spec and dereferences the dangling pointer.
See also POSIX spec here: https://pubs.opengroup.org/onlinepubs/9699919799/functions/t...
> The tdelete() function shall return a pointer to the parent of the deleted node, or an unspecified non-null pointer if the deleted node was the root node, or a null pointer if the node is not found.
Oh and here is Ubuntu man pages, seeing that they are online and all:
https://manpages.ubuntu.com/manpages/focal/en/man3/tdelete.3...
https://manpages.ubuntu.com/manpages/focal/en/man3/tdelete.3...
void *tsearch(const void *key, void **rootp,
int (*compar)(const void *, const void *));
void *tfind(const void *key, void *const *rootp,
int (*compar)(const void *, const void *));
...
rootp points to a variable which points to the root of the tree.
You can't operate on a tree unless you have a pointer to it, i.e., it's root."The tdelete() function deletes a node from the specified binary search tree and returns a pointer to the parent of the node that was deleted. It takes the same arguments as tfind() and tsearch(). If the node to be deleted is the root of the binary search tree, rootp will be adjusted."
It seems reasonable to infer that if a tree has only 1 node that node is the root node.
If I'm wrong I'd appreciate an explanation; I'm only a hobbyist programmer and my understanding of these things is limited.