From 015b1103fb0db699bf8f881092e488126cf483b4 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Mon, 16 Oct 2023 16:15:07 +0300 Subject: [PATCH] net: lwm2m: Properly initialize buffers for resource instance When resource instances are initialized, we must calculate beginning of the data buffer using the index and maximum data length. Otherwise buffers would overlap with previous. Fixes #64011 Signed-off-by: Seppo Takalo --- subsys/net/lib/lwm2m/lwm2m_object.h | 2 +- tests/net/lib/lwm2m/lwm2m_registry/prj.conf | 1 + .../lwm2m/lwm2m_registry/src/lwm2m_registry.c | 36 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_object.h b/subsys/net/lib/lwm2m/lwm2m_object.h index 170a99c023d..26322f50b1b 100644 --- a/subsys/net/lib/lwm2m/lwm2m_object.h +++ b/subsys/net/lib/lwm2m/lwm2m_object.h @@ -251,7 +251,7 @@ struct lwm2m_engine_obj { if (_ri_ptr != NULL && _ri_count > 0) { \ for (int _i = 0; _i < _ri_count; _i++) { \ _ri_ptr[_ri_idx + _i].data_ptr = \ - (_data_ptr + _i); \ + ((uint8_t *) _data_ptr + (_i * _data_sz)); \ _ri_ptr[_ri_idx + _i].max_data_len = \ _data_sz; \ _ri_ptr[_ri_idx + _i].data_len = \ diff --git a/tests/net/lib/lwm2m/lwm2m_registry/prj.conf b/tests/net/lib/lwm2m/lwm2m_registry/prj.conf index 84e3a4bb289..223f97a0762 100644 --- a/tests/net/lib/lwm2m/lwm2m_registry/prj.conf +++ b/tests/net/lib/lwm2m/lwm2m_registry/prj.conf @@ -13,3 +13,4 @@ CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1=y CONFIG_LWM2M_IPSO_TEMP_SENSOR_INSTANCE_COUNT=4 CONFIG_LWM2M_CONN_MON_OBJ_SUPPORT=y CONFIG_LWM2M_CONNMON_OBJECT_VERSION_1_2=y +CONFIG_LWM2M_PORTFOLIO_OBJ_SUPPORT=y diff --git a/tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c b/tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c index d847b5a35cd..d4c0a7757a6 100644 --- a/tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c +++ b/tests/net/lib/lwm2m/lwm2m_registry/src/lwm2m_registry.c @@ -285,6 +285,42 @@ ZTEST(lwm2m_registry, test_resource_instance_creation_and_deletion) zassert_equal(ret, 0); } +ZTEST(lwm2m_registry, test_resource_instance_strings) +{ + int ret; + char buf[256] = {0}; + static const char string_a[] = "Hello"; + static const char string_b[] = "World"; + struct lwm2m_obj_path path_a = LWM2M_OBJ(16, 0, 0, 0); + struct lwm2m_obj_path path_b = LWM2M_OBJ(16, 0, 0, 1); + + ret = lwm2m_create_object_inst(&LWM2M_OBJ(16, 0)); + zassert_equal(ret, 0); + + ret = lwm2m_create_res_inst(&path_a); + zassert_equal(ret, 0); + + ret = lwm2m_create_res_inst(&path_b); + zassert_equal(ret, 0); + + ret = lwm2m_set_string(&path_a, string_a); + zassert_equal(ret, 0); + + ret = lwm2m_set_string(&path_b, string_b); + zassert_equal(ret, 0); + + ret = lwm2m_get_string(&path_a, buf, sizeof(buf)); + zassert_equal(ret, 0); + zassert_equal(0, memcmp(buf, string_a, sizeof(string_a))); + + ret = lwm2m_get_string(&path_b, buf, sizeof(buf)); + zassert_equal(ret, 0); + zassert_equal(0, memcmp(buf, string_b, sizeof(string_b))); + + ret = lwm2m_delete_object_inst(&LWM2M_OBJ(16, 0)); + zassert_equal(ret, 0); +} + ZTEST(lwm2m_registry, test_callbacks) { int ret;