]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Make code thread-safe.
authorUlrich Drepper <drepper@redhat.com>
Sun, 1 Aug 1999 21:55:30 +0000 (21:55 +0000)
committerUlrich Drepper <drepper@redhat.com>
Sun, 1 Aug 1999 21:55:30 +0000 (21:55 +0000)
elf/dlerror.c

index 4716bdd272da2c8db62fa5d3550f9f491f732d96..d78c94af819a5c38390e9eda949807afe57750e6 100644 (file)
@@ -29,6 +29,7 @@
 struct dl_action_result
   {
     int errcode;
+    int returned;
     char *errstring;
   };
 static struct dl_action_result last_result;
@@ -46,38 +47,40 @@ static void free_key_mem (void *mem);
 char *
 dlerror (void)
 {
-  static char *buf;
+  char *buf;
   struct dl_action_result *result;
 
-  if (buf)
-    {
-      free (buf);
-      buf = NULL;
-    }
-
   /* Get error string.  */
   result = (struct dl_action_result *) __libc_getspecific (key);
   if (result == NULL)
     result = &last_result;
 
-  if (! result->errstring)
-    return NULL;
-
-  if (result->errcode == 0)
-    buf = result->errstring;
+  if (result->returned != 0)
+    {
+      /* We can now free the string.  */
+      if (result->errstring != NULL)
+       {
+         free (result->errstring);
+         result->errstring = NULL;
+       }
+      buf = NULL;
+    }
   else
     {
-      if (__asprintf (&buf, "%s: %s",
-                     result->errstring, strerror (result->errcode)) == -1)
-       buf = NULL;
+      buf = result->errstring;
+      if (result->errcode != 0
+         && __asprintf (&buf, "%s: %s",
+                        result->errstring, strerror (result->errcode)) != -1)
+       {
+         /* We don't need the error string anymore.  */
+         free (result->errstring);
+         result->errstring = buf;
+       }
 
-      /* We don't need the error string anymore.  */
-      free (result->errstring);
+      /* Mark the error as returned.  */
+      result->returned = 1;
     }
 
-  /* Reset the error indicator.  */
-  result->errstring = NULL;
-
   return buf;
 }
 
@@ -121,6 +124,9 @@ _dlerror_run (void (*operate) (void *), void *args)
 
   result->errcode = _dl_catch_error (&result->errstring, operate, args);
 
+  /* If no error we mark that no error string is available.  */
+  result->returned = result->errstring == NULL;
+
   return result->errstring != NULL;
 }