Skip to content

Conversation

rlubos
Copy link
Contributor

@rlubos rlubos commented Oct 10, 2025

The library used to store the path/options pointer within the request context and used them in case of retransmissions/block transfer/cancellation. This however didn't work well if the pointer provided were pointing to for example memory on a stack, that could've been overwritten.

Therefore, rework how the data is passed to the library to make it more robust. Instead of using pointer, allocate buffers within request context for path/options so that the data remains valid throughout the exchange lifetime.

Additionally, rework the retransmission logic, so that the request is not built from scratch, but instead the originally created message is reused. This also solves the transient pointer issue for payload in simple cases. For block transfer, the payload pointer needs to remain valid throughout the exchange, which has been documented.

Fixes #97115

Use buffers instead of pointers for path and extra options provided in
the struct coap_client_request, so that the library keeps a copy of the
path/options instead of pointers. That way, the library can still work
correctly in case of retransmissions or block transfer in case when the
original path or options were automatic variables that went out of
context.

Signed-off-by: Robert Lubos <[email protected]>
Move the definition from the test suite to a public header.

Signed-off-by: Robert Lubos <[email protected]>
Keep a separate buffer for each request context instead of having a
single common buffer for the entire client context. This allows to
simplify the retransmission logic, as parallel requests will no longer
overwrite the buffer for each other. That way, we can simply retransmit
the packet w/o a need to recreate it, and thus reach to the payload
pointer or callback.

This removes the requirement to keep the payload pointer, provided to
asynchronous coap_client_req() call, valid throughout the exchange
lifetime for simple cases (i.e. no block transfer used). In case of
block transfer, this is unavoidable and needs to be documented.

Signed-off-by: Robert Lubos <[email protected]>
@zephyrbot zephyrbot added area: Networking Release Notes To be mentioned in the release notes area: Sockets Networking sockets area: Samples Samples area: Tests Issues related to a particular existing or missing test labels Oct 10, 2025
@rlubos rlubos requested a review from SeppoTakalo October 10, 2025 08:18
pdgendt
pdgendt previously approved these changes Oct 10, 2025
Copy link
Contributor

@pdgendt pdgendt left a comment

Choose a reason for hiding this comment

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

Nice, thanks!

@rlubos rlubos force-pushed the net/coap-client-request-pointers branch from 1e04c20 to 19c622b Compare October 10, 2025 08:47
@rlubos rlubos force-pushed the net/coap-client-request-pointers branch from 19c622b to 83395d8 Compare October 10, 2025 09:05
Cover struct coap_client_request changes in the migration guide.

Signed-off-by: Robert Lubos <[email protected]>
@rlubos rlubos force-pushed the net/coap-client-request-pointers branch from 83395d8 to a4ecb40 Compare October 10, 2025 09:11
Copy link

config COAP_CLIENT_MAX_PATH_LENGTH
int "Maximum length of the path string"
default 64
range 1 128
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we set the range here?
This might end up limiting some use-cases where lots of parameters are supplied in path.

I would leave the range out, and just build-time assert that it is more than zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only reason I had was that COAP_CLIENT_MESSAGE_HEADER_SIZE config already had a range on it (

range 24 128
), so we won't be able to encode larger paths anyway (more or less of course due to option encoding).

@SeppoTakalo
Copy link
Contributor

@juhaylinen Do you know any nRF Cloud path lengths? Is this going to kick us in the next upmerge?

@jhedberg jhedberg merged commit 140f193 into zephyrproject-rtos:main Oct 14, 2025
27 checks passed
@juhaylinen
Copy link
Contributor

@juhaylinen Do you know any nRF Cloud path lengths? Is this going to kick us in the next upmerge?

Maximum path length for Device provisioning client seems to be around 88.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Networking area: Samples Samples area: Sockets Networking sockets area: Tests Issues related to a particular existing or missing test Release Notes To be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

net: lib: coap: client: Invalid path pointer

7 participants