summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-10-06 12:37:01 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-10-06 12:37:01 -0400
commit054ef7f719ce6fd2167f1c1b4433feaa438bebfc (patch)
tree47d8e315d4bc5d3e125309a2782b84c83343830d /tests
parentbcfe9c4e846bc97f97967c7df95e6b0a08a9a0ad (diff)
downloadbfs-054ef7f719ce6fd2167f1c1b4433feaa438bebfc.tar.xz
alloc: Test allocation size overflows
Diffstat (limited to 'tests')
-rw-r--r--tests/alloc.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/tests/alloc.c b/tests/alloc.c
index 9e6e892..382131f 100644
--- a/tests/alloc.c
+++ b/tests/alloc.c
@@ -3,6 +3,7 @@
#include "../src/alloc.h"
#include "../src/diag.h"
+#include <errno.h>
#include <stdlib.h>
int main(void) {
@@ -13,13 +14,20 @@ int main(void) {
};
bfs_verify(sizeof_flex(struct flexible, bar, 0) >= sizeof(struct flexible));
bfs_verify(sizeof_flex(struct flexible, bar, 16) % alignof(struct flexible) == 0);
- bfs_verify(sizeof_flex(struct flexible, bar, SIZE_MAX / sizeof(int) + 1)
- == align_floor(alignof(struct flexible), SIZE_MAX));
+
+ size_t too_many = SIZE_MAX / sizeof(int) + 1;
+ bfs_verify(sizeof_flex(struct flexible, bar, too_many) == align_floor(alignof(struct flexible), SIZE_MAX));
// Corner case: sizeof(type) > align_ceil(alignof(type), offsetof(type, member))
// Doesn't happen in typical ABIs
bfs_verify(flex_size(8, 16, 4, 4, 1) == 16);
+ // Make sure we detect allocation size overflows
+ bfs_verify(ALLOC_ARRAY(int, too_many) == NULL && errno == EOVERFLOW);
+ bfs_verify(ZALLOC_ARRAY(int, too_many) == NULL && errno == EOVERFLOW);
+ bfs_verify(ALLOC_FLEX(struct flexible, bar, too_many) == NULL && errno == EOVERFLOW);
+ bfs_verify(ZALLOC_FLEX(struct flexible, bar, too_many) == NULL && errno == EOVERFLOW);
+
// varena tests
struct varena varena;
VARENA_INIT(&varena, struct flexible, bar);