Skip to content

Conversation

@tpambor
Copy link
Contributor

@tpambor tpambor commented Jun 24, 2025

cbprintf_package_convert may invoke z_cbprintf_cpy with a null pointer to buf and zero length to indicate a flush operation.

/* Empty call (can be interpreted as flushing) */
(void)cb(NULL, 0, ctx);

This triggers an error from the undefined behavior sanitizer due to memcpy being called with a null src:

zephyr/include/zephyr/sys/cbprintf.h:536:45: runtime error: null pointer passed as argument 2, which is declared to never be null
/usr/include/string.h:44:28: note: nonnull attribute specified here
    #0 0x8186fa9 in z_cbprintf_cpy zephyr/include/zephyr/sys/cbprintf.h:536:2
    #1 0x816bd79 in cbprintf_package_convert zephyr/lib/os/cbprintf_packaged.c:1167:8
    #2 0x81865c7 in cbprintf_package_copy zephyr/include/zephyr/sys/cbprintf.h:586:9
    #3 0x81865c7 in z_impl_z_log_msg_static_create zephyr/subsys/logging/log_msg.c:323:10
<snip>

SUMMARY: UndefinedBehaviorSanitizer: invalid-null-argument zephyr/include/zephyr/sys/cbprintf.h:536:45

Passing a null pointer as source to memcpy is undefined behavior according to the C standard.

The behavior is undefined if either dest or src is an invalid or null pointer.

This PR adds a fix by exiting early in z_cbprintf_cpy when length is zero.

cbprintf_package_convert may invoke z_cbprintf_cpy with a null pointer
to buf and zero length to indicate a flush operation. This triggers
an error from the undefined behavior sanitizer due to memcpy being called
with a null src, which is undefined behavior according to the C standard.

This is avoided by exiting early in z_cbprintf_cpy when length is zero.

Signed-off-by: Tim Pambor <tim.pambor@codewrights.de>
@tpambor tpambor force-pushed the fix-z_cbprintf_cpy branch from edbc949 to 3dec396 Compare June 24, 2025 13:42
@sonarqubecloud
Copy link

@danieldegrasse danieldegrasse merged commit 542ea03 into zephyrproject-rtos:main Jun 26, 2025
26 checks passed
@ghost ghost mentioned this pull request Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants