@@ -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}
0 commit comments