Skip to content

Conversation

ljd42
Copy link
Contributor

@ljd42 ljd42 commented Sep 26, 2025

Add a note about a limitation when using deferred logging + thread name together with dynamically allocated struct k_thread. This limitation has been raised in issue #95077.

dcpleung
dcpleung previously approved these changes Sep 29, 2025
Add a note about a limitation when using deferred logging + thread name
together with dynamically allocated struct k_thread. This limitation has
been raised in issue zephyrproject-rtos#95077.

Signed-off-by: Loic Domaigne <[email protected]>
@ljd42 ljd42 force-pushed the docfix-95077-logging-thread_name branch from 2fba872 to 30b5cd6 Compare October 8, 2025 21:02
@ljd42 ljd42 requested review from JarmouniA and dcpleung October 8, 2025 21:02
Copy link

sonarqubecloud bot commented Oct 8, 2025

@ljd42
Copy link
Contributor Author

ljd42 commented Oct 13, 2025

Hi @dcpleung, @nordic-krch :
could you please review this PR? Daniel had already approved it, but it needed another round of review as Abderahmane had some review comments. Thanks!

@JarmouniA JarmouniA requested a review from kartben October 13, 2025 19:01
:c:func:`malloc` for instance. In this case, if the thread logs some messages and then gets
stopped and its ``struct k_thread`` is freed, the log system will still try to access that
structure when handling the message later. This creates a use-after-free scenario.
To avoid this, a solution consists of calling :c:func:`log_flush` before freeing the structure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
To avoid this, a solution consists of calling :c:func:`log_flush` before freeing the structure.
To avoid this, a solution consists in calling :c:func:`log_flush` before freeing the structure.

struct k_thread *thread = malloc(sizeof(*thread)); /* struct allocated dynamically */
k_thread_create(thread, ...);
k_thread_name_set(thread, "foobar2025");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
k_thread_name_set(thread, "foobar2025");
k_thread_name_set(thread, "foobar2025");

Nit but you probably shouldn't use a year here since this will have the code snippet "age" for no reason. "foo bar" should be just fine :)

k_thread_create(thread, ...);
k_thread_name_set(thread, "foobar2025");
// Thread calls LOG_*(...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be C style too


.. code-block:: c
struct k_thread *thread = malloc(sizeof(*thread)); /* struct allocated dynamically */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be k_malloc (resp. k_free), not everyone necessarily have malloc/free available

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.

6 participants