Skip to content

LwM2M string resource is not a zero terminated C-string according to the LwM2M specification #90719

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions include/zephyr/net/lwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -1046,21 +1046,29 @@ int lwm2m_set_bulk(const struct lwm2m_res_item res_list[], size_t res_list_size)
* @param[in] path LwM2M path as a struct
* @param[out] buf Data buffer to copy data into
* @param[in] buflen Length of buffer
* @param[out] reslen On success, the resource length is set. Can be `NULL`
*
* @return 0 for success or negative in case of error.
*/
int lwm2m_get_opaque(const struct lwm2m_obj_path *path, void *buf, uint16_t buflen);
int lwm2m_get_opaque(const struct lwm2m_obj_path *path, void *buf, uint16_t buflen,
uint16_t *reslen);
Comment on lines +1053 to +1054
Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of weird we had function like this, how in the world would the user know how large the actual data content is in the buffer.

But anayway, it's still an API change - LwM2M API is unstable, but it should be covered in the migration guide for 4.3, please add an entry, can be in a separate commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some notes here. The current lwm2m_get_opaque function is absolutely useless because of the missing information about the resource length. There are 2 solution:

  1. resource size in the return value: <0 an error, >= 0 success and resource length
  2. return the resource size in *reslen which results in an API change

I have chosen the second option because there might be a lot of code that check for errors like.
if (ret) { an error } instead of if (ret < 0) { an error }
In case of an API change, the compiler will tell you all locations where you have to fix your code.
However, in case of lwm2m_get_opaque, which had no use case anyway, the 1st option is also an acceptable solution.


/**
* @brief Get resource (instance) value (string)
* @brief Get resource (instance) value (string) as C string with zero termination.
*
* Due to zero-termination, `buf` must be capable to get the resource with additional
* zero-termination. -ENOMEM is returned, if data and zero-termiantion do not fit
* into the buffer.
*
* @param[in] path LwM2M path as a struct
* @param[out] str String buffer to copy data into
* @param[in] buflen Length of buffer
* @param[out] reslen On success, the resource length is set. Can be `NULL`
*
* @return 0 for success or negative in case of error.
*/
int lwm2m_get_string(const struct lwm2m_obj_path *path, void *str, uint16_t buflen);
int lwm2m_get_string(const struct lwm2m_obj_path *path, char *str, uint16_t buflen,
uint16_t *const reslen);

/**
* @brief Get resource (instance) value (u8)
Expand Down
Loading