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 <robert.lubos@nordicsemi.no>
This commit is contained in:
Robert Lubos 2021-08-20 15:28:23 +02:00 committed by Carles Cufí
commit b474e0a8c3

View file

@ -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;