Hi,

On Thu, Mar 10, 2011 at 19:24, Jim Meyering <[email protected]> wrote:
> I noticed/fixed a minor leak:
>
> From 55d16c63f13898351cbd8c2d2309295c0d9507a8 Mon Sep 17 00:00:00 2001
> From: Jim Meyering <[email protected]>
> Date: Thu, 10 Mar 2011 19:23:28 +0100
> Subject: [PATCH] avoid a minor leak
>
> * src/util.c (parse_c_string): Avoid a leak when realloc-to-smaller
> returns a different pointer.
> ---
>  ChangeLog  |    6 ++++++
>  src/util.c |    6 +++++-
>  2 files changed, 11 insertions(+), 1 deletions(-)
>
> diff --git a/ChangeLog b/ChangeLog
> index 77fb4d0..0fe6aa6 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,9 @@
> +2011-03-10  Jim Meyering  <[email protected]>
> +
> +       avoid a minor leak
> +       * src/util.c (parse_c_string): Avoid a leak when realloc-to-smaller
> +       returns a different pointer.

That would hit me by surprise. Quoting POSIX.1-2008 for realloc(3):

If the new size of the memory object would require movement of the
object, the space for the previous instantiation of the object is
freed.

Bert

> +
>  2011-02-20  Jim Meyering  <[email protected]>
>
>        maint: remove unnecessary tests before free
> diff --git a/src/util.c b/src/util.c
> index f1187ff..42c4cb3 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -1342,7 +1342,11 @@ parse_c_string (char const *s, char const **endp)
>            *v++ = 0;
>            v = realloc (u, v - u);
>            if (v)
> -             u = v;
> +             {
> +               if (u != v)
> +                 free (u);
> +               u = v;
> +             }
>            if (endp)
>              *endp = s;
>            return u;
> --
> 1.7.4.1.299.ga459d
>
>

Reply via email to