From 460b404fe4d1eb2905d6985bc7de36469a149ed5 Mon Sep 17 00:00:00 2001 From: lixiang_yewu Date: Thu, 17 Aug 2023 06:14:55 +0000 Subject: [PATCH] update tests/test_app/test_app.c. Signed-off-by: lixiang_yewu update tests/test_app/test_app.c. Signed-off-by: lixiang_yewu update tests/test_app/test_app.c. Signed-off-by: lixiang_yewu --- tests/test_app/test_app.c | 48 ++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/tests/test_app/test_app.c b/tests/test_app/test_app.c index 1d77b0f..fbe7457 100644 --- a/tests/test_app/test_app.c +++ b/tests/test_app/test_app.c @@ -30,6 +30,24 @@ __thread int g_thread_count; __thread int g_thread_count2 = 2; __thread const int g_thread_count3 = 3; +// Function to handle dlsym errors +void handle_dlsym_error(const char *error) +{ + if (error != NULL) { + fprintf(stderr, "Error during dlsym: %s\n", error); + exit(EXIT_FAILURE); + } +} + +// Function to handle dlopen errors +void handle_dlopen_error(const char *error) +{ + if (error != NULL) { + fprintf(stderr, "Error during dlopen: %s\n", error); + exit(EXIT_FAILURE); + } +} + void *thread1(void *junk) { (void)junk; @@ -105,22 +123,14 @@ void test_dlsym_from_lib(void) char *error; handle = dlopen("./libutil1.so", RTLD_NOW); - if (!handle) { - fprintf(stderr, "%s\n", dlerror()); - return; - } - dlerror(); - - // lookup function from lib - add_func = dlsym(handle, "lib1_add"); - error = dlerror(); - if (error != NULL) { - fprintf(stderr, "%s\n", error); - return; - } + handle_dlopen_error(dlerror()); + + add_func = dlsym(handle, "lib1_add"); + handle_dlsym_error(dlerror()); - printf("test_dlsym_from_lib: %d\n", add_func(1, 2)); - dlclose(handle); + printf("test_dlsym_from_lib: %d\n", add_func(1, 2)); + + dlclose(handle); } void test_dlsym_any(void) @@ -130,13 +140,9 @@ void test_dlsym_any(void) // lookup function from anywhere add_func = dlsym(RTLD_DEFAULT, "lib1_add"); - error = dlerror(); - if (error != NULL) { - fprintf(stderr, "%s\n", error); - return; - } + handle_dlsym_error(dlerror()); - printf("test_dlsym_any: %d\n", add_func(1, 2)); + printf("test_dlsym_any: %d\n", add_func(1, 2)); } void test_dlsym(void) -- Gitee