net/unicoap: initialize .payload_representation in message_init helpers#22321
Open
adityachilka1 wants to merge 1 commit into
Open
net/unicoap: initialize .payload_representation in message_init helpers#22321adityachilka1 wants to merge 1 commit into
adityachilka1 wants to merge 1 commit into
Conversation
The four `unicoap_message_init*` inline initializers in `sys/include/net/unicoap/message.h` set `code`, `options`, `payload`, and `payload_size` but never wrote the `.payload_representation : 1` bitfield. The function-level contract documented above each initializer is that callers do NOT need to zero the `unicoap_message_t` struct before calling — but with a stack-allocated, un-zeroed struct, the representation bit ends up holding whatever was on the stack. If that bit happens to be `1` (= UNICOAP_PAYLOAD_NONCONTIGUOUS), subsequent calls to `unicoap_message_payload_get_size`, the chunks accessors, or serialisation enter the noncontiguous codepath and dereference `message->payload_chunks` as an `iolist_t*` — even though the caller populated the contiguous-payload fields. Set `message->payload_representation = UNICOAP_PAYLOAD_CONTIGUOUS;` in each of the four initializers. The sibling `unicoap_message_alloc_*` helpers already get correct behaviour for free via designated initializers (unmentioned fields are value-initialised to zero, and UNICOAP_PAYLOAD_CONTIGUOUS is the zero value of the enum). The fifth `unicoap_message_init_string_with_options` is unchanged on purpose: it delegates to `unicoap_message_init_with_options` and inherits the fix transitively. Fixes RIOT-OS#22285.
|
Hey @adityachilka1, thank you for your first contribution! We really appreciate it! If you haven't already, please take a look at our contributing guidelines before the review process starts. Also, due to how the GitHub review system works, please avoid force-pushing or squashing your commits unless asked to by a maintainer (or unless your commit is still in "draft commit" stage). Your pull request will be reviewed as soon as possible. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contribution description
Fixes #22285.
The four
unicoap_message_init*inline initializers insys/include/net/unicoap/message.hpopulatedcode,options,payload, andpayload_sizebut never wrote the.payload_representation : 1bitfield. The function-level contract above each initializer explicitly tells callers they do not need to zero theunicoap_message_tstruct beforehand — but with a stack-allocated un-zeroed struct, the representation bit ends up holding whatever was on the stack. When that bit happens to be1(UNICOAP_PAYLOAD_NONCONTIGUOUS), subsequent calls tounicoap_message_payload_get_size, the chunks accessors, or any serialisation routine enter the noncontiguous codepath and dereferencemessage->payload_chunksas aniolist_t*— even though the caller populated the contiguous-payload fields.This PR adds
message->payload_representation = UNICOAP_PAYLOAD_CONTIGUOUS;to each of the four initializers (unicoap_message_init_empty,unicoap_message_init,unicoap_message_init_string,unicoap_message_init_with_options).The sibling
unicoap_message_alloc_*helpers already get correct behaviour for free via designated initializers (any field not mentioned is value-initialised to zero, which isUNICOAP_PAYLOAD_CONTIGUOUS).The fifth
unicoap_message_init_string_with_optionsis deliberately unchanged — it delegates tounicoap_message_init_with_optionsinternally, so it inherits the fix transitively.Testing procedure
Before this patch, the following reproducer (taken from the issue body) can segfault depending on what happened to be on the stack:
After the patch,
message.payload_representationis alwaysUNICOAP_PAYLOAD_CONTIGUOUSafter anyunicoap_message_init*call, so the contiguous-payload branch inunicoap_message_payload_get_sizeis always taken. The asserts inunicoap_message_payload_getandunicoap_message_payload_get_chunkskeep the contract honest.You can exercise this in the existing unicoap example app (
examples/networking/coap/unicoap):The change is header-only and additive, so any build that pulls in
net/unicoap/message.hcovers it.Issues/PRs references
Fixes #22285.
Declaration of AI-Tools / LLMs usage
AI-Tools / LLMs that were used are:
UNICOAP_PAYLOAD_CONTIGUOUS) by reading the existing setterunicoap_message_payload_setand the asserts in the contiguous/noncontiguous getters — with user (@adityachilka1) review of every code line before submission ("agent mode" with explicit user review at each step).The C source change itself is mechanical (one identical line added to four already-existing function bodies) and matches the exact pattern used by the
unicoap_message_payload_setsetter elsewhere in the same file. No new logic is introduced.