[Libguestfs] [nbdkit PATCH 3/4] plugins: Make plugin_name() reliable past unload

Eric Blake eblake at redhat.com
Fri Nov 17 03:13:57 UTC 2017


Directly returning storage in the module is risky, because it
forces callers to strdup() if they aren't sure of the lifetime.
Copying into the heap up front means that plugin_name() can
now be safely used even if unload happens in the meantime.

As long as we have only a single plugin in static storage (see
TODO), we don't need to free the copy.

Signed-off-by: Eric Blake <eblake at redhat.com>
---
 src/plugins.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/plugins.c b/src/plugins.c
index f5056d9..e8c6b28 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -143,6 +143,12 @@ plugin_register (const char *_filename,
       exit (EXIT_FAILURE);
     }
   }
+  /* Copy the module's name into local storage, so that plugin.name
+   * survives past unload. */
+  if (!(plugin.name = strdup (plugin.name))) {
+    perror ("strdup");
+    exit (EXIT_FAILURE);
+  }

   debug ("registered %s (name %s)", filename, plugin.name);

@@ -177,8 +183,6 @@ plugin_cleanup (void)
 const char *
 plugin_name (void)
 {
-  assert (dl);
-
   return plugin.name;
 }

-- 
2.13.6




More information about the Libguestfs mailing list