From 5c5d2a6db3ba48381c9135c2f9d69a519f9b061c Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Sat, 24 Dec 2022 18:36:26 +0100 Subject: More assertions --- source/kit/dynamic_array.c | 16 +++++++++++++++- source/kit/input_buffer.c | 30 +++++++++++++++++++++++------- source/kit/secure_random.c | 10 +++------- 3 files changed, 41 insertions(+), 15 deletions(-) (limited to 'source') diff --git a/source/kit/dynamic_array.c b/source/kit/dynamic_array.c index 3bda284..1829c45 100644 --- a/source/kit/dynamic_array.c +++ b/source/kit/dynamic_array.c @@ -1,9 +1,16 @@ #include "dynamic_array.h" +#include #include void kit_da_init(kit_da_void_t *array, ptrdiff_t element_size, ptrdiff_t size, kit_allocator_t alloc) { + assert(array != NULL); + assert(element_size > 0); + assert(size >= 0); + assert(alloc.allocate != NULL); + assert(alloc.deallocate != NULL); + memset(array, 0, sizeof(kit_da_void_t)); if (size > 0) @@ -28,11 +35,18 @@ static ptrdiff_t eval_capacity(ptrdiff_t current_cap, void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, ptrdiff_t size) { + assert(array != NULL); + assert(element_size > 0); + assert(size >= 0); + if (size <= array->capacity) { array->size = size; } else { ptrdiff_t capacity = eval_capacity(array->capacity, size); + assert(array->alloc.allocate != NULL); + assert(array->alloc.deallocate != NULL); + void *bytes = array->alloc.allocate(array->alloc.state, element_size * capacity); if (bytes != NULL) { @@ -45,4 +59,4 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, array->values = bytes; } } -} \ No newline at end of file +} diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c index 5c38c22..208d0de 100644 --- a/source/kit/input_buffer.c +++ b/source/kit/input_buffer.c @@ -1,5 +1,6 @@ #include "input_buffer.h" +#include #include typedef struct { @@ -11,8 +12,10 @@ typedef struct { static internal_buffer_t *buf_init(kit_is_handle_t upstream, kit_allocator_t alloc) { - internal_buffer_t *buf; - buf = alloc.allocate(alloc.state, sizeof *buf); + assert(alloc.allocate != NULL); + internal_buffer_t *const buf = alloc.allocate(alloc.state, + sizeof *buf); + if (buf != NULL) { memset(buf, 0, sizeof *buf); buf->ref_count = 1; @@ -20,29 +23,42 @@ static internal_buffer_t *buf_init(kit_is_handle_t upstream, buf->alloc = alloc; DA_INIT(buf->data, 0, alloc); } + return buf; } static kit_allocator_t buf_alloc(void *p) { + assert(p != NULL); + return ((internal_buffer_t *) p)->alloc; } static void buf_acquire(void *p) { - internal_buffer_t *buf = (internal_buffer_t *) p; - buf->ref_count++; + assert(p != NULL); + + ((internal_buffer_t *) p)->ref_count++; } static void buf_release(void *p) { - internal_buffer_t *buf = (internal_buffer_t *) p; + assert(p != NULL); + + internal_buffer_t *const buf = (internal_buffer_t *) p; + if (--buf->ref_count == 0) { DA_DESTROY(buf->data); + + assert(buf->alloc.deallocate != NULL); buf->alloc.deallocate(buf->alloc.state, buf); } } static void buf_adjust(void *p, ptrdiff_t size) { - internal_buffer_t *buf = (internal_buffer_t *) p; - ptrdiff_t offset = buf->data.size; + assert(p != NULL); + assert(size >= 0); + + internal_buffer_t *const buf = (internal_buffer_t *) p; + ptrdiff_t const offset = buf->data.size; + if (offset < size) { DA_RESIZE(buf->data, size); kit_out_str_t destination = { diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c index 65b567c..2f1c796 100644 --- a/source/kit/secure_random.c +++ b/source/kit/secure_random.c @@ -108,15 +108,11 @@ void kit_secure_random(ptrdiff_t const size, void *const data) { } size_t const n = fread(data, 1, size, f); + fclose(f); + assert(n == size); - if (n != size) { + if (n != size) secure_random_fallback(size, data); - fclose(f); - return; - } - - fclose(f); - return; #endif } -- cgit v1.2.3