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; 5+ 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] 5+ messages in thread

* [patch] tsearch.3: dispose properly of allocated resources in example code
@ 2008-09-21  6:15 André Goddard Rosa
  0 siblings, 0 replies; 5+ messages in thread
From: André Goddard Rosa @ 2008-09-21  6:15 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w
  Cc: linux-man-u79uwXL29TY76Z2rM5mHXA,
	andre.goddard-Re5JQEeQqe8AvxtiuMwx3w

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

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

* Re: [patch] tsearch.3: dispose properly of allocated resources in example code
       [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>
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Kerrisk @ 2008-09-23  6:28 UTC (permalink / raw)
  To: André Goddard Rosa; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

André,

On Sun, Sep 21, 2008 at 8:10 AM, André Goddard Rosa
<andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> 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.

Thanks.  My first patch applied via 'git am'!

I applied this patch for man-pages-3.10.  (I added
#define _GNU_SOURCE
since that's required to expose the tdestroy() declaration.)

Cheers,

Michael

> --- 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
>
>



-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
man-pages online: http://www.kernel.org/doc/man-pages/online_pages.html
Found a bug? http://www.kernel.org/doc/man-pages/reporting_bugs.html
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [patch] tsearch.3: dispose properly of allocated resources in example code
       [not found]     ` <cfd18e0f0809222328r599e22c1je0f4261ee969bca8-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2008-09-23 13:23       ` André Goddard Rosa
       [not found]         ` <b8bf37780809230623k62d43d6cgf11af4820566cd2d-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: André Goddard Rosa @ 2008-09-23 13:23 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

On Tue, Sep 23, 2008 at 3:28 AM, Michael Kerrisk
<mtk.manpages-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
> André,
>
> On Sun, Sep 21, 2008 at 8:10 AM, André Goddard Rosa
> <andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> 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.
>
> Thanks.  My first patch applied via 'git am'!
>
> I applied this patch for man-pages-3.10.  (I added
> #define _GNU_SOURCE
> since that's required to expose the tdestroy() declaration.)
>

Hi, Michael!

    Very nice, thank you! Good to know that you're using git.

    I appreciate your work in the man pages, they were improved a lot
since you've started working on them. Also, I appreciate your concerns
regarding paccept().

    Notice that I sent two different patches in a sequence (sorry for that).

This is the first one:

> +        else if (!((*(int **) val) == ptr))

and this is the second one:

+        else if ((*(int **) val) != ptr)

    They work the same, but second is cleaner and simpler ;-)

Kind regards,
-- 
[]s,
André Goddard
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [patch] tsearch.3: dispose properly of allocated resources in example code
       [not found]         ` <b8bf37780809230623k62d43d6cgf11af4820566cd2d-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2008-09-23 13:56           ` Michael Kerrisk
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Kerrisk @ 2008-09-23 13:56 UTC (permalink / raw)
  To: André Goddard Rosa; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

André,

On Tue, Sep 23, 2008 at 3:23 PM, André Goddard Rosa
<andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> On Tue, Sep 23, 2008 at 3:28 AM, Michael Kerrisk
> <mtk.manpages-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:
>> André,
>>
>> On Sun, Sep 21, 2008 at 8:10 AM, André Goddard Rosa
>> <andre.goddard-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>>> 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.
>>
>> Thanks.  My first patch applied via 'git am'!
>>
>> I applied this patch for man-pages-3.10.  (I added
>> #define _GNU_SOURCE
>> since that's required to expose the tdestroy() declaration.)
>>
>
> Hi, Michael!
>
>    Very nice, thank you! Good to know that you're using git.

(At last... '-).)

>    I appreciate your work in the man pages, they were improved a lot
> since you've started working on them.

Thanks!

> Also, I appreciate your concerns
> regarding paccept().
>
>    Notice that I sent two different patches in a sequence (sorry for that).

It's a good thing you mentioned this, since I did _not_ notice that:
you sent two mails at almost the same, with the same description, and
which looked otherwise identical (unless one looked very close), so I
assumed you had accidentally sent the same mail twice.  (Best to
change the description a little so I know you are resending a
different patch.)

> This is the first one:
>
>> +        else if (!((*(int **) val) == ptr))
>
> and this is the second one:
>
> +        else if ((*(int **) val) != ptr)
>
>    They work the same, but second is cleaner and simpler ;-)

Okay -- I've fixed the page to use the second version.  And you only
just got that one in in time for 3.10!

Thanks,

Michael
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2008-09-23 13:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-21  6:15 [patch] tsearch.3: dispose properly of allocated resources in example code André Goddard Rosa
  -- strict thread matches above, loose matches on Subject: below --
2008-09-21  6:10 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

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