mqtt: parse properties in MQTT 5.0 CONNACK packets

Under MQTT 5.0, decode the property length and set props_start and
props_size for MQTT_CMD_CONNACK. Add unit test coverage.
This commit is contained in:
jnnrgd
2026-09-30 10:51:41 +02:00
parent 8e0c765c2c
commit 1f2b14bb66
3 changed files with 27 additions and 0 deletions
+10
View File
@@ -7779,6 +7779,16 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
case MQTT_CMD_CONNACK:
if (end - p < 2) return MQTT_MALFORMED;
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
if (p > end) return MQTT_MALFORMED;
}
break;
case MQTT_CMD_PUBACK:
case MQTT_CMD_PUBREC:
+10
View File
@@ -438,6 +438,16 @@ int mg_mqtt_parse(const uint8_t *buf, size_t len, uint8_t version,
case MQTT_CMD_CONNACK:
if (end - p < 2) return MQTT_MALFORMED;
m->ack = p[1];
p += 2;
if (version == 5) {
if (p >= end) return MQTT_MALFORMED;
len_len = (uint32_t) decode_varint(p, (size_t) (end - p), &tmp);
if (!len_len) return MQTT_MALFORMED;
m->props_size = (size_t) tmp;
m->props_start = (size_t) (p + len_len - buf);
p += len_len + m->props_size;
if (p > end) return MQTT_MALFORMED;
}
break;
case MQTT_CMD_PUBACK:
case MQTT_CMD_PUBREC:
+7
View File
@@ -803,11 +803,18 @@ static void test_mqtt_parse(void) {
struct mg_mqtt_prop prop;
// MQTT5 PUBLISH with Subscription Identifier = 5
const uint8_t sub_id_pkt[] = {0x30, 0x06, 0x00, 0x01, 'x', 0x02, 0x0B, 0x05};
// MQTT5 CONNACK with Server Keep Alive = 60
const uint8_t connack_pkt[] = {0x20, 0x06, 0x00, 0x00, 0x03, 0x13, 0x00, 0x3C};
memset(&mm, 0, sizeof(struct mg_mqtt_message));
memset(&prop, 0, sizeof(struct mg_mqtt_prop));
ASSERT(mg_mqtt_parse(sub_id_pkt, sizeof(sub_id_pkt), 5, &mm) == MQTT_OK);
mg_mqtt_next_prop(&mm, &prop, 0);
ASSERT(mg_mqtt_parse(connack_pkt, sizeof(connack_pkt), 5, &mm) == MQTT_OK);
ASSERT(mm.ack == 0);
ASSERT(mg_mqtt_next_prop(&mm, &prop, 0) > 0);
ASSERT(prop.id == MQTT_PROP_SERVER_KEEP_ALIVE && prop.iv == 60);
}
static void test_mqtt(void) {