summaryrefslogtreecommitdiffstats
path: root/libdimension/dimension
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2010-01-18 23:33:16 -0500
committerTavian Barnes <tavianator@gmail.com>2010-01-18 23:33:16 -0500
commitf20792e865f90665bba84c16e94cf4a3aa682a42 (patch)
treee6097cb7b8c57575d259c0333ece06e559104cd7 /libdimension/dimension
parent1e0b3d9fd77b1e9809c7724325cf52ddf29d1eb0 (diff)
downloaddimension-f20792e865f90665bba84c16e94cf4a3aa682a42.tar.xz
Add proper range checks to dmnsn_array_insert() and _remove().
Diffstat (limited to 'libdimension/dimension')
-rw-r--r--libdimension/dimension/array.h15
1 files changed, 11 insertions, 4 deletions
diff --git a/libdimension/dimension/array.h b/libdimension/dimension/array.h
index 1b3771d..0a2551b 100644
--- a/libdimension/dimension/array.h
+++ b/libdimension/dimension/array.h
@@ -147,13 +147,15 @@ dmnsn_array_pop(dmnsn_array *array, void *obj)
DMNSN_INLINE void
dmnsn_array_insert(dmnsn_array *array, size_t i, const void *obj)
{
- size_t size = dmnsn_array_size(array);
- /* Increase the size by 1 */
- dmnsn_array_resize(array, size + 1);
+ size_t size = dmnsn_array_size(array) + 1;
+ if (i >= size)
+ size = i + 1;
+ dmnsn_array_resize(array, size);
+
/* Move the elements at and after `i' 1 to the right */
memmove((char *)array->ptr + array->obj_size*(i + 1),
(char *)array->ptr + array->obj_size*i,
- array->obj_size*(size - i));
+ array->obj_size*(size - i - 1));
/* Insert `obj' at `i' */
memcpy((char *)array->ptr + array->obj_size*i, obj, array->obj_size);
}
@@ -163,6 +165,11 @@ DMNSN_INLINE void
dmnsn_array_remove(dmnsn_array *array, size_t i)
{
size_t size = dmnsn_array_size(array);
+ if (i >= size) {
+ /* Range check failed */
+ dmnsn_error(DMNSN_SEVERITY_HIGH, "Array index out of bounds.");
+ }
+
/* Move the array elements after `i' 1 to the left */
memmove((char *)array->ptr + array->obj_size*i,
(char *)array->ptr + array->obj_size*(i + 1),