Skip to content

Commit

Permalink
net: mqtt: improve decoder buffer handling
Browse files Browse the repository at this point in the history
Improve buffer handling logic to use local variables extensively.

This change reduces the number of pointer dereferences, which leads
to more efficient runtime and helps reduce the code size.

(cherry picked from commit 6211de8)

Original-Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
GitOrigin-RevId: 6211de8
Change-Id: If7073516c999c0e7a1af49eca5c7b863d6cb7f6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5753582
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Commit-Queue: Fabio Baltieri <fabiobaltieri@google.com>
Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
Tested-by: Fabio Baltieri <fabiobaltieri@google.com>
  • Loading branch information
ndrs-pst authored and Chromeos LUCI committed Jul 31, 2024
1 parent 9ae1038 commit fa1e9c3
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions subsys/net/lib/mqtt/mqtt_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit fa1e9c3

Please sign in to comment.