darray: use unsigned int instead of size_t for array size size_t is too large; if we ever need it, that's the least of our problems. Besides, when we roll our own (e.g. in keymap.h) it's already unsigned int. Instead, add some emergency overflow check. So, why? - It plays nicer with all the other uint32_t's and unsigned int's (no extensions, etc.). - Reduces keymap memory usage by 5% or so as a bonus. Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
diff --git a/src/darray.h b/src/darray.h
index b6ad2cf..e44b2e7 100644
--- a/src/darray.h
+++ b/src/darray.h
@@ -23,10 +23,15 @@
#ifndef CCAN_DARRAY_H
#define CCAN_DARRAY_H
+/* Originally taken from: http://ccodearchive.net/info/darray.html
+ * But modified for libxkbcommon. */
+
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
+#include <limits.h>
-#define darray(type) struct { type *item; size_t size; size_t alloc; }
+#define darray(type) struct { type *item; unsigned size; unsigned alloc; }
#define darray_new() { 0, 0, 0 }
@@ -85,13 +90,13 @@ typedef darray (unsigned long) darray_ulong;
/*** Insertion (multiple items) ***/
#define darray_append_items(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
+ unsigned __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __oldSize + __count); \
memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
} while (0)
#define darray_from_items(arr, items, count) do { \
- size_t __count = (count); \
+ unsigned __count = (count); \
darray_resize(arr, __count); \
memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
} while (0)
@@ -113,14 +118,14 @@ typedef darray (unsigned long) darray_ulong;
} while (0)
#define darray_appends_nullterminate(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
+ unsigned __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __oldSize + __count + 1); \
memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
(arr).item[--(arr).size] = 0; \
} while (0)
#define darray_prepends_nullterminate(arr, items, count) do { \
- size_t __count = (count), __oldSize = (arr).size; \
+ unsigned __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __count + __oldSize + 1); \
memmove((arr).item + __count, (arr).item, \
__oldSize * sizeof(*(arr).item)); \
@@ -134,7 +139,7 @@ typedef darray (unsigned long) darray_ulong;
darray_growalloc(arr, (arr).size = (newSize))
#define darray_resize0(arr, newSize) do { \
- size_t __oldSize = (arr).size, __newSize = (newSize); \
+ unsigned __oldSize = (arr).size, __newSize = (newSize); \
(arr).size = __newSize; \
if (__newSize > __oldSize) { \
darray_growalloc(arr, __newSize); \
@@ -149,14 +154,16 @@ typedef darray (unsigned long) darray_ulong;
} while (0)
#define darray_growalloc(arr, need) do { \
- size_t __need = (need); \
+ unsigned __need = (need); \
if (__need > (arr).alloc) \
- darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
+ darray_realloc(arr, darray_next_alloc((arr).alloc, __need, \
+ sizeof(*(arr).item))); \
} while (0)
-static inline size_t
-darray_next_alloc(size_t alloc, size_t need)
+static inline unsigned
+darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize)
{
+ assert(need < UINT_MAX / itemSize / 2); /* Overflow. */
if (alloc == 0)
alloc = 4;
while (alloc < need)