diff buffer.c @ 28:904a373aa120

Buffer security fix. Also switched to == NULL statement instead of ! as advised in man style(9)
author David Demelier <markand@malikania.fr>
date Thu, 22 Sep 2011 13:03:27 +0200
parents ae4128d16c92
children 5252fa9b5cb1
line wrap: on
line diff
--- a/buffer.c	Wed Sep 21 12:03:23 2011 +0200
+++ b/buffer.c	Thu Sep 22 13:03:27 2011 +0200
@@ -34,16 +34,20 @@
 {
 	struct buffer *buf;
 
-	if (!(buf = malloc(sizeof (struct buffer))))
+	if ((buf = malloc(sizeof (struct buffer))) == NULL)
 		return NULL;
 
 	memset(buf, 0, sizeof (struct buffer));
 	buf->bsize	= (bsize == 0) ? BUFFER_DEFAULT_BSIZE : bsize;
-	buf->size	= bsize + 1;
+	buf->size	= buf->bsize + 1;
 	buf->flags	= flags;
 
-	if (!(buf->data = calloc(bsize + 1, 1)) ||
-	    (str && buffer_strcat(buf, str) < 0)) {
+	if ((buf->data = calloc(buf->size, 1)) == NULL) {
+		free(buf);
+		return NULL;
+	}
+
+	if (str != NULL && buffer_strcat(buf, str) < 0) {
 		free(buf);
 		return NULL;
 	}
@@ -175,7 +179,7 @@
 int
 buffer_shrink(struct buffer *buf)
 {
-	if (!(buf->data = realloc(buf->data, buf->length + 1)))
+	if ((buf->data = realloc(buf->data, buf->length + 1)) == NULL)
 		return -1;
 
 	buf->size = buf->length + 1;
@@ -238,7 +242,7 @@
 		while (newlen - buf->length - 1 <= needed)
 			newlen += buf->bsize;
 
-		if (!(buf->data = realloc(buf->data, newlen)))
+		if ((buf->data = realloc(buf->data, newlen)) == NULL)
 			return -1;
 
 		buf->size = newlen;