Skip to content

Commit 548521d

Browse files
committed
Fix F-1604 MqttEncode_Props check max props
1 parent 2e26ac2 commit 548521d

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/mqtt_packet.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,17 @@ int MqttEncode_Data(byte *buf, const byte *data, word16 data_len)
391391
int MqttEncode_Props(MqttPacketType packet, MqttProp* props, byte* buf)
392392
{
393393
int rc = 0, tmp;
394+
int prop_count = 0;
394395
MqttProp* cur_prop = props;
395396

396-
/* TODO: Check against max size. Sometimes all properties are not
397-
expected to be added */
398-
399397
/* loop through the list properties */
400398
while ((cur_prop != NULL) && (rc >= 0))
401399
{
400+
/* Guard against a corrupted or circular property list */
401+
if (++prop_count > MQTT_MAX_PROPS) {
402+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY);
403+
break;
404+
}
402405
if (cur_prop->type >= sizeof(gPropMatrix) / sizeof(gPropMatrix[0])) {
403406
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_PROPERTY);
404407
break;
@@ -511,6 +514,12 @@ int MqttEncode_Props(MqttPacketType packet, MqttProp* props, byte* buf)
511514
}
512515
}
513516

517+
/* Check cumulative size against MQTT v5 max remaining length */
518+
if (rc > (int)MQTT_PACKET_MAX_REMAIN_LEN) {
519+
rc = MQTT_TRACE_ERROR(MQTT_CODE_ERROR_OUT_OF_BUFFER);
520+
break;
521+
}
522+
514523
cur_prop = cur_prop->next;
515524
}
516525

0 commit comments

Comments
 (0)