diff array.c @ 29:19c0d2e11406

Security fixes for array
author David Demelier <markand@malikania.fr>
date Thu, 22 Sep 2011 21:07:51 +0200
parents 904a373aa120
children e2c3a0a549d2
line wrap: on
line diff
--- a/array.c	Thu Sep 22 13:03:27 2011 +0200
+++ b/array.c	Thu Sep 22 21:07:51 2011 +0200
@@ -104,64 +104,43 @@
 }
 
 /*
- * Remove the array's head and return the object or NULL if
- * the array is empty.
+ * Remove the array's head.
  */
 
-void *
+void
 array_pop(struct array *arr)
 {
-	void *data;
-
-	if (arr->length == 0)
-		return NULL;
-
-	data = arr->data;
-	memmove((char *) arr->data, (char *) arr->data + SIZE(1),
-	    SIZE(arr->length));
-	memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit);
-
-	return data;
+	if (arr->length > 0) {
+		memmove((char *) arr->data, (char *) arr->data + SIZE(1),
+		    SIZE(--arr->length));
+		memset((char *) arr->data + SIZE(arr->length), 0, arr->unit);
+	}
 }
 
 /*
- * Remove the array's queue and return the object or NULL
- * if the array is empty.
+ * Remove the array's tail.
  */
 
-void *
+void
 array_unqueue(struct array *arr)
 {
-	void *data;
-
-	if (arr->length == 0)
-		return NULL;
-
-	data = (char *) arr->data + SIZE(--arr->length);
-	memset((char *) arr->data + SIZE(arr->length), 0, arr->unit);
-
-	return data;
+	if (arr->length > 0)
+		memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit);
 }
 
 /*
- * Remove the entry at the specified index and return it. If the index is out of
- * bounds or the list is empty the functions returns NULL.
+ * Remove the data at the specified index. Bounds are checked.
  */
 
-void *
+void
 array_remove(struct array *arr, int index)
 {
-	void *data;
-
-	if (arr->length == 0 || index < 0 || index > arr->length - 1)
-		return NULL;
-
-	data = (char *) arr->data + SIZE(index);
-	memmove((char *) arr->data + SIZE(index),
-	    (char *) arr->data + SIZE(index + 1), SIZE(arr->length - index));
-	memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit);
-
-	return data;
+	if (arr->length > 0 && index >= 0 && index < arr->length) {
+		memmove((char *) arr->data + SIZE(index),
+		    (char *) arr->data + SIZE(index + 1),
+		    SIZE(arr->length - index - 1));
+		memset((char *) arr->data + SIZE(--arr->length), 0, arr->unit);
+	}
 }
 
 /*
@@ -174,7 +153,7 @@
 array_swap(struct array *arr, int i1, int i2)
 {
 	/* Out of bounds */
-	if (i1 > arr->length - 1|| i1 < 0 || i2 > arr->length - 1 || i2 < 0)
+	if (i1 >= arr->length || i1 < 0 || i2 >= arr->length || i2 < 0)
 		return -1;
 
 	/*
@@ -199,7 +178,7 @@
  */
 
 void
-array_map(struct array *arr, void (*fn)(void *, void *), void *udata)
+array_map(const struct array *arr, void (*fn)(void *, void *), void *udata)
 {
 	int i;
 
@@ -215,18 +194,18 @@
  */
 
 void *
-array_find(struct array *arr, int (*fn)(void *, void *), int *idx, void *udata)
+array_find(const struct array *arr, int (*fn)(void *, void *), int *ix, void *u)
 {
 	int st, i;
 	void *data;
 
 	for (i = st = 0; i < arr->length && st != 1; ++i)
-		st = fn((char *) arr->data + SIZE(i), udata);
+		st = fn((char *) arr->data + SIZE(i), u);
 
 	if (st)	{
 		data = (char *) arr->data + SIZE(--i);
-		if (idx)
-			*idx = i;
+		if (ix)
+			*ix = i;
 	} else
 		data = NULL;
 
@@ -281,7 +260,7 @@
 
 		arr->size += SIZE(arr->bsize);
 	} else
-		return (arr->size / arr->unit <= (size_t) arr->length) ? -1 : 0;
+		return -1;
 
 	return 0;
 }