From b87682d493ecd7cd8e3912529e62f7ef10ef9676 Mon Sep 17 00:00:00 2001 From: Jun-ichiro itojun Hagino Date: Thu, 30 Dec 1999 16:58:23 +0000 Subject: avoid memory leak on realloc() failure. --- usr.bin/telnet/telnet.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'usr.bin/telnet/telnet.c') diff --git a/usr.bin/telnet/telnet.c b/usr.bin/telnet/telnet.c index 65ee77f1e64..0a3b80b20d7 100644 --- a/usr.bin/telnet/telnet.c +++ b/usr.bin/telnet/telnet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: telnet.c,v 1.7 1999/07/23 15:04:48 aaron Exp $ */ +/* $OpenBSD: telnet.c,v 1.8 1999/12/30 16:58:22 itojun Exp $ */ /* $NetBSD: telnet.c,v 1.7 1996/02/28 21:04:15 thorpej Exp $ */ /* @@ -1509,10 +1509,15 @@ unsigned char *opt_replyend; 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; @@ -1560,9 +1565,13 @@ env_opt_add(ep) 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; -- cgit v1.2.3