diff --git a/vector.h b/vector.h index ef9504e..b82a7cc 100644 --- a/vector.h +++ b/vector.h @@ -93,6 +93,13 @@ SOFTWARE. typedef void (*vector_destructor_t)(void* element); // Function pointer type for element destructor +/* + A vector. Do not construct, copy or modify this struct directly - the fields are + visible only because the functions below are inline. Create vectors with + vector_create() and use the accessors. A vector_t that did not come from + vector_create() is not a valid vector, and the functions will reject it where + they can and misbehave where they cannot. +*/ typedef struct { size_t size; // Number of elements in the vector size_t capacity; // Allocated capacity of the vector @@ -297,6 +304,10 @@ static inline int vector_prune(vector_t* vec) { return -1; // Invalid vector } + if (vec->element_size == 0) { + return -1; // Not a usable vector; also guards the division below + } + if (vec->size < vec->capacity) { size_t new_capacity = vec->size > 0 ? vec->size : 1; // Ensure capacity is at least 1