public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
* [patch] tsearch.3: dispose properly of allocated resources in example code
@ 2008-09-21  6:10 André Goddard Rosa
       [not found] ` <200809210310.43647.andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  0 siblings, 1 reply; 8+ messages in thread
From: André Goddard Rosa @ 2008-09-21  6:10 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

[-- Attachment #1: Type: text/plain, Size: 743 bytes --]

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 02:42:55.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

[-- Attachment #2: tsearch3-free-allocated-resources.patch --]
[-- Type: text/x-patch, Size: 414 bytes --]

--- man3/tsearch.3	2008-09-10 10:16:13.000000000 -0300
+++ man3/tsearch.3.goddard	2008-09-21 02:42:55.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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-09-25  8:14 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-21  6:10 [patch] tsearch.3: dispose properly of allocated resources in example code André Goddard Rosa
     [not found] ` <200809210310.43647.andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2008-09-23  6:28   ` Michael Kerrisk
     [not found]     ` <cfd18e0f0809222328r599e22c1je0f4261ee969bca8-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-09-23 13:23       ` André Goddard Rosa
     [not found]         ` <b8bf37780809230623k62d43d6cgf11af4820566cd2d-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2008-09-23 13:56           ` Michael Kerrisk
2008-09-24 14:01   ` [patch] hsearch.3: Call hdestroy() after using hash table created by hcreate(), for the sake of completeness André Goddard Rosa
     [not found]     ` <200809241101.23128.andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2008-09-25  8:04       ` Michael Kerrisk
2008-09-24 14:01   ` [patch] strcpy.3: Avoid redundantly filling the position 'n - 1' two times in strncpy() example code André Goddard Rosa
     [not found]     ` <200809241101.34334.andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2008-09-25  8:14       ` Michael Kerrisk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox