Skip to content

Commit fd68fc4

Browse files
yonschnashif
authored andcommitted
bindesc: Add maximum data size and assertion
Add a Kconfig symbol to limit the maximum size of a descriptor's data, enforced by a build assertion. Signed-off-by: Yonatan Schachter <[email protected]>
1 parent 5da7ba5 commit fd68fc4

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

include/zephyr/bindesc.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ extern "C" {
171171
* @param id Unique ID of the descriptor
172172
* @param value A string value for the descriptor
173173
*/
174-
#define BINDESC_STR_DEFINE(name, id, value) \
175-
__BINDESC_ENTRY_DEFINE(name) = { \
176-
.tag = BINDESC_TAG(STR, id), \
177-
.len = (uint16_t)sizeof(value), \
178-
.data = value, \
179-
}
174+
#define BINDESC_STR_DEFINE(name, id, value) \
175+
__BINDESC_ENTRY_DEFINE(name) = { \
176+
.tag = BINDESC_TAG(STR, id), \
177+
.len = (uint16_t)sizeof(value), \
178+
.data = value, \
179+
}; \
180+
BUILD_ASSERT(sizeof(value) <= CONFIG_BINDESC_DEFINE_MAX_DATA_SIZE, \
181+
"Bindesc " STRINGIFY(name) " exceeded maximum size, consider reducing the" \
182+
" size or changing CONFIG_BINDESC_DEFINE_MAX_DATA_SIZE. ")
180183

181184
/**
182185
* @brief Define a binary descriptor of type uint.
@@ -217,12 +220,16 @@ extern "C" {
217220
* @param id Unique ID of the descriptor
218221
* @param value A uint8_t array as data for the descriptor
219222
*/
220-
#define BINDESC_BYTES_DEFINE(name, id, value) \
221-
__BINDESC_ENTRY_DEFINE(name) = { \
222-
.tag = BINDESC_TAG(BYTES, id), \
223-
.len = (uint16_t)sizeof((uint8_t [])__DEBRACKET value), \
224-
.data = __DEBRACKET value, \
225-
}
223+
#define BINDESC_BYTES_DEFINE(name, id, value) \
224+
__BINDESC_ENTRY_DEFINE(name) = { \
225+
.tag = BINDESC_TAG(BYTES, id), \
226+
.len = (uint16_t)sizeof((uint8_t [])__DEBRACKET value), \
227+
.data = __DEBRACKET value, \
228+
}; \
229+
BUILD_ASSERT(sizeof((uint8_t [])__DEBRACKET value) <= \
230+
CONFIG_BINDESC_DEFINE_MAX_DATA_SIZE, \
231+
"Bindesc " STRINGIFY(name) " exceeded maximum size, consider reducing the" \
232+
" size or changing CONFIG_BINDESC_DEFINE_MAX_DATA_SIZE. ")
226233

227234
/**
228235
* @brief Get the value of a string binary descriptor

subsys/bindesc/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ source "subsys/bindesc/Kconfig.version"
2020
source "subsys/bindesc/Kconfig.build_time"
2121
source "subsys/bindesc/Kconfig.host_info"
2222

23+
config BINDESC_DEFINE_MAX_DATA_SIZE
24+
int "Bindesc max data size"
25+
range 4 $(UINT16_MAX)
26+
default 128
27+
help
28+
Determines the maximum size of a binary descriptor's data. The theoretical
29+
limit to this value is the maximum value of a uint16_t (65535), in practice
30+
it's recommened to keep this value much smaller for easier handling of the data.
31+
2332
endif # BINDESC_DEFINE
2433

2534
endif # BINDESC

0 commit comments

Comments
 (0)