From 0afe7ce669459637b2820cfa1cd1a43e9b917423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=8F=A1=E7=8C=AB=E7=8C=AB?= <164346864@qq.com> Date: Thu, 27 Apr 2023 16:46:29 +0800 Subject: [PATCH] Update easy_pool_cleanup_new (easy_pool.c) fixed bug that alloc memory of incorrect size and add test --- deps/easy/src/util/easy_pool.c | 2 +- deps/easy/test/util/easy_pool_test.c | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/deps/easy/src/util/easy_pool.c b/deps/easy/src/util/easy_pool.c index f383810f7..420e67667 100644 --- a/deps/easy/src/util/easy_pool.c +++ b/deps/easy/src/util/easy_pool.c @@ -243,7 +243,7 @@ char *easy_pool_strdup(easy_pool_t *pool, const char *str) easy_pool_cleanup_t *easy_pool_cleanup_new(easy_pool_t *pool, const void *data, easy_pool_cleanup_pt *handler) { easy_pool_cleanup_t *cl; - cl = easy_pool_alloc(pool, sizeof(easy_pool_t)); + cl = easy_pool_alloc(pool, sizeof(easy_pool_cleanup_t)); if (cl) { cl->handler = handler; diff --git a/deps/easy/test/util/easy_pool_test.c b/deps/easy/test/util/easy_pool_test.c index 03be414ad..8ad68e6be 100644 --- a/deps/easy/test/util/easy_pool_test.c +++ b/deps/easy/test/util/easy_pool_test.c @@ -44,6 +44,13 @@ static void *test_realloc_2 (void *ptr, size_t size) return NULL; } +static void test_cleanup(const void *data) +{ + int *p; + + p = (int *) data; + *p += 1; +} TEST(easy_pool, create) { @@ -281,3 +288,28 @@ TEST(easy_pool, strdup) easy_pool_destroy(pool); } +TEST(easy_pool, cleanup) +{ + easy_pool_t *pool; + easy_pool_cleanup_t *cl; + int i, cnt, size; + + i = 0; + cnt = 0; + size = 111; + cl = NULL; + pool = easy_pool_create(0); + + for (i = 0; i < size; ++ i) + { + cl = easy_pool_cleanup_new(pool, &cnt, test_cleanup); + EXPECT_TRUE(cl != NULL); + + easy_pool_cleanup_reg(pool, cl); + } + + cl = NULL; + easy_pool_destroy(pool); + + EXPECT_TRUE(cnt == size); +}