From b474e0a8c39a8ca18cc0e620692090a5a0017e59 Mon Sep 17 00:00:00 2001 From: Robert Lubos Date: Fri, 20 Aug 2021 15:28:23 +0200 Subject: [PATCH] net: lwm2m: Fix unsigned integers ecoding in TLV As the resource values represented by the unsigned integers were casted to integers of a corresponding size in the read handler, they were not ecoded properly if the unsigned value was larger than the maximum integer value of the corresponding size (i.e. they were encoded as negative values). Fix this by casting the unsinged value to a wider integer type, to prevent incorrect interpratetion of the data provided. The TLV encoding functions take care of the optimization (i. e. encoding integers on the minimum number of bytes needed), so it should prevent bandwith waste if the unsigned value would actually fit into the integer of the corresponding size. Similar case is for the write hander, where unsigned integers encoded at 8 bytes were not processed correctly. Fix this by using wider decoder as well. Signed-off-by: Robert Lubos --- subsys/net/lib/lwm2m/lwm2m_engine.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index ae02836ea8b..3e899948f97 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -2365,18 +2365,18 @@ static int lwm2m_read_handler(struct lwm2m_engine_obj_inst *obj_inst, case LWM2M_RES_TYPE_U32: case LWM2M_RES_TYPE_TIME: - engine_put_s32(&msg->out, &msg->path, - (int32_t)*(uint32_t *)data_ptr); + engine_put_s64(&msg->out, &msg->path, + (int64_t)*(uint32_t *)data_ptr); break; case LWM2M_RES_TYPE_U16: - engine_put_s16(&msg->out, &msg->path, - (int16_t)*(uint16_t *)data_ptr); + engine_put_s32(&msg->out, &msg->path, + (int32_t)*(uint16_t *)data_ptr); break; case LWM2M_RES_TYPE_U8: - engine_put_s8(&msg->out, &msg->path, - (int8_t)*(uint8_t *)data_ptr); + engine_put_s16(&msg->out, &msg->path, + (int16_t)*(uint8_t *)data_ptr); break; case LWM2M_RES_TYPE_S64: @@ -2640,8 +2640,8 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst, case LWM2M_RES_TYPE_U32: case LWM2M_RES_TYPE_TIME: - engine_get_s32(&msg->in, &temp32); - *(uint32_t *)write_buf = temp32; + engine_get_s64(&msg->in, &temp64); + *(uint32_t *)write_buf = temp64; len = 4; break;