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 <andypython@protonmail.com>
This commit is contained in:
Anhad Singh
2024-11-25 18:00:03 +11:00
parent 6020c54ce7
commit 22bbdb025c
2 changed files with 13 additions and 4 deletions
+1 -1
View File
@@ -157,7 +157,7 @@ EXPECT_NAMES=\
BUILD?=.
DYNAMIC_ONLY_NAMES=\
# dlfcn
dlfcn
# Binaries that may generate varied output
NAMES=\
+12 -3
View File
@@ -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);