Skip to content

Commit 60a9b53

Browse files
committed
fix(mqtt): fix memory leak in sub and thingmodel
1 parent 06cc4f5 commit 60a9b53

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/iot/iot_mqtt.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static int callback_mqtt(struct lws *wsi, enum lws_callback_reasons reason,
154154
(ctx->sub_topic_count + 1) * sizeof(iot_mqtt_topic_map_t));
155155
if (new_sub_topic_maps == NULL) {
156156
// realloc 失败,保留原有 sub_topic_maps
157-
lwsl_err("%s: realloc sub_topic_maps failed\n", __func__);
157+
lwsl_err("%s: realloc sub_topic_maps failed\\n", __func__);
158158
// 释放当前 topic_map 的 topic 内存,因为它无法被添加
159159
free((void*)topic_map->topic);
160160
topic_map->topic = NULL;
@@ -563,19 +563,34 @@ static void _iot_mqtt_append_sub_topic(iot_mqtt_ctx_t *ctx, iot_mqtt_topic_map_t
563563
// 将topic_map存入待订阅列表
564564
if (ctx->pending_sub_list == NULL) {
565565
ctx->pending_sub_list = (iot_mqtt_pending_sub_list_t *)malloc(sizeof(iot_mqtt_pending_sub_list_t));
566+
if (ctx->pending_sub_list == NULL) {
567+
lws_pthread_mutex_unlock(&ctx->sub_topic_mutex);
568+
return;
569+
}
566570
ctx->pending_sub_list->topic_maps = NULL;
567571
ctx->pending_sub_list->count = 0;
568572
}
569573
// copy topic_map
570-
iot_mqtt_topic_map_t *topic_map_copy = (iot_mqtt_topic_map_t *)malloc(sizeof(iot_mqtt_topic_map_t));
571-
memset(topic_map_copy, 0, sizeof(iot_mqtt_topic_map_t));
572-
topic_map_copy->topic = strdup(topic_map->topic);
573-
topic_map_copy->message_callback = topic_map->message_callback;
574-
topic_map_copy->event_callback = topic_map->event_callback;
575-
topic_map_copy->user_data = topic_map->user_data;
576-
topic_map_copy->qos = topic_map->qos;
577-
ctx->pending_sub_list->topic_maps = (iot_mqtt_topic_map_t *)realloc(ctx->pending_sub_list->topic_maps, (ctx->pending_sub_list->count + 1) * sizeof(iot_mqtt_topic_map_t));
578-
ctx->pending_sub_list->topic_maps[ctx->pending_sub_list->count] = *topic_map_copy;
574+
iot_mqtt_topic_map_t topic_map_copy;
575+
memset(&topic_map_copy, 0, sizeof(iot_mqtt_topic_map_t));
576+
topic_map_copy.topic = strdup(topic_map->topic);
577+
if (topic_map_copy.topic == NULL) {
578+
lws_pthread_mutex_unlock(&ctx->sub_topic_mutex);
579+
return;
580+
}
581+
topic_map_copy.message_callback = topic_map->message_callback;
582+
topic_map_copy.event_callback = topic_map->event_callback;
583+
topic_map_copy.user_data = topic_map->user_data;
584+
topic_map_copy.qos = topic_map->qos;
585+
586+
iot_mqtt_topic_map_t *new_maps = (iot_mqtt_topic_map_t *)realloc(ctx->pending_sub_list->topic_maps, (ctx->pending_sub_list->count + 1) * sizeof(iot_mqtt_topic_map_t));
587+
if (new_maps == NULL) {
588+
free((void*)topic_map_copy.topic);
589+
lws_pthread_mutex_unlock(&ctx->sub_topic_mutex);
590+
return;
591+
}
592+
ctx->pending_sub_list->topic_maps = new_maps;
593+
ctx->pending_sub_list->topic_maps[ctx->pending_sub_list->count] = topic_map_copy;
579594
ctx->pending_sub_list->count++;
580595
lws_pthread_mutex_unlock(&ctx->sub_topic_mutex);
581596
}

src/iot/thingmodel/custom_topic.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ int32_t tm_sub_custom_topic(void *handler, const char *topic_suffix) {
113113
aws_mem_release(dm_handle->allocator, topic);
114114
aws_string_destroy_secure(product_key);
115115
aws_string_destroy_secure(device_name);
116+
aws_string_destroy_secure(topic_suffix_string);
116117

117118
return 0;
118119
}

src/iot/thingmodel/iot_tm_api.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ int __s_tm_set_up_mqtt_topic(iot_tm_handler_t *iot_tm_handler, struct aws_string
342342
topic_mapping.message_callback = g_dm_recv_topic_mapping[i].func;
343343
topic_mapping.user_data = iot_tm_handler;
344344
if (topic_mapping.message_callback == NULL) {
345+
aws_mem_release(iot_tm_handler->allocator, (void*)topic_mapping.topic);
345346
continue;
346347
}
347348
topic_mapping.qos = IOT_MQTT_QOS1;

0 commit comments

Comments
 (0)