From 5bcd8c53987200107b28a76f4ef38d805e0f6d25 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 1 May 2017 11:03:01 +0200 Subject: Fix bug in dlerror second consecutive call According to the specs, a second consecutive call to dlerror should always return NULL . This was the case in dlfcn-win32 before https://github.com/dlfcn-win32/dlfcn-win32/pull/20 introduce a regression that caused dlerror to crash on the second consecutive call. In this commit the issue is fixed as suggested in https://github.com/dlfcn-win32/dlfcn-win32/issues/34 and a regression test has been added. --- dlfcn.c | 6 ++++++ test.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/dlfcn.c b/dlfcn.c index 4367bd7..a91e963 100644 --- a/dlfcn.c +++ b/dlfcn.c @@ -431,6 +431,12 @@ end: char *dlerror( void ) { char *error_pointer = dlerror_buffer; + + /* If this is the second consecutive call to dlerror, return NULL */ + if (current_error == NULL) + { + return NULL; + } #ifdef UNICODE errno_t err = 0; diff --git a/test.c b/test.c index 14523ad..ba440da 100644 --- a/test.c +++ b/test.c @@ -304,6 +304,21 @@ int main() error = dlerror( ); printf( "SUCCESS\tCould not get nonexistent symbol from global handle: %s\n", error ? error : "" ); + + /* Test that the second call to dlerror() returns null as in the specs + See https://github.com/dlfcn-win32/dlfcn-win32/issues/34 */ + error = dlerror( ); + if( error == NULL ) + { + printf( "SUCCESS\tSecond consecutive call to dlerror returned NULL\n"); + } + else + { + printf( "ERROR\tSecond consecutive call to dlerror returned a non-NULL pointer: %p\n", error ); + CLOSE_LIB; + CLOSE_GLOBAL; + RETURN_ERROR; + } } function = dlsym(global, "fwrite"); -- cgit v1.2.3-55-g6feb