diff --git a/include/zephyr/sys/cbprintf_internal.h b/include/zephyr/sys/cbprintf_internal.h index 5334bef193..ce6e8352de 100644 --- a/include/zephyr/sys/cbprintf_internal.h +++ b/include/zephyr/sys/cbprintf_internal.h @@ -382,6 +382,20 @@ do { \ _name##_buf32))) #endif +/* When the first argument of Z_CBPRINTF_STATIC_PACKAGE_GENERIC() is a + * static memory location, some compiler warns you if you compare the + * location against NULL. ___is_null() is used to kill this warning. + * + * The warnings would be visible when you built with -save-temps=obj, + * our standard debugging tip for macro problems. + * + * https://github.com/zephyrproject-rtos/zephyr/issues/51528 + */ +static ALWAYS_INLINE bool ___is_null(void *p) +{ + return p == NULL; +} + /** @brief Statically package a formatted string with arguments. * * @param buf buffer. If null then only length is calculated. @@ -430,7 +444,7 @@ do { \ Z_CBPRINTF_ON_STACK_ALLOC(_ros_pos_buf, _ros_cnt); \ uint8_t *_rws_buffer; \ Z_CBPRINTF_ON_STACK_ALLOC(_rws_buffer, 2 * _rws_cnt); \ - size_t _pmax = (buf != NULL) ? _inlen : INT32_MAX; \ + size_t _pmax = !___is_null(buf) ? _inlen : INT32_MAX; \ int _pkg_len = 0; \ int _total_len = 0; \ int _pkg_offset = _align_offset; \