From: Simon McVittie Date: Thu, 29 Jun 2023 16:00:58 +0000 (+0100) Subject: sysdeps-unix: Deduplicate error handling for getpwnam and getpwnam_r X-Git-Tag: dbus-1.15.8~3^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80b90e570e0cc950491aaeacb008b101922d8a57;p=thirdparty%2Fdbus.git 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 --- diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 60bdc6f71..4dfdf5494 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;