summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@sun.com>2008-10-22 09:28:56 -0700
committerAlan Coopersmith <alan.coopersmith@sun.com>2008-10-22 09:28:56 -0700
commit7dee380747bbd0a07c64d8cdb2a229485403847a (patch)
treea20ed264010b7c1f637fdaa21d9b611c21db6f26
parent01ac0b14bea486a626d8565dec8c8e546bb3d82f (diff)
Use realloc to resize buffer, not malloc+strcpy+free
-rw-r--r--info.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/info.c b/info.c
index a560623..7760ae4 100644
--- a/info.c
+++ b/info.c
@@ -108,15 +108,18 @@ AppendStr(Buffer *buffer, char *str)
if ((buffer->bytesLeft - 1) < len)
{
int newBufSize = buffer->bufSize + len + BUF_GROW_SIZE;
- char *newbuf = (char *) malloc (newBufSize);
int bytesUsed = buffer->bufPtr - buffer->bufStart;
- memcpy (newbuf, buffer->bufStart, bytesUsed);
- newbuf[bytesUsed] = '\0';
- free (buffer->bufStart);
- buffer->bufStart = newbuf;
- buffer->bufPtr = newbuf + bytesUsed;
- buffer->bufSize = newBufSize;
- buffer->bytesLeft = newBufSize - bytesUsed;
+ char *newbuf = realloc (buffer->bufStart, newBufSize);
+ if (newbuf != NULL) {
+ newbuf[bytesUsed] = '\0';
+ buffer->bufStart = newbuf;
+ buffer->bufPtr = newbuf + bytesUsed;
+ buffer->bufSize = newBufSize;
+ buffer->bytesLeft = newBufSize - bytesUsed;
+ } else {
+ perror("realloc failed, aborting");
+ exit(1);
+ }
}
strcat (buffer->bufPtr, str);