Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/rwhod avoid memory leak on realloc() error.



details:   https://anonhg.NetBSD.org/src/rev/5a254424a165
branches:  trunk
changeset: 480004:5a254424a165
user:      itojun <itojun%NetBSD.org@localhost>
date:      Thu Dec 30 09:10:28 1999 +0000

description:
avoid memory leak on realloc() error.

diffstat:

 usr.sbin/rwhod/rwhod.c |  12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diffs (41 lines):

diff -r 93ba015a573c -r 5a254424a165 usr.sbin/rwhod/rwhod.c
--- a/usr.sbin/rwhod/rwhod.c    Thu Dec 30 04:18:51 1999 +0000
+++ b/usr.sbin/rwhod/rwhod.c    Thu Dec 30 09:10:28 1999 +0000
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)rwhod.c    8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: rwhod.c,v 1.16 1999/11/15 15:59:24 mjl Exp $");
+__RCSID("$NetBSD: rwhod.c,v 1.17 1999/12/30 09:10:28 itojun Exp $");
 #endif
 #endif /* not lint */
 
@@ -283,6 +283,7 @@
        struct stat stb;
        double avenrun[3];
        time_t now;
+       struct utmp *nutmp;
        int cc;
 
        now = time(NULL);
@@ -295,14 +296,17 @@
                if (stb.st_size > utmpsize) {
                        utmpsize = stb.st_size + 10 * sizeof(struct utmp);
                        if (utmp)
-                               utmp = (struct utmp *)realloc(utmp, utmpsize);
+                               nutmp = (struct utmp *)realloc(utmp, utmpsize);
                        else
-                               utmp = (struct utmp *)malloc(utmpsize);
-                       if (! utmp) {
+                               nutmp = (struct utmp *)malloc(utmpsize);
+                       if (! nutmp) {
                                warn("malloc failed");
+                               if (utmp)
+                                       free(utmp);
                                utmpsize = 0;
                                goto done;
                        }
+                       utmp = nutmp;
                }
                (void)lseek(utmpf, (off_t)0, SEEK_SET);
                cc = read(utmpf, (char *)utmp, stb.st_size);



Home | Main Index | Thread Index | Old Index