Skip to content

Commit f572bbe

Browse files
authored
Merge pull request #21610 from mguetschow/unicoap-test-stack-overflow
unittests/unicoap: statically allocate option buffers
2 parents 4f9f755 + 8623772 commit f572bbe

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

sys/include/net/unicoap/options.h

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ static inline void unicoap_options_clear(unicoap_options_t* options)
177177
* @{
178178
*/
179179
#ifndef DOXYGEN
180-
# define _UNICOAP_OPTIONS_ALLOC(_buf, _name, capacity) \
181-
uint8_t _buf[capacity]; \
182-
unicoap_options_t _name; \
183-
unicoap_options_init(&_name, _buf, capacity);
180+
# define _UNICOAP_OPTIONS_ALLOC(_buf, _name, capacity, _static) \
181+
_static uint8_t _buf[capacity]; \
182+
_static unicoap_options_t _name = { \
183+
.entries = { { .data = _buf } }, \
184+
.storage_capacity = capacity, \
185+
};
184186
#endif
185187

186188
/**
@@ -190,22 +192,56 @@ static inline void unicoap_options_clear(unicoap_options_t* options)
190192
* @param capacity Storage buffer capacity in bytes
191193
*
192194
* Allocates a new @ref unicoap_options_t container and a storage buffer with
193-
* the given capacity, then calls @ref unicoap_options_t::unicoap_options_init.
195+
* the given capacity, and initializes it. No need to call
196+
* @ref unicoap_options_t::unicoap_options_init afterwards.
197+
*
198+
* See @ref UNICOAP_OPTIONS_ALLOC_STATIC for static allocation
194199
*/
195200
#define UNICOAP_OPTIONS_ALLOC(name, capacity) \
196-
_UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity)
201+
_UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity,)
202+
203+
/**
204+
* @brief Statically allocates options with buffer capacity
205+
*
206+
* @param name Name of the variable storing the options structure
207+
* @param capacity Static storage buffer capacity in bytes
208+
*
209+
* Statically allocates a new @ref unicoap_options_t container and a storage
210+
* buffer with the given capacity, and initializes it. No need to call
211+
* @ref unicoap_options_t::unicoap_options_init afterwards.
212+
*
213+
* See @ref UNICOAP_OPTIONS_ALLOC for non-static allocation
214+
*/
215+
#define UNICOAP_OPTIONS_ALLOC_STATIC(name, capacity) \
216+
_UNICOAP_OPTIONS_ALLOC(_CONCAT3(name, _storage, __LINE__), name, capacity, static)
197217

198218
/**
199219
* @brief Allocates options with default capacity
200220
*
201221
* @param name Name of the variable storing the options structure
202222
*
203223
* Allocates a new @ref unicoap_options_t container and a storage buffer with
204-
* @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY,
205-
* then calls @ref unicoap_options_t::unicoap_options_init.
224+
* @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it.
225+
* No need to call @ref unicoap_options_t::unicoap_options_init afterwards.
226+
*
227+
* See @ref UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT for static allocation
206228
*/
207229
#define UNICOAP_OPTIONS_ALLOC_DEFAULT(name) \
208230
UNICOAP_OPTIONS_ALLOC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
231+
232+
/**
233+
* @brief Statically allocates options with default capacity
234+
*
235+
* @param name Name of the variable storing the options structure
236+
*
237+
* Statically allocates a new @ref unicoap_options_t container and a storage buffer
238+
* with @ref CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY, and initializes it.
239+
* No need to call @ref unicoap_options_t::unicoap_options_init afterwards.
240+
*
241+
* See @ref UNICOAP_OPTIONS_ALLOC_DEFAULT for non-static allocation
242+
*/
243+
#define UNICOAP_OPTIONS_ALLOC_STATIC_DEFAULT(name) \
244+
UNICOAP_OPTIONS_ALLOC_STATIC(name, CONFIG_UNICOAP_OPTIONS_BUFFER_DEFAULT_CAPACITY)
209245
/** @} */
210246

211247
/* MARK: - Option characteristics */

tests/unittests/Makefile.ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ BOARD_INSUFFICIENT_MEMORY := \
7979
pba-d-01-kw2x \
8080
remote-pa \
8181
remote-reva \
82+
remote-revb \
8283
samd10-xmini \
8384
samd20-xpro \
8485
samd21-xpro \

tests/unittests/tests-unicoap/tests-unicoap-options.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void assert_options_data(const unicoap_options_t* options)
5959

6060
static void test_in_order(void)
6161
{
62-
UNICOAP_OPTIONS_ALLOC(options, 100);
62+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 100);
6363
TEST_ASSERT_EQUAL_INT(unicoap_options_add_uri_path_component_string(&options, "actuators"), 0);
6464
TEST_ASSERT_EQUAL_INT(unicoap_options_add_uri_path_component_string(&options, "leds"), 0);
6565
TEST_ASSERT_EQUAL_INT(unicoap_options_set_content_format(&options, UNICOAP_FORMAT_JSON), 0);
@@ -71,7 +71,7 @@ static void test_in_order(void)
7171

7272
static void test_out_of_order(void)
7373
{
74-
UNICOAP_OPTIONS_ALLOC(options, 100);
74+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 100);
7575
TEST_ASSERT_EQUAL_INT(unicoap_options_set_accept(&options, UNICOAP_FORMAT_JSON), 0);
7676
TEST_ASSERT_EQUAL_INT(unicoap_options_add_uri_path_component_string(&options, "actuators"), 0);
7777
TEST_ASSERT_EQUAL_INT(unicoap_options_add_uri_query_string(&options, "color=g"), 0);
@@ -83,7 +83,7 @@ static void test_out_of_order(void)
8383

8484
static void test_idempotent(void)
8585
{
86-
UNICOAP_OPTIONS_ALLOC(options, 100);
86+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 100);
8787
TEST_ASSERT_EQUAL_INT(unicoap_options_set_accept(&options, UNICOAP_FORMAT_JSON), 0);
8888
TEST_ASSERT_EQUAL_INT(unicoap_options_set_accept(&options, UNICOAP_FORMAT_JSON), 0);
8989
TEST_ASSERT_EQUAL_INT(unicoap_options_add_uri_path_component_string(&options, "actuators"), 0);
@@ -112,7 +112,7 @@ static void _populate(unicoap_options_t* options)
112112

113113
static void test_extended_uint_shifts(void)
114114
{
115-
UNICOAP_OPTIONS_ALLOC(options, 2100);
115+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 900);
116116
_populate(&options);
117117

118118
/* options blob, from nanoCoAP */
@@ -180,7 +180,7 @@ static void test_extended_uint_shifts(void)
180180

181181
static void test_remove_leading(void)
182182
{
183-
UNICOAP_OPTIONS_ALLOC(options, 2100);
183+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 900);
184184
_populate(&options);
185185

186186
TEST_ASSERT_EQUAL_INT(unicoap_options_remove(&options, 1), 0);
@@ -250,7 +250,7 @@ static void test_remove_leading(void)
250250

251251
static void test_remove_trailing(void)
252252
{
253-
UNICOAP_OPTIONS_ALLOC(options, 2100);
253+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 900);
254254
_populate(&options);
255255

256256
TEST_ASSERT_EQUAL_INT(unicoap_options_remove(&options, 70), 0);
@@ -320,7 +320,7 @@ static void test_remove_trailing(void)
320320

321321
static void test_remove_multiple(void)
322322
{
323-
UNICOAP_OPTIONS_ALLOC(options, 2100);
323+
UNICOAP_OPTIONS_ALLOC_STATIC(options, 900);
324324
_populate(&options);
325325

326326
TEST_ASSERT_EQUAL_INT(unicoap_options_remove(&options, 12), 0);

0 commit comments

Comments
 (0)