bug-gnulib
[Top][All Lists]
Advanced

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

[Bug-gnulib] getndelim2.c: don't leak upon failed realloc


From: Jim Meyering
Subject: [Bug-gnulib] getndelim2.c: don't leak upon failed realloc
Date: Tue, 20 Apr 2004 00:24:46 +0200

Here's the patch I'm going to apply for coreutils.
gnulib will get the same exact patch.

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

        * getndelim2.c (getndelim2): Upon realloc failure, don't leak memory.

Index: getndelim2.c
===================================================================
RCS file: /fetish/cu/lib/getndelim2.c,v
retrieving revision 1.4
diff -u -p -r1.4 getndelim2.c
--- a/getndelim2.c      4 Apr 2004 06:51:11 -0000       1.4
+++ b/getndelim2.c      19 Apr 2004 22:18:32 -0000
@@ -1,7 +1,7 @@
 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
    with bounded memory allocation.
 
-   Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
+   Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004 Free Software
    Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
@@ -80,15 +80,17 @@ getndelim2 (char **lineptr, size_t *line
        {
          size_t newlinesize =
            (*linesize > MIN_CHUNK ? 2 * *linesize : *linesize + MIN_CHUNK);
+         char *p;
 
          if (! (*linesize < newlinesize && newlinesize <= nmax))
            newlinesize = nmax;
 
          *linesize = newlinesize;
          nbytes_avail = *linesize + *lineptr - read_pos;
-         *lineptr = realloc (*lineptr, *linesize);
-         if (!*lineptr)
+         p = realloc (*lineptr, *linesize);
+         if (!p)
            return -1;
+         *lineptr = p;
          read_pos = *linesize - nbytes_avail + *lineptr;
        }
 




reply via email to

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