changeset 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
files array.c array.h
diffstat 2 files changed, 36 insertions(+), 52 deletions(-) [+]
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;
 }
--- a/array.h	Thu Sep 22 13:03:27 2011 +0200
+++ b/array.h	Thu Sep 22 21:07:51 2011 +0200
@@ -43,12 +43,12 @@
 int	array_push(struct array *, const void *);
 int	array_insert(struct array *, const void *, int);
 int	array_append(struct array *, const void *);
-void	*array_pop(struct array *);
-void	*array_unqueue(struct array *);
-void	*array_remove(struct array *, int);
+void	array_pop(struct array *);
+void	array_unqueue(struct array *);
+void	array_remove(struct array *, int);
 int	array_swap(struct array *, int, int);
-void	array_map(struct array *, void (*fn)(void *, void *), void *);
-void	*array_find(struct array *, int (*fn)(void *, void *), int *, void *);
+void	array_map(const struct array *, void (*fn)(void *, void *), void *);
+void	*array_find(const struct array *, int (*fn)(void *, void *), int *, void *);
 void	array_clear(struct array *);
 void	array_free(struct array *);
 
@@ -60,4 +60,9 @@
 	    var = (void *) ((char *) a->data + a->unit * (a->length - 1));	\
 	    i < a->length; ++i, --var)	
 
+#define ARRAY_HEAD(a)								\
+	a->data
+#define ARRAY_TAIL(a)								\
+	(void *) ((char *) a->data + (a->unit * (a->length - 1)))
+
 #endif /* _ARRAY_H_ */