about summary refs log tree commit diff
path: root/dlfcn
diff options
context:
space:
mode:
Diffstat (limited to 'dlfcn')
-rw-r--r--dlfcn/Makefile9
-rw-r--r--dlfcn/dlerror.c9
-rw-r--r--dlfcn/failtest.c58
-rw-r--r--dlfcn/failtestmod.c25
4 files changed, 95 insertions, 6 deletions
diff --git a/dlfcn/Makefile b/dlfcn/Makefile
index d14d382702..ed725639ef 100644
--- a/dlfcn/Makefile
+++ b/dlfcn/Makefile
@@ -20,7 +20,7 @@ subdir		:= dlfcn
 headers		:= bits/dlfcn.h dlfcn.h
 extra-libs	:= libdl
 libdl-routines	:= dlopen dlclose dlsym dlvsym dlerror dladdr
-distribute	:= dlopenold.c glreflib1.c glreflib2.c
+distribute	:= dlopenold.c glreflib1.c glreflib2.c failtestmod.c
 
 extra-libs-others := libdl
 
@@ -32,9 +32,9 @@ libdl-shared-only-routines := dlopenold
 endif
 
 ifeq (yes,$(build-shared))
-tests = glrefmain
+tests = glrefmain failtest
 endif
-modules-names = glreflib1 glreflib2
+modules-names = glreflib1 glreflib2 failtestmod
 extra-objs += $(modules-names:=.os)
 
 include ../Rules
@@ -46,3 +46,6 @@ $(test-modules): $(objpfx)%.so: $(objpfx)%.os
 $(objpfx)glrefmain: $(objpfx)libdl.so
 $(objpfx)glrefmain.out: $(objpfx)glrefmain \
 			$(objpfx)glreflib1.so $(objpfx)glreflib2.so
+
+$(objpfx)failtest: $(libdl)
+$(objpfx)failtest.out: $(objpfx)failtestmod.so
diff --git a/dlfcn/dlerror.c b/dlfcn/dlerror.c
index 972a9ac263..34ea82907e 100644
--- a/dlfcn/dlerror.c
+++ b/dlfcn/dlerror.c
@@ -120,9 +120,12 @@ _dlerror_run (void (*operate) (void *), void *args)
     }
 
   if (result->errstring != NULL)
-    /* Free the error string from the last failed command.  This can
-       happen if `dlerror' was not run after an error was found.  */
-    free ((char *) result->errstring);
+    {
+      /* Free the error string from the last failed command.  This can
+	 happen if `dlerror' was not run after an error was found.  */
+      free ((char *) result->errstring);
+      result->errstring = NULL;
+    }
 
   result->errcode = _dl_catch_error (&result->objname, &result->errstring,
 				     operate, args);
diff --git a/dlfcn/failtest.c b/dlfcn/failtest.c
new file mode 100644
index 0000000000..50bbf10596
--- /dev/null
+++ b/dlfcn/failtest.c
@@ -0,0 +1,58 @@
+#include <dlfcn.h>
+#include <stdio.h>
+
+
+/* Number of rounds we perform the test.  */
+#define TEST_ROUNDS	10
+
+
+static const char unknown[] = "a-file-with-this-name-does-not-exist";
+static const char exists[] = "failtestmod.so";
+
+
+int
+main (void)
+{
+  int i;
+
+  setvbuf (stdout, NULL, _IONBF, 0);
+
+  for (i = 0; i < TEST_ROUNDS; ++i)
+    {
+      void *dsc;
+
+      printf ("Round %d: Try loading \"%s\"\n", i, unknown);
+
+      dsc = dlopen (unknown, RTLD_NOW);
+      if (dsc != NULL)
+	{
+	  printf ("We found a file of name \"%s\": this should not happen\n",
+		  unknown);
+	  return 1;
+	}
+
+      printf ("Round %d: loading \"%s\" failed\n", i, unknown);
+
+      /* Don't use `dlerror', just load an existing file.  */
+      dsc = dlopen (exists, RTLD_NOW);
+      if (dsc == NULL)
+	{
+	  printf ("Could not load \"%s\": %s\n", exists, dlerror ());
+	  return 1;
+	}
+
+      printf ("Round %d: Loaded \"%s\"\n", i, exists);
+
+      dlclose (dsc);
+
+      printf ("Round %d: Unloaded \"%s\"\n", i, exists);
+    }
+
+  return 0;
+}
+
+
+void
+foo (void)
+{
+}
diff --git a/dlfcn/failtestmod.c b/dlfcn/failtestmod.c
new file mode 100644
index 0000000000..595da4d567
--- /dev/null
+++ b/dlfcn/failtestmod.c
@@ -0,0 +1,25 @@
+#include <dlfcn.h>
+#include <stdio.h>
+
+
+void
+__attribute__ ((__constructor__))
+constr (void)
+{
+  void *handle;
+  void *m;
+
+  /* Open the library.  */
+  handle = dlopen (NULL, RTLD_NOW);
+  if (handle == NULL)
+    {
+      puts ("Cannot get handle to own object");
+      return;
+    }
+
+  /* Get a symbol.  */
+  m = dlsym (handle, "main");
+  puts ("called dlsym() to get main");
+
+  dlclose (handle);
+}