[patch] tsearch.3: dispose properly of allocated resources in example code

André Goddard Rosa
2008-09-21T06:15:21Z

As there are many users who base their own code on these examples, we should avoid leaking memory when using functions of tsearch() family.
In the provided example, tsearch() leaks memory whenever it tries to insert a number which was already inserted before.

Please CC me, as I´m not subscribed.

Applies to man-pages-3.09.

--- man3/tsearch.3 2008-09-10 10:16:13.000000000 -0300
+++ man3/tsearch.3.goddard 2008-09-21 03:12:43.000000000 -0300
@@ -246,8 +246,11 @@
val = tsearch((void *) ptr, &root, compare);
if (val == NULL)
exit(EXIT_FAILURE);
+ else if ((*(int **) val) != ptr)
+ free(ptr);
}
twalk(root, action);
+ tdestroy(root, free);
exit(EXIT_SUCCESS);
}
.fi
\0