@@ -304,19 +304,29 @@ typedef union
304304 * @brief Read an named enumerator value from a node.
305305 *
306306 * This macro will ensure the enumerator value is properly casted from the
307- * corresponding integer stored in the pack.
307+ * corresponding integer stored in the pack, and validate that it falls within
308+ * the valid range [first, max).
308309 *
309310 * @param node Node to read from.
310311 * @param key Key of the node to read. Can't be NULL.
311312 * @param value Pointer to the enumerator value to read into.
313+ * @param first First valid enumerator value (inclusive).
314+ * @param max Maximum enumerator value (exclusive).
312315 * @return 0 on success, or a negative error value on failure.
313316 */
314- #define bf_rpack_kv_enum (node , key , value ) \
317+ #define bf_rpack_kv_enum (node , key , value , first , max ) \
315318 ({ \
316319 int __value; \
317320 int __r = bf_rpack_kv_int(node, key, &__value); \
318- if (!__r) \
319- *(value) = __value; \
321+ if (!__r) { \
322+ if (__value < (first) || __value >= (max)) { \
323+ bf_err("invalid %s value %d (expected [%d, %d))", key, \
324+ __value, (int)(first), (int)(max)); \
325+ __r = -EINVAL; \
326+ } else { \
327+ *(value) = __value; \
328+ } \
329+ } \
320330 __r; \
321331 })
322332
@@ -327,14 +337,23 @@ typedef union
327337 *
328338 * @param node Node to read from.
329339 * @param value Pointer to the enumerator value to read into.
340+ * @param first First valid enumerator value (inclusive).
341+ * @param max Maximum enumerator value (exclusive).
330342 * @return 0 on success, or a negative error value on failure.
331343 */
332- #define bf_rpack_enum (node , value ) \
344+ #define bf_rpack_enum (node , value , first , max ) \
333345 ({ \
334346 int __value; \
335347 int __r = bf_rpack_int(node, &__value); \
336- if (!__r) \
337- *(value) = __value; \
348+ if (!__r) { \
349+ if (__value < (first) || __value >= (max)) { \
350+ bf_err("invalid enum value %d (expected [%d, %d))", __value, \
351+ (int)(first), (int)(max)); \
352+ __r = -EINVAL; \
353+ } else { \
354+ *(value) = __value; \
355+ } \
356+ } \
338357 __r; \
339358 })
340359
0 commit comments