summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon McVittie <smcv@collabora.com>2023-06-29 17:00:58 +0100
committerSimon McVittie <smcv@collabora.com>2023-08-21 13:49:31 +0000
commit80b90e570e0cc950491aaeacb008b101922d8a57 (patch)
tree12bd458cb21b1ba5691d6ef15414a8c9f94d4fcf
parent672f05e5f377cc28c5a13f65c9d1bd9ae74c9b8f (diff)
sysdeps-unix: Deduplicate error handling for getpwnam and getpwnam_r
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>
-rw-r--r--dbus/dbus-sysdeps-unix.c34
1 files changed, 9 insertions, 25 deletions
diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c
index 60bdc6f7..4dfdf549 100644
--- a/dbus/dbus-sysdeps-unix.c
+++ b/dbus/dbus-sysdeps-unix.c
@@ -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;