Commit 4960605816a3ae1bc2de3e357529ea955e256601

Silvio Traversaro 2017-05-04T13:12:01

Merge pull request #36 from dlfcn-win32/fix-dlerror Fix bug in dlerror second consecutive call

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");