Do not try to access errno after calling getpwnam_r.

This commit is contained in:
Gleb Popov 2021-09-28 10:36:42 +00:00 committed by Philip Withnall
parent 6ccb387197
commit 709226d6f2

View File

@ -463,7 +463,6 @@ g_unix_get_passwd_entry (const gchar *user_name,
} *buffer = NULL; } *buffer = NULL;
gsize string_buffer_size = 0; gsize string_buffer_size = 0;
GError *local_error = NULL; GError *local_error = NULL;
int errsv = 0;
g_return_val_if_fail (user_name != NULL, NULL); g_return_val_if_fail (user_name != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@ -493,10 +492,8 @@ g_unix_get_passwd_entry (const gchar *user_name,
*/ */
buffer = g_malloc0 (sizeof (*buffer) + string_buffer_size + 6); buffer = g_malloc0 (sizeof (*buffer) + string_buffer_size + 6);
errno = 0;
retval = getpwnam_r (user_name, &buffer->pwd, buffer->string_buffer, retval = getpwnam_r (user_name, &buffer->pwd, buffer->string_buffer,
string_buffer_size, &passwd_file_entry); string_buffer_size, &passwd_file_entry);
errsv = errno;
/* Bail out if: the lookup was successful, or if the user id can't be /* Bail out if: the lookup was successful, or if the user id can't be
* found (should be pretty rare case actually), or if the buffer should be * found (should be pretty rare case actually), or if the buffer should be
@ -508,19 +505,19 @@ g_unix_get_passwd_entry (const gchar *user_name,
break; break;
} }
else if (retval == 0 || else if (retval == 0 ||
errsv == ENOENT || errsv == ESRCH || retval == ENOENT || retval == ESRCH ||
errsv == EBADF || errsv == EPERM) retval == EBADF || retval == EPERM)
{ {
/* Username not found. */ /* Username not found. */
g_unix_set_error_from_errno (&local_error, errsv); g_unix_set_error_from_errno (&local_error, retval);
break; break;
} }
else if (errsv == ERANGE) else if (retval == ERANGE)
{ {
/* Cant allocate enough string buffer space. */ /* Cant allocate enough string buffer space. */
if (string_buffer_size > 32 * 1024) if (string_buffer_size > 32 * 1024)
{ {
g_unix_set_error_from_errno (&local_error, errsv); g_unix_set_error_from_errno (&local_error, retval);
break; break;
} }
@ -529,7 +526,7 @@ g_unix_get_passwd_entry (const gchar *user_name,
} }
else else
{ {
g_unix_set_error_from_errno (&local_error, errsv); g_unix_set_error_from_errno (&local_error, retval);
break; break;
} }
} }
@ -543,7 +540,6 @@ g_unix_get_passwd_entry (const gchar *user_name,
{ {
g_clear_pointer (&buffer, g_free); g_clear_pointer (&buffer, g_free);
g_propagate_error (error, g_steal_pointer (&local_error)); g_propagate_error (error, g_steal_pointer (&local_error));
errno = errsv;
} }
return (struct passwd *) g_steal_pointer (&buffer); return (struct passwd *) g_steal_pointer (&buffer);