-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
logging: rpc: fix overwriting of buffer's header #20032
base: main
Are you sure you want to change the base?
Conversation
CI InformationTo view the history of this post, clich the 'edited' button above Inputs:Sources:sdk-nrf: PR head: b571a6ea9ecf4d0c0ca54dacadb51c657c4e3825 more detailssdk-nrf:
Github labels
List of changed files detected by CI (1)
Outputs:ToolchainVersion: 342151af73 Test Spec & Results: ✅ Success; ❌ Failure; 🟠 Queued; 🟡 Progress; ◻️ Skipped;
|
This commit fixes an issue where a header of a MPSC buffer was overwritten by log's header causing a log be falsely claimed. Signed-off-by: Konrad Derda <[email protected]>
7686057
to
b571a6e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, a few readability/maintainability suggestions but LGTM otherwise
@@ -14,6 +14,40 @@ | |||
static uint32_t __aligned(Z_LOG_MSG_ALIGNMENT) log_history_raw[HISTORY_WLEN]; | |||
static struct mpsc_pbuf_buffer log_history_pbuf; | |||
|
|||
static bool copy_to_pbuffer(const union log_msg_generic *msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to extract this to a separate function if it's used in only one function that does only this? This way it's harder to figure out that the actual change is copying everything without the first two bits :)
uint8_t *src_data; | ||
size_t hdr_wlen; | ||
|
||
wlen = log_msg_generic_get_wlen((union mpsc_pbuf_generic *)msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wlen = log_msg_generic_get_wlen((union mpsc_pbuf_generic *)msg); | |
wlen = log_msg_generic_get_wlen(&msg->buf); |
To avoid unsafe casts if not needed.
hdr_wlen = DIV_ROUND_UP(sizeof(struct mpsc_pbuf_hdr), sizeof(uint32_t)); | ||
|
||
if (wlen <= hdr_wlen) { | ||
return false; | ||
} | ||
|
||
dst->hdr.data = msg->buf.hdr.data; | ||
memcpy(dst_data, src_data, (wlen - hdr_wlen) * sizeof(uint32_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the rounding up was actually needed, this code would be incorrect as it would copy too few bytes. Suggest a bit more assumption-less code:
hdr_wlen = DIV_ROUND_UP(sizeof(struct mpsc_pbuf_hdr), sizeof(uint32_t)); | |
if (wlen <= hdr_wlen) { | |
return false; | |
} | |
dst->hdr.data = msg->buf.hdr.data; | |
memcpy(dst_data, src_data, (wlen - hdr_wlen) * sizeof(uint32_t)); | |
if (wlen * sizeof(uint32_t) <= sizeof(struct mpsc_pbuf_hdr)) { | |
return false; | |
} | |
dst->hdr.data = msg->buf.hdr.data; | |
memcpy(dst_data, src_data, wlen * sizeof(uint32_t) - sizeof(struct mpsc_pbuf_hdr)); |
This commit fixes an issue where a header of a MPSC buffer was overwritten by log's header causing a log be falsely claimed.