net: lwm2m: Ensure string termination
When writing string data to resources which are string types, we should count in the terminating character into the data length. Corner cases exist where LwM2M resource type is opaque but lwm2m_get_string() or lwm2m_set_string() are used to read/write the data. We must ensure string termination on those case, but terminating character must not be stored in the engine buffer or counted in the data length as this might be considered as part of the binary data. Fixes #59196 Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
parent
8b9815aff6
commit
95cef5f3ab
3 changed files with 99 additions and 19 deletions
|
@ -294,3 +294,60 @@ ZTEST(lwm2m_registry, test_callbacks)
|
|||
zassert_equal(ret, 0);
|
||||
zassert_equal(callback_checker, 0x7F);
|
||||
}
|
||||
|
||||
ZTEST(lwm2m_registry, test_strings)
|
||||
{
|
||||
int ret;
|
||||
char buf[256] = {0};
|
||||
struct lwm2m_obj_path path = LWM2M_OBJ(0, 0, 0);
|
||||
static const char uri[] = "coap://127.0.0.1";
|
||||
uint16_t len;
|
||||
uint8_t *p;
|
||||
|
||||
ret = lwm2m_get_res_buf(&path, (void **)&p, &len, NULL, NULL);
|
||||
zassert_equal(ret, 0);
|
||||
memset(p, 0xff, len); /* Pre-fill buffer to check */
|
||||
|
||||
/* Handle strings in string resources */
|
||||
ret = lwm2m_set_string(&path, uri);
|
||||
zassert_equal(ret, 0);
|
||||
ret = lwm2m_get_res_buf(&path, (void **)&p, NULL, &len, NULL);
|
||||
zassert_equal(ret, 0);
|
||||
zassert_equal(len, sizeof(uri));
|
||||
zassert_equal(p[len - 1], '\0'); /* string terminator in buffer */
|
||||
zassert_equal(p[len], 0xff);
|
||||
|
||||
ret = lwm2m_get_string(&path, buf, sizeof(buf));
|
||||
zassert_equal(ret, 0);
|
||||
zassert_equal(memcmp(uri, buf, sizeof(uri)), 0);
|
||||
ret = lwm2m_get_string(&path, buf, sizeof(uri));
|
||||
zassert_equal(ret, 0);
|
||||
ret = lwm2m_get_string(&path, buf, strlen(uri));
|
||||
zassert_equal(ret, -ENOMEM);
|
||||
|
||||
/* Handle strings in opaque resources (no terminator) */
|
||||
path = LWM2M_OBJ(0, 0, 3);
|
||||
ret = lwm2m_get_res_buf(&path, (void **)&p, &len, NULL, NULL);
|
||||
zassert_equal(ret, 0);
|
||||
memset(p, 0xff, len); /* Pre-fill buffer to check */
|
||||
|
||||
ret = lwm2m_set_string(&path, uri);
|
||||
zassert_equal(ret, 0);
|
||||
ret = lwm2m_get_res_buf(&path, (void **)&p, NULL, &len, NULL);
|
||||
zassert_equal(ret, 0);
|
||||
zassert_equal(len, strlen(uri)); /* No terminator counted in data length */
|
||||
zassert_equal(p[len - 1], '1'); /* Last character in buffer is not terminator */
|
||||
zassert_equal(p[len], 0xff);
|
||||
memset(buf, 0xff, sizeof(buf));
|
||||
ret = lwm2m_get_string(&path, buf, sizeof(buf)); /* get_string ensures termination */
|
||||
zassert_equal(ret, 0);
|
||||
zassert_equal(memcmp(uri, buf, sizeof(uri)), 0);
|
||||
ret = lwm2m_get_string(&path, buf, sizeof(uri));
|
||||
zassert_equal(ret, 0);
|
||||
ret = lwm2m_get_string(&path, buf, strlen(uri));
|
||||
zassert_equal(ret, -ENOMEM);
|
||||
/* Corner case: we request exactly as much is stored in opaque resource, */
|
||||
/* but because we request as a string, it must have room for terminator. */
|
||||
ret = lwm2m_get_string(&path, buf, len);
|
||||
zassert_equal(ret, -ENOMEM);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue