Commit f6f6dd2dcff42b0f7c0cee047f1e9862412a3f02

Pali Rohár 2019-05-23T20:24:05

Test that dlerror() returns non-NULL error for failed calls

diff --git a/test.c b/test.c
index 814aca1..d99fd52 100644
--- a/test.c
+++ b/test.c
@@ -201,11 +201,14 @@ int main()
         CLOSE_GLOBAL;
         RETURN_ERROR;
     }
-    else {
-        error = dlerror( );
-        printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n",
-                error ? error : "" );
+    error = dlerror( );
+    if( !error )
+    {
+        printf( "ERROR\tNo error from dlsym for nonexistent symbol\n" );
+        RETURN_ERROR;
     }
+    else
+        printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n", error );
 
     function = dlsym( global, "function" );
     if( !function )
@@ -231,11 +234,14 @@ int main()
         CLOSE_GLOBAL;
         RETURN_ERROR;
     }
-    else {
-        error = dlerror( );
-        printf( "SUCCESS\tCould not get nonexistent symbol from global handle: %s\n",
-                error ? error : "" );
+    error = dlerror( );
+    if( !error )
+    {
+        printf( "ERROR\tNo error from dlsym for nonexistent symbol\n" );
+        RETURN_ERROR;
     }
+    else
+        printf( "SUCCESS\tCould not get nonexistent symbol from global handle: %s\n", error );
 
     ret = dlclose( library );
     if( ret )
@@ -293,11 +299,14 @@ int main()
         CLOSE_GLOBAL;
         RETURN_ERROR;
     }
-    else {
-        error = dlerror( );
-        printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n",
-                error ? error : "" );
+    error = dlerror( );
+    if( !error )
+    {
+        printf( "ERROR\tNo error from dlsym for nonexistent symbol\n" );
+        RETURN_ERROR;
     }
+    else
+        printf( "SUCCESS\tCould not get nonexistent symbol from library handle: %s\n", error );
 
     function = dlsym( global, "function" );
     if( function )
@@ -321,11 +330,14 @@ int main()
         CLOSE_GLOBAL;
         RETURN_ERROR;
     }
-    else {
-        error = dlerror( );
-        printf( "SUCCESS\tDid not get nonexistent local symbol from global handle: %s\n",
-                error ? error : "" );
+    error = dlerror( );
+    if( !error )
+    {
+        printf( "ERROR\tNo error from dlsym for nonexistent symbol\n" );
+        RETURN_ERROR;
     }
+    else
+        printf( "SUCCESS\tDid not get nonexistent local symbol from global handle: %s\n", error );
 
     library = dlopen( "testdll.dll", RTLD_GLOBAL );
     if( !library )
@@ -363,10 +375,15 @@ int main()
         CLOSE_GLOBAL;
         RETURN_ERROR;
     }
-    else {
-        error = dlerror( );
-        printf( "SUCCESS\tCould not get nonexistent symbol from global handle: %s\n",
-                error ? error : "" );
+    error = dlerror( );
+    if( !error )
+    {
+        printf( "ERROR\tNo error from dlsym for nonexistent symbol\n" );
+        RETURN_ERROR;
+    }
+    else
+    {
+        printf( "SUCCESS\tCould not get nonexistent symbol from global handle: %s\n", error );
                 
         /* Test that the second call to dlerror() returns null as in the specs 
            See https://github.com/dlfcn-win32/dlfcn-win32/issues/34 */