do not return NULL symbols.
authorTim Janik <timj@gtk.org>
Wed, 1 Mar 2000 04:57:07 +0000 (04:57 +0000)
committerTim Janik <timj@src.gnome.org>
Wed, 1 Mar 2000 04:57:07 +0000 (04:57 +0000)
Wed Mar  1 05:34:47 2000  Tim Janik  <timj@gtk.org>

        * gmodule-beos.c (_g_module_symbol): do not return NULL symbols.

        * gmodule-os2.c: removed NetBSD specific defines.
        (_g_module_self): set an error message for unsupported behaviour.

        * gmodule-beos.c: many coding style fixups.
        (_g_module_open):
        (_g_module_self):
        (_g_module_close):
        (_g_module_symbol): bunch of memory leaks plugged.

        * gmodule-dl.c: make sure the error message returned from dlerror()
        is always != NULL, by using a wrapper function fetch_dlerror(). based
        on a patch to fix _g_module_symbol() for NetBSD from Scott Presnell
        <srp@zgi.com>.

        * gmodule-dld.c: minor indentation.

        * gmodule-win32.c: minor cleanups.

        * merges from glib-1-2.

gmodule/ChangeLog
gmodule/gmodule-beos.c
gmodule/gmodule-dl.c
gmodule/gmodule-dld.c
gmodule/gmodule-os2.c

index 040f881..f37adc1 100644 (file)
@@ -1,3 +1,27 @@
+Wed Mar  1 05:34:47 2000  Tim Janik  <timj@gtk.org>
+
+        * gmodule-beos.c (_g_module_symbol): do not return NULL symbols.
+
+        * gmodule-os2.c: removed NetBSD specific defines.
+        (_g_module_self): set an error message for unsupported behaviour.
+
+        * gmodule-beos.c: many coding style fixups.
+        (_g_module_open):
+        (_g_module_self):
+        (_g_module_close):
+        (_g_module_symbol): bunch of memory leaks plugged.
+
+        * gmodule-dl.c: make sure the error message returned from dlerror()
+        is always != NULL, by using a wrapper function fetch_dlerror(). based
+        on a patch to fix _g_module_symbol() for NetBSD from Scott Presnell
+        <srp@zgi.com>.
+
+        * gmodule-dld.c: minor indentation.
+
+        * gmodule-win32.c: minor cleanups.
+
+       * merges from glib-1-2.
+
 2000-01-13  Martin Baulig  <martin@home-of-linux.org>
 
        * gmodule.c (g_module_open): Check whether `check_init' is not NULL
index 8cc99b2..159d4f8 100644 (file)
@@ -1,5 +1,5 @@
 /* GMODULE - GLIB wrapper code for dynamic module loading
- * Copyright (C) 1998 Tim Janik  
+ * Copyright (C) 1998, 2000 Tim Janik  
  *
  * BeOS GMODULE implementation
  * Copyright (C) 1999 Richard Offer and Shawn T. Amundson (amundson@gtk.org)
 
 /* --- functions --- */
 static gpointer
-_g_module_open (const gchar    *file_name,
-               gboolean        bind_lazy)
+_g_module_open (const gchar *file_name,
+               gboolean     bind_lazy)
 {
   image_id handle;
   
   handle = load_add_on (file_name);
-  if (handle < B_OK) {
-    g_module_set_error (g_strdup_printf("failed to load_add_on(%s), reason: %s", 
-                       (gchar *) file_name, strerror(handle)));
-    return NULL;
-  }
-
+  if (handle < B_OK)
+    {
+      gchar *msg = g_strdup_printf ("failed to load_add_on(%s): %s",
+                                   file_name,
+                                   strerror (handle));
+      
+      g_module_set_error (msg);
+      g_free (msg);
+      
+      return NULL;
+    }
+  
   return (gpointer) handle;
 }
 
@@ -76,81 +82,100 @@ _g_module_self (void)
   image_info info;
   int32 cookie = 0;
   status_t status;
-
+  
   /* Is it always the first one?  I'm guessing yes. */
-  if ((status = get_next_image_info(0, &cookie, &info)) == B_OK)
+  status = get_next_image_info (0, &cookie, &info);
+  if (status == B_OK)
     return (gpointer) info.id;
   else
     {
-      g_module_set_error (g_strdup_printf("get_next_image_info() for self failed, reason: %s", strerror(status)));
+      gchar *msg = g_strdup_printf ("failed to get_next_image_info(self): %s",
+                                   strerror (status));
+      
+      g_module_set_error (msg);
+      g_free (msg);
+      
       return NULL;
     }
 }
 
 static void
-_g_module_close (gpointer        handle,
-                gboolean         is_unref)
+_g_module_close (gpointer handle,
+                gboolean is_unref)
 {
-   image_info info;
-   gchar *name;
-
-   if (unload_add_on((image_id) handle) != B_OK)
-     {
-       /* Try and get the name of the image. */
-       if (get_image_info((image_id) handle, &info) != B_OK)
-         name = g_strdup("(unknown)");
-       else
-         name = g_strdup (info.name);
-
-       g_module_set_error (g_strdup_printf("failed to unload_add_on(%s)", 
-                           name));
-       g_free (name);
-     }
+  image_info info;
+  gchar *name;
+  
+  if (unload_add_on ((image_id) handle) != B_OK)
+    {
+      gchar *msg;
+      
+      /* Try and get the name of the image. */
+      if (get_image_info ((image_id) handle, &info) != B_OK)
+       name = g_strdup ("unknown");
+      else
+       name = g_strdup (info.name);
+      
+      msg = g_strdup_printf ("failed to unload_add_on(%s): %s", name, strerror (status));
+      g_module_set_error (msg);
+      g_free (msg);
+      g_free (name);
+    }
 }
 
 static gpointer
-_g_module_symbol (gpointer       handle,
-                 const gchar    *symbol_name)
+_g_module_symbol (gpointer     handle,
+                 const gchar *symbol_name)
 {
   image_id id;
-  gpointer p;
   status_t status;
   image_info info;
-  gchar name[256];
-  int32 name_len;
-  int32 type;
-  int32 n;
-
+  int32 type, name_len;
+  void *p;
+  gchar *msg, name[256];
+  gint n, l;
+  
   id = (image_id) handle;
-
-  if ((status = get_image_info(id, &info)) != B_OK)
+  
+  status = get_image_info (id, &info);
+  if (status != B_OK)
     {
-      g_module_set_error (g_strdup_printf("failed get_image_info(), reason: %s", strerror(status)));
+      msg = g_strdup_printf ("failed to get_image_info(): %s", strerror (status));
+      g_module_set_error (msg);
+      g_free (msg);
+      
       return NULL;
     }
-
+  
+  l = strlen (symbol_name);
   name_len = 256;
   type = B_SYMBOL_TYPE_ANY;
   n = 0;
-  while ((status = get_nth_image_symbol(id, n, name, &name_len, &type, (void **)&p)) == B_OK)
+  status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
+  while (status == B_OK)
     {
-      if (!strncmp (name, symbol_name, strlen(symbol_name)))
-        {
-          return p;
-        }
-
-      if (!strcmp (name, "_end"))
+      if (p && strncmp (name, symbol_name, l) == 0)
+       return p;
+      
+      if (strcmp (name, "_end") == 0)
         {
-          g_module_set_error (g_strdup_printf("g_module_symbol(): no symbol matching '%s'", symbol_name));
-          return NULL;
+         msg = g_strdup_printf ("unmatched symbol name `%s'", symbol_name);
+          g_module_set_error (msg);
+         g_free (msg);
+         
+         return NULL;
         }
-
+      
       name_len = 256;
       type = B_SYMBOL_TYPE_ANY;
       n++;
+      status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
     }
-
-  g_module_set_error (g_strdup_printf("failed get_image_symbol(%s), reason: %s", symbol_name, strerror(status)));
+  
+  msg = g_strdup_printf ("failed to get_image_symbol(%s): %s", symbol_name, strerror (status));
+  g_module_set_error (msg);
+  g_free (msg);
+  
   return NULL;
 }
 
@@ -158,13 +183,16 @@ static gchar*
 _g_module_build_path (const gchar *directory,
                      const gchar *module_name)
 {
-  printf("WARNING: _g_module_build_path() untested!\n");
-  if (directory && *directory) {
-    if (strncmp (module_name, "lib", 3) == 0)
-      return g_strconcat (directory, "/", module_name, NULL);
-    else
-      return g_strconcat (directory, "/lib", module_name, ".so", NULL);
-  } else if (strncmp (module_name, "lib", 3) == 0)
+  g_warning ("_g_module_build_path() untested for BeOS!");
+  
+  if (directory && *directory)
+    {
+      if (strncmp (module_name, "lib", 3) == 0)
+       return g_strconcat (directory, "/", module_name, NULL);
+      else
+       return g_strconcat (directory, "/lib", module_name, ".so", NULL);
+    }
+  else if (strncmp (module_name, "lib", 3) == 0)
     return g_strdup (module_name);
   else
     return g_strconcat ("lib", module_name, ".so", NULL);
index e8fb3f2..bbf7691 100644 (file)
@@ -1,5 +1,5 @@
 /* GMODULE - GLIB wrapper code for dynamic module loading
- * Copyright (C) 1998 Tim Janik
+ * Copyright (C) 1998, 2000 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
 
 
 /* --- functions --- */
+static gchar*
+fetch_dlerror (void)
+{
+  gchar *msg = dlerror ();
+
+  /* make sure we always return an error message != NULL */
+
+  return msg ? msg : "unknown dl-error";
+}
+
 static gpointer
-_g_module_open (const gchar    *file_name,
-               gboolean        bind_lazy)
+_g_module_open (const gchar *file_name,
+               gboolean     bind_lazy)
 {
   gpointer handle;
   
   handle = dlopen (file_name, RTLD_GLOBAL | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
   if (!handle)
-    g_module_set_error (dlerror ());
+    g_module_set_error (fetch_dlerror ());
   
   return handle;
 }
@@ -94,14 +104,14 @@ _g_module_self (void)
   
   handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
   if (!handle)
-    g_module_set_error (dlerror ());
+    g_module_set_error (fetch_dlerror ());
   
   return handle;
 }
 
 static void
-_g_module_close (gpointer        handle,
-                gboolean         is_unref)
+_g_module_close (gpointer handle,
+                gboolean is_unref)
 {
   /* are there any systems out there that have dlopen()/dlclose()
    * without a reference count implementation?
@@ -111,19 +121,19 @@ _g_module_close (gpointer   handle,
   if (is_unref)
     {
       if (dlclose (handle) != 0)
-       g_module_set_error (dlerror ());
+       g_module_set_error (fetch_dlerror ());
     }
 }
 
 static gpointer
-_g_module_symbol (gpointer       handle,
-                 const gchar    *symbol_name)
+_g_module_symbol (gpointer     handle,
+                 const gchar *symbol_name)
 {
   gpointer p;
   
   p = dlsym (handle, symbol_name);
   if (!p)
-    g_module_set_error (dlerror ());
+    g_module_set_error (fetch_dlerror ());
   
   return p;
 }
index e8cf40d..1d1391a 100644 (file)
@@ -1,5 +1,5 @@
 /* GMODULE - GLIB wrapper code for dynamic module loading
- * Copyright (C) 1998 Tim Janik
+ * Copyright (C) 1998, 2000 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -68,8 +68,8 @@
 
 /* --- functions --- */
 static gpointer
-_g_module_open (const gchar    *file_name,
-               gboolean        bind_lazy)
+_g_module_open (const gchar *file_name,
+               gboolean     bind_lazy)
 {
   shl_t shl_handle;
   
@@ -97,8 +97,8 @@ _g_module_self (void)
 }
 
 static void
-_g_module_close (gpointer        handle,
-                gboolean         is_unref)
+_g_module_close (gpointer handle,
+                gboolean is_unref)
 {
   if (!is_unref)
     {
@@ -108,13 +108,22 @@ _g_module_close (gpointer   handle,
 }
 
 static gpointer
-_g_module_symbol (gpointer       handle,
-                 const gchar    *symbol_name)
+_g_module_symbol (gpointer     handle,
+                 const gchar *symbol_name)
 {
   gpointer p = NULL;
   
   /* should we restrict lookups to TYPE_PROCEDURE?
    */
+  if (handle == PROG_HANDLE)
+    {
+      /* PROG_HANDLE will only lookup symbols in the program itself, not honouring
+       * libraries. passing NULL as a handle will also try to lookup the symbol
+       * in currently loaded libraries. fix pointed out and supplied by:
+       * David Gero <dgero@nortelnetworks.com>
+       */
+      handle = NULL;
+    }
   if (shl_findsym ((shl_t*) &handle, symbol_name, TYPE_UNDEFINED, &p) != 0 ||
       handle == NULL || p == NULL)
     {
index 7cfa720..d689a42 100644 (file)
@@ -1,5 +1,5 @@
 /* GMODULE - GLIB wrapper code for dynamic module loading
- * Copyright (C) 1998 Tim Janik
+ * Copyright (C) 1998, 2000 Tim Janik
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
 /* dlerror() is not implemented on all systems
  */
 #ifndef        G_MODULE_HAVE_DLERROR
-#  ifdef __NetBSD__
-#    define dlerror()  g_strerror (errno)
-#  else /* !__NetBSD__ */
 /* could we rely on errno's state here? */
-#    define dlerror()  "unknown dl-error"
-#  endif /* !__NetBSD__ */
+#  define dlerror()    "unknown dl-error"
 #endif /* G_MODULE_HAVE_DLERROR */
 
 /* some flags are missing on some systems, so we provide
@@ -92,15 +88,16 @@ _g_module_self (void)
    * are required on some systems.
    */
   
-/* XXX, not supported */
+  /* XXX, not supported */
   handle = NULL;
+  g_module_set_error ("module handle for self not supported");
   
   return handle;
 }
 
 static void
-_g_module_close (gpointer        handle,
-                gboolean         is_unref)
+_g_module_close (gpointer handle,
+                gboolean is_unref)
 {
   /* are there any systems out there that have dlopen()/dlclose()
    * without a reference count implementation?
@@ -109,14 +106,14 @@ _g_module_close (gpointer   handle,
   
   if (is_unref)
     {
-/* XXX, no return code */
+      /* XXX, no return code */
       dlclose (handle);
     }
 }
 
 static gpointer
-_g_module_symbol (gpointer       handle,
-                 const gchar    *symbol_name)
+_g_module_symbol (gpointer     handle,
+                 const gchar *symbol_name)
 {
   gpointer p;