realloc properly without leaking memory

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@7691 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
1999-12-30 23:43:50 +00:00
parent e95a92be9e
commit f3351b4b8e

View File

@@ -1414,8 +1414,14 @@ unsigned char *opt_replyend;
void void
env_opt_start() env_opt_start()
{ {
if (opt_reply) if (opt_reply) {
opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE); void *tmp = realloc (opt_reply, OPT_REPLY_SIZE);
if (tmp != NULL) {
opt_reply = tmp;
} else {
free (opt_reply);
opt_reply = NULL;
}
else else
opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE); opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
if (opt_reply == NULL) { if (opt_reply == NULL) {
@@ -1464,14 +1470,16 @@ env_opt_add(unsigned char *ep)
strlen((char *)ep) + 6 > opt_replyend) strlen((char *)ep) + 6 > opt_replyend)
{ {
int len; int len;
void *tmp;
opt_replyend += OPT_REPLY_SIZE; opt_replyend += OPT_REPLY_SIZE;
len = opt_replyend - opt_reply; len = opt_replyend - opt_reply;
opt_reply = (unsigned char *)realloc(opt_reply, len); tmp = realloc(opt_reply, len);
if (opt_reply == NULL) { if (tmp == NULL) {
/*@*/ printf("env_opt_add: realloc() failed!!!\n"); /*@*/ printf("env_opt_add: realloc() failed!!!\n");
opt_reply = opt_replyp = opt_replyend = NULL; opt_reply = opt_replyp = opt_replyend = NULL;
return; return;
} }
opt_reply = tmp;
opt_replyp = opt_reply + len - (opt_replyend - opt_replyp); opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
opt_replyend = opt_reply + len; opt_replyend = opt_reply + len;
} }