diff --git a/subsys/net/lib/mqtt/mqtt_decoder.c b/subsys/net/lib/mqtt/mqtt_decoder.c index ee86d44e184..678546b2f11 100644 --- a/subsys/net/lib/mqtt/mqtt_decoder.c +++ b/subsys/net/lib/mqtt/mqtt_decoder.c @@ -29,13 +29,17 @@ LOG_MODULE_REGISTER(net_mqtt_dec, CONFIG_MQTT_LOG_LEVEL); */ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val) { - NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end); + uint8_t *cur = buf->cur; + uint8_t *end = buf->end; + + NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end); - if ((buf->end - buf->cur) < sizeof(uint8_t)) { + if ((end - cur) < sizeof(uint8_t)) { return -EINVAL; } - *val = *(buf->cur++); + *val = cur[0]; + buf->cur = (cur + sizeof(uint8_t)); NET_DBG("<< val:%02x", *val); @@ -55,14 +59,17 @@ static int unpack_uint8(struct buf_ctx *buf, uint8_t *val) */ static int unpack_uint16(struct buf_ctx *buf, uint16_t *val) { - NET_DBG(">> cur:%p, end:%p", (void *)buf->cur, (void *)buf->end); + uint8_t *cur = buf->cur; + uint8_t *end = buf->end; + + NET_DBG(">> cur:%p, end:%p", (void *)cur, (void *)end); - if ((buf->end - buf->cur) < sizeof(uint16_t)) { + if ((end - cur) < sizeof(uint16_t)) { return -EINVAL; } - *val = *(buf->cur++) << 8; /* MSB */ - *val |= *(buf->cur++); /* LSB */ + *val = sys_get_be16(cur); + buf->cur = (cur + sizeof(uint16_t)); NET_DBG("<< val:%04x", *val);