From 88221f3b5fb7f2d824e367bf565b71640dedc8f9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Wed, 19 May 2010 08:47:44 +0200 Subject: fixed the function dlsym casting problem --- tests/library/test_loader.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/library/test_loader.c b/tests/library/test_loader.c index b9bce9d..7e5fa28 100644 --- a/tests/library/test_loader.c +++ b/tests/library/test_loader.c @@ -26,6 +26,8 @@ int main( void ) { wolf_error_t error; char errbuf[512]; typedef int (*multiply_by_two_func)( int ); + union { multiply_by_two_func func; void *obj; } alias; + void *symbol; multiply_by_two_func func; int res = 0; @@ -37,8 +39,12 @@ int main( void ) { return EXIT_FAILURE; } - /* fetch a known symbol (multiply_by_two) */ - *(void **)(&func) = wolf_library_get( library, "multiply_by_two", &error ); + /* fetch a known function symbol (multiply_by_two), for the reasons why naive + * casting doesn't work, see: + * http://en.wikipedia.org/wiki/Dynamic_loading#cite_note-gcc-strict-aliasing-5 */ + symbol = wolf_library_get( library, "multiply_by_two", &error ); + alias.obj = symbol; + func = alias.func; /* call it */ res = func( 2 ); -- cgit v1.2.3-54-g00ecf