Skip to content

Commit 1f15479

Browse files
authored
Merge pull request #4743 from sszllzss/at_client-rt_realloc_iss
[components][net][at]修改at_obj_set_urc_table函数重新分配内存可能导致内存泄漏问题
2 parents 8663e87 + bf88963 commit 1f15479

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

components/net/at/src/at_client.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2018-04-12 chenyong add client implement
1010
* 2018-08-17 chenyong multiple client support
1111
* 2021-03-17 Meco Man fix a buf of leaking memory
12+
* 2021-07-14 Sszl fix a buf of leaking memory
1213
*/
1314

1415
#include <at.h>
@@ -571,30 +572,19 @@ int at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_
571572
}
572573
else
573574
{
574-
struct at_urc_table *old_urc_table = RT_NULL;
575-
size_t old_table_size = client->urc_table_size * sizeof(struct at_urc_table);
576-
577-
old_urc_table = (struct at_urc_table *) rt_malloc(old_table_size);
578-
if (old_urc_table == RT_NULL)
579-
{
580-
return -RT_ENOMEM;
581-
}
582-
rt_memcpy(old_urc_table, client->urc_table, old_table_size);
575+
struct at_urc_table *new_urc_table = RT_NULL;
583576

584577
/* realloc urc table space */
585-
client->urc_table = (struct at_urc_table *) rt_realloc(client->urc_table,
586-
old_table_size + sizeof(struct at_urc_table));
587-
if (client->urc_table == RT_NULL)
578+
new_urc_table = (struct at_urc_table *) rt_realloc(client->urc_table,client->urc_table_size * sizeof(struct at_urc_table) + sizeof(struct at_urc_table));
579+
if (new_urc_table == RT_NULL)
588580
{
589-
rt_free(old_urc_table);
590581
return -RT_ENOMEM;
591582
}
592-
rt_memcpy(client->urc_table, old_urc_table, old_table_size);
583+
client->urc_table = new_urc_table;
593584
client->urc_table[client->urc_table_size].urc = urc_table;
594585
client->urc_table[client->urc_table_size].urc_size = table_sz;
595586
client->urc_table_size++;
596587

597-
rt_free(old_urc_table);
598588
}
599589

600590
return RT_EOK;

0 commit comments

Comments
 (0)