bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] localcharset.c: proposed patch to avoid leak upon realloc f


From: Jim Meyering
Subject: [Bug-gnulib] localcharset.c: proposed patch to avoid leak upon realloc failure
Date: Tue, 20 Apr 2004 09:16:43 +0200

Bruno,

When realloc fails in localcharset's get_charset_aliases,
it can clobber the sole pointer to a block of malloc'd memory,
thus leaking that block.

Here's a proposed patch:

2004-04-20  Jim Meyering  <address@hidden>

        * localcharset.c (get_charset_aliases) [!VMS && !WIN32]:
        Don't leak upon realloc failure.

Index: localcharset.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/localcharset.c,v
retrieving revision 1.16
diff -u -p -r1.16 localcharset.c
--- localcharset.c      24 Dec 2003 13:07:08 -0000      1.16
+++ localcharset.c      20 Apr 2004 07:07:31 -0000
@@ -1,6 +1,6 @@
 /* Determine a canonical name for the current locale's character encoding.
 
-   Copyright (C) 2000-2003 Free Software Foundation, Inc.
+   Copyright (C) 2000-2004 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -176,8 +176,12 @@ get_charset_aliases ()
                }
              else
                {
+                 char *p;
                  res_size += l1 + 1 + l2 + 1;
-                 res_ptr = (char *) realloc (res_ptr, res_size + 1);
+                 p = (char *) realloc (res_ptr, res_size + 1);
+                 if (p == NULL)
+                   free (res_ptr);
+                 res_ptr = p;
                }
              if (res_ptr == NULL)
                {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]