diff options
author | Alan Coopersmith <alan.coopersmith@sun.com> | 2008-10-22 09:28:56 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@sun.com> | 2008-10-22 09:28:56 -0700 |
commit | 7dee380747bbd0a07c64d8cdb2a229485403847a (patch) | |
tree | a20ed264010b7c1f637fdaa21d9b611c21db6f26 | |
parent | 01ac0b14bea486a626d8565dec8c8e546bb3d82f (diff) |
Use realloc to resize buffer, not malloc+strcpy+free
-rw-r--r-- | info.c | 19 |
1 files changed, 11 insertions, 8 deletions
@@ -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); |