added guard to vector_prune() to check that element_size > 0

This commit is contained in:
2026-08-19 22:23:52 +02:00
parent 8af8473c72
commit d912d6d559
+11
View File
@@ -93,6 +93,13 @@ SOFTWARE.
typedef void (*vector_destructor_t)(void* element); // Function pointer type for element destructor 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 { typedef struct {
size_t size; // Number of elements in the vector size_t size; // Number of elements in the vector
size_t capacity; // Allocated capacity of 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 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) { if (vec->size < vec->capacity) {
size_t new_capacity = vec->size > 0 ? vec->size : 1; // Ensure capacity is at least 1 size_t new_capacity = vec->size > 0 ? vec->size : 1; // Ensure capacity is at least 1