[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnulib] localcharset.c: proposed patch to avoid leak upon reall
From: |
Bruno Haible |
Subject: |
Re: [Bug-gnulib] localcharset.c: proposed patch to avoid leak upon realloc failure |
Date: |
Tue, 20 Apr 2004 13:01:37 +0200 |
User-agent: |
KMail/1.5 |
Jim Meyering wrote:
> 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.
Thanks. I'm using this patch, which looks a little bit more symmetrical.
diff -c -3 -r1.16 localcharset.c
*** localcharset.c 24 Dec 2003 13:07:08 -0000 1.16
--- localcharset.c 20 Apr 2004 10:56:26 -0000
***************
*** 1,6 ****
/* Determine a canonical name for the current locale's character encoding.
! Copyright (C) 2000-2003 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
--- 1,6 ----
/* Determine a canonical name for the current locale's character encoding.
! 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
***************
*** 140,154 ****
else
{
/* Parse the file's contents. */
- int c;
- char buf1[50+1];
- char buf2[50+1];
char *res_ptr = NULL;
size_t res_size = 0;
- size_t l1, l2;
for (;;)
{
c = getc (fp);
if (c == EOF)
break;
--- 140,156 ----
else
{
/* Parse the file's contents. */
char *res_ptr = NULL;
size_t res_size = 0;
for (;;)
{
+ int c;
+ char buf1[50+1];
+ char buf2[50+1];
+ size_t l1, l2;
+ char *old_res_ptr;
+
c = getc (fp);
if (c == EOF)
break;
***************
*** 169,174 ****
--- 171,177 ----
break;
l1 = strlen (buf1);
l2 = strlen (buf2);
+ old_res_ptr = res_ptr;
if (res_size == 0)
{
res_size = l1 + 1 + l2 + 1;
***************
*** 183,188 ****
--- 186,193 ----
{
/* Out of memory. */
res_size = 0;
+ if (old_res_ptr != NULL)
+ free (old_res_ptr);
break;
}
strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);