From 22bbdb025c7516818e6b143f3af8160be231b64a Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Mon, 25 Nov 2024 18:00:03 +1100 Subject: [PATCH] misc(tests): fix compilation of `dlfcn.c` From man dlopen(3) > According to the ISO C standard, casting between function > pointers and 'void *', as done above, produces undefined results. > POSIX.1-2001 and POSIX.1-2008 accepted this state of affairs and > proposed the following workaround: > > *(void **) (&cosine) = dlsym(handle, "cos"); > > This (clumsy) cast conforms with the ISO C standard and will > avoid any compiler warnings. > > The 2013 Technical Corrigendum 1 to POSIX.1-2008 improved matters > by requiring that conforming implementations support casting > 'void *' to a function pointer. Nevertheless, some compilers > (e.g., gcc with the '-pedantic' option) may complain about the > cast used in this program. Signed-off-by: Anhad Singh --- tests/Makefile | 2 +- tests/dlfcn.c | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 16a5e3b2f4..ffea216c9b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -157,7 +157,7 @@ EXPECT_NAMES=\ BUILD?=. DYNAMIC_ONLY_NAMES=\ - # dlfcn + dlfcn # Binaries that may generate varied output NAMES=\ diff --git a/tests/dlfcn.c b/tests/dlfcn.c index 47e2f69bfa..bf1d7354d4 100644 --- a/tests/dlfcn.c +++ b/tests/dlfcn.c @@ -15,7 +15,10 @@ void test_dlopen_null() printf("dlopen(NULL) failed\n"); exit(1); } - int (*f)(int, int) = dlsym(handle, "add"); + + int (*f)(int, int); + *(void **)(&f) = dlsym(handle, "add"); + if (!f) { printf("dlsym(handle, add) failed\n"); exit(2); @@ -33,7 +36,10 @@ void test_dlopen_libc() printf("dlopen(libc.so.6) failed\n"); exit(1); } - int (*f)(const char*) = dlsym(handle, "puts"); + + int (*f)(const char*); + *(void **)(&f) = dlsym(handle, "puts"); + if (!f) { printf("dlsym(handle, puts) failed\n"); exit(2); @@ -50,7 +56,10 @@ void test_dlsym_function() printf("dlopen(sharedlib.so) failed\n"); exit(1); } - void (*f)() = dlsym(handle, "print"); + + void (*f)(); + *(void **)(&f) = dlsym(handle, "print"); + if (!f) { printf("dlsym(handle, print) failed\n"); exit(2);