[Libguestfs] [PATCH v2] lib, lua: Fix usage of strerror_r

Richard W.M. Jones rjones at redhat.com
Thu Dec 9 09:06:01 UTC 2021


  $ ./run guestfish -vx add-drive foo "readonly:true"
  libguestfs: trace: set_pgroup true
  libguestfs: trace: set_pgroup = 0
  libguestfs: trace: add_drive "foo" "readonly:true"
  libguestfs: error: foo:
  libguestfs: trace: add_drive = -1 (error)
  libguestfs: trace: close
  libguestfs: closing guestfs handle 0x55c0bacf12a0 (state 0)

Fix the usage of strerror_r by using the new wrapper defined in
libguestfs-common.  A similar fix is made in the Lua bindings.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2030396
Reported-by: Masayoshi Mizuma <m.mizuma at jp.fujitsu.com>
Tested-by: Masayoshi Mizuma <m.mizuma at jp.fujitsu.com>
---
 common           |  2 +-
 generator/lua.ml | 18 ++++++++----------
 lib/errors.c     |  7 ++++---
 3 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/common b/common
index a405dc599e..8fcdc2c6a1 160000
--- a/common
+++ b/common
@@ -1 +1 @@
-Subproject commit a405dc599e571410b83d145d02705708e1715e94
+Subproject commit 8fcdc2c6a1e438012aaa24c58fa06f9d1efc6b93
diff --git a/generator/lua.ml b/generator/lua.ml
index 7b05c72e7a..0d7e63be0f 100644
--- a/generator/lua.ml
+++ b/generator/lua.ml
@@ -164,10 +164,9 @@ guestfs_int_lua_create (lua_State *L)
     return luaL_error (L, \"Guestfs.create: too many arguments\");
 
   g = guestfs_create_flags (flags);
-  if (!g) {
-    ignore_value (strerror_r (errno, err, sizeof err));
-    return luaL_error (L, \"Guestfs.create: cannot create handle: %%s\", err);
-  }
+  if (!g)
+    return luaL_error (L, \"Guestfs.create: cannot create handle: %%s\",
+                       guestfs_int_strerror (errno, err, sizeof err));
 
   guestfs_set_error_handler (g, NULL, NULL);
 
@@ -243,10 +242,9 @@ error__tostring (lua_State *L)
   lua_gettable (L, 1);
   msg = luaL_checkstring (L, -1);
 
-  if (code) {
-    ignore_value (strerror_r (code, err, sizeof err));
-    lua_pushfstring (L, \"%%s: %%s\", msg, err);
-  }
+  if (code)
+    lua_pushfstring (L, \"%%s: %%s\", msg,
+                     guestfs_int_strerror (code, err, sizeof err));
   else
     lua_pushstring (L, msg);
 
@@ -655,8 +653,8 @@ get_string_list (lua_State *L, int index)
 
   strs = malloc ((len+1) * sizeof (char *));
   if (strs == NULL) {
-    ignore_value (strerror_r (errno, err, sizeof err));
-    luaL_error (L, \"get_string_list: malloc failed: %%s\", err);
+    luaL_error (L, \"get_string_list: malloc failed: %%s\",
+                guestfs_int_strerror (errno, err, sizeof err));
     /*NOTREACHED*/
     return NULL;
   }
diff --git a/lib/errors.c b/lib/errors.c
index bd9699eeb1..285209e44e 100644
--- a/lib/errors.c
+++ b/lib/errors.c
@@ -322,6 +322,7 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...)
   const int errnum = errno;
   int err;
   char buf[256];
+  const char *errstr;
   struct error_data *error_data = get_error_data (g);
 
   va_start (args, fs);
@@ -330,11 +331,11 @@ guestfs_int_perrorf (guestfs_h *g, const char *fs, ...)
 
   if (err < 0) return;
 
-  ignore_value (strerror_r (errnum, buf, sizeof buf));
+  errstr = guestfs_int_strerror (errnum, buf, sizeof buf);
 
-  msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (buf) + 1);
+  msg = safe_realloc (g, msg, strlen (msg) + 2 + strlen (errstr) + 1);
   strcat (msg, ": ");
-  strcat (msg, buf);
+  strcat (msg, errstr);
 
   /* set_last_error first so that the callback can access the error
    * message and errno through the handle if it wishes.
-- 
2.32.0




More information about the Libguestfs mailing list