]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
sysdeps-unix: Deduplicate error handling for getpwnam and getpwnam_r
authorSimon McVittie <smcv@collabora.com>
Thu, 29 Jun 2023 16:00:58 +0000 (17:00 +0100)
committerSimon McVittie <smcv@collabora.com>
Mon, 21 Aug 2023 13:49:31 +0000 (13:49 +0000)
The only difference between these was that we only needed to allocate
and free buf in the getpwnam_r case. We expect that all reasonable
Unix platforms will have getpwnam_r (it's in POSIX) so adding a no-op
dbus_free(NULL) to the getpwnam code path seems harmless.

This will be helpful when we make the error handling better, in a
subsequent commit.

Helps: https://gitlab.freedesktop.org/dbus/dbus/-/issues/343
Signed-off-by: Simon McVittie <smcv@collabora.com>
dbus/dbus-sysdeps-unix.c

index 60bdc6f710ae4f6b8d86deb55d9c1b95390f0c2c..4dfdf549413fbc0716ba99510d914505a4f829d6 100644 (file)
@@ -2727,12 +2727,12 @@ fill_user_info (DBusUserInfo       *info,
    * checks
    */
 
-#ifdef HAVE_GETPWNAM_R
   {
     struct passwd *p;
+    char *buf = NULL;
+#ifdef HAVE_GETPWNAM_R
     int result;
     size_t buflen;
-    char *buf;
     struct passwd p_str;
 
     /* retrieve maximum needed size for buf */
@@ -2773,43 +2773,27 @@ fill_user_info (DBusUserInfo       *info,
             break;
           }
       }
-    if (result == 0 && p == &p_str)
-      {
-        if (!fill_user_info_from_passwd (p, info, error))
-          {
-            dbus_free (buf);
-            return FALSE;
-          }
-        dbus_free (buf);
-      }
-    else
-      {
-        dbus_set_error (error, _dbus_error_from_errno (errno),
-                        "User \"%s\" unknown or no memory to allocate password entry\n",
-                        username_c ? username_c : "???");
-        _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
-        dbus_free (buf);
-        return FALSE;
-      }
-  }
+
+    if (result != 0 || p != &p_str)
+      p = NULL;
 #else /* ! HAVE_GETPWNAM_R */
-  {
     /* I guess we're screwed on thread safety here */
-    struct passwd *p;
-
 #warning getpwnam_r() not available, please report this to the dbus maintainers with details of your OS
 
     if (uid != DBUS_UID_UNSET)
       p = getpwuid (uid);
     else
       p = getpwnam (username_c);
+#endif  /* ! HAVE_GETPWNAM_R */
 
     if (p != NULL)
       {
         if (!fill_user_info_from_passwd (p, info, error))
           {
+            dbus_free (buf);
             return FALSE;
           }
+        dbus_free (buf);
       }
     else
       {
@@ -2817,10 +2801,10 @@ fill_user_info (DBusUserInfo       *info,
                         "User \"%s\" unknown or no memory to allocate password entry\n",
                         username_c ? username_c : "???");
         _dbus_verbose ("User %s unknown\n", username_c ? username_c : "???");
+        dbus_free (buf);
         return FALSE;
       }
   }
-#endif  /* ! HAVE_GETPWNAM_R */
 
   /* Fill this in so we can use it to get groups */
   username_c = info->username;