Skip to content

logging: transient strings are no longer duplicated correctly #44996

@attie-argentum

Description

@attie-argentum

Describe the bug

Since #43520 was merged (specifically the changes to include/zephyr/logging/log_msg2.h, ping @nordic-krch), logged strings that are not in read-only memory are no longer correctly duplicated and preserved for deferred output... in some cases... (🤯)

If the following conditions are met, you will expose the issue: (there may be others)

  • The string variable is declared uint8_t * or uint8_t []
  • One or more calls to LOG_*(), where it needs to duplicate more than one string between log flushes

This patch to samples/subsys/logging/logger will demonstrate the issue: 0001-demo-string-handling-bug.zip
I have removed the call to log_strdup() as A) it should be unnecessary, B) it's return type is char *, so you'll need to introduce a cast to see the issue, which isn't as clear.

To prevent the issue from occurring, you can take one of the following actions:

This is demonstrable on a SAMR34 XPro that I've recently added support for (see #41308), and the qemu_cortex_m0 target... though I expect others would exhibit the issue too - possibly restricted to ARM / Cortex-M0, but I doubt it.

Expected behavior

Log output should appear as follows:

String logging showcase.
demo_tag [00:00:01.356,077] <inf> main: Logging transient string:transient_string
String logging showcase #2.
demo_tag [00:00:01.356,178] <inf> main: Logging transient string:transient_string2

Log output actually shows:

String logging showcase.
demo_tag [00:00:01.356,078] <inf> main: Logging transient string:transient_string2
String logging showcase #2.
demo_tag [00:00:01.356,162] <inf> main: Logging transient string:transient_string2

Swapping the log_strdup_showcase2() call for a second log_strdup_showcase() results in the following (no text shown here, can be garbage).

String logging showcase.
demo_tag [00:00:01.356,078] <inf> main: Logging transient string:
String logging showcase.
demo_tag [00:00:01.356,160] <inf> main: Logging transient string:

Impact
I've spent quite a bit of time tracking this down... I going forwards I can make sure to use char *, but I suspect others will bump into this before long.

Environment (please complete the following information):

Metadata

Metadata

Assignees

Labels

area: LoggingbugThe issue is a bug, or the PR is fixing a bugpriority: lowLow impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions