diff --git a/include/zenoh-pico/api/handlers.h b/include/zenoh-pico/api/handlers.h index e39281c31..489ba4605 100644 --- a/include/zenoh-pico/api/handlers.h +++ b/include/zenoh-pico/api/handlers.h @@ -113,6 +113,9 @@ extern "C" { \ static inline int8_t handler_new_f_name(callback_type *callback, z_owned_##handler_name##_t *handler, \ size_t capacity) { \ + if (capacity < 1) { \ + return _Z_ERR_INVALID; \ + } \ handler->_val.collection = collection_new_f(capacity); \ if (handler->_val.collection == NULL) { \ return _Z_ERR_SYSTEM_OUT_OF_MEMORY; \ diff --git a/tests/z_channels_test.c b/tests/z_channels_test.c index 701cf7656..98301411b 100644 --- a/tests/z_channels_test.c +++ b/tests/z_channels_test.c @@ -186,9 +186,24 @@ void sample_ring_channel_test_over_size(void) { z_drop(z_move(handler)); } +void zero_size_test(void) { + z_owned_closure_sample_t closure; + + z_owned_fifo_handler_sample_t fifo_handler; + assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 0) != Z_OK); + assert(z_fifo_channel_sample_new(&closure, &fifo_handler, 1) == Z_OK); + z_drop(z_move(fifo_handler)); + + z_owned_ring_handler_sample_t ring_handler; + assert(z_ring_channel_sample_new(&closure, &ring_handler, 0) != Z_OK); + assert(z_ring_channel_sample_new(&closure, &ring_handler, 1) == Z_OK); + z_drop(z_move(ring_handler)); +} + int main(void) { sample_fifo_channel_test(); sample_fifo_channel_test_try_recv(); sample_ring_channel_test_in_size(); sample_ring_channel_test_over_size(); + zero_size_test(); }