changeset 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 02b0ee204042
children 19c0d2e11406
files array.c buffer.c
diffstat 2 files changed, 16 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/array.c	Wed Sep 21 12:03:23 2011 +0200
+++ b/array.c	Thu Sep 22 13:03:27 2011 +0200
@@ -31,7 +31,7 @@
 {
 	struct array *arr;
 
-	if (unit == 0 || !(arr = malloc(sizeof (struct array))))
+	if (unit == 0 || (arr = malloc(sizeof (struct array))) == NULL)
 		return NULL;
 
 	arr->tmp	= NULL;
@@ -41,7 +41,7 @@
 	arr->unit	= unit;
 	arr->size	= SIZE(arr->bsize);
 
-	if (!(arr->data = malloc(arr->size))) {
+	if ((arr->data = malloc(arr->size)) == NULL) {
 		free(arr);
 		return NULL;
 	}
@@ -182,7 +182,7 @@
 	 * function.
 	 */
 
-	if (!arr->tmp && !(arr->tmp = malloc(arr->unit)))
+	if (arr->tmp == NULL && (arr->tmp = malloc(arr->unit)) == NULL)
 		return -1;
 
 	memcpy((char *) arr->tmp, (char *) arr->data + SIZE(i1), arr->unit);
@@ -220,7 +220,7 @@
 	int st, i;
 	void *data;
 
-	for (i = st = 0; i < arr->length && !st; ++i)
+	for (i = st = 0; i < arr->length && st != 1; ++i)
 		st = fn((char *) arr->data + SIZE(i), udata);
 
 	if (st)	{
@@ -275,8 +275,8 @@
 		return 0;
 
 	if (arr->flags & ARRAY_AUTO) {
-		if (!(arr->data = realloc(arr->data, arr->size +
-		    SIZE(arr->bsize))))
+		if ((arr->data = realloc(arr->data, arr->size +
+		    SIZE(arr->bsize))) == NULL)
 			return -1;
 
 		arr->size += SIZE(arr->bsize);
--- 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;