Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/telnet avoid memory leak on realloc() failure.



details:   https://anonhg.NetBSD.org/src/rev/ab57b921d35f
branches:  trunk
changeset: 480005:ab57b921d35f
user:      itojun <itojun%NetBSD.org@localhost>
date:      Thu Dec 30 09:52:03 1999 +0000

description:
avoid memory leak on realloc() failure.

diffstat:

 usr.bin/telnet/telnet.c |  23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)

diffs (53 lines):

diff -r 5a254424a165 -r ab57b921d35f usr.bin/telnet/telnet.c
--- a/usr.bin/telnet/telnet.c   Thu Dec 30 09:10:28 1999 +0000
+++ b/usr.bin/telnet/telnet.c   Thu Dec 30 09:52:03 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: telnet.c,v 1.13 1999/07/28 06:35:16 abs Exp $  */
+/*     $NetBSD: telnet.c,v 1.14 1999/12/30 09:52:03 itojun Exp $       */
 
 /*
  * Copyright (c) 1988, 1990, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)telnet.c   8.4 (Berkeley) 5/30/95";
 #else
-__RCSID("$NetBSD: telnet.c,v 1.13 1999/07/28 06:35:16 abs Exp $");
+__RCSID("$NetBSD: telnet.c,v 1.14 1999/12/30 09:52:03 itojun Exp $");
 #endif
 #endif /* not lint */
 
@@ -1530,10 +1530,15 @@
        void
 env_opt_start()
 {
-       if (opt_reply)
-               opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
-       else
-               opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
+       unsigned char *p;
+
+       if (opt_reply) {
+               p = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
+               if (p == NULL)
+                       free(opt_reply);
+       } else
+               p = (unsigned char *)malloc(OPT_REPLY_SIZE);
+       opt_reply = p;
        if (opt_reply == NULL) {
 /*@*/          printf("env_opt_start: malloc()/realloc() failed!!!\n");
                opt_reply = opt_replyp = opt_replyend = NULL;
@@ -1581,9 +1586,13 @@
                                strlen((char *)ep) + 6 > opt_replyend)
        {
                register int len;
+               unsigned char *p;
                opt_replyend += OPT_REPLY_SIZE;
                len = opt_replyend - opt_reply;
-               opt_reply = (unsigned char *)realloc(opt_reply, len);
+               p = (unsigned char *)realloc(opt_reply, len);
+               if (p == NULL)
+                       free(opt_reply);
+               opt_reply = p;
                if (opt_reply == NULL) {
 /*@*/                  printf("env_opt_add: realloc() failed!!!\n");
                        opt_reply = opt_replyp = opt_replyend = NULL;



Home | Main Index | Thread Index | Old Index