From 309b6fa12b33599999a9af4ac0c51cac2f8a3ccb Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 4 May 2025 19:12:24 +0200 Subject: [PATCH] Cleanup php_posix_passwd_to_array() This function can be static, and the error checks are pointless: 1. It's guaranteed that the return value is an array by now, as it is always preceded by array_init(return_value). 2. The null check for pw is pointless as every callee already handles that in a better way. --- ext/posix/posix.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/ext/posix/posix.c b/ext/posix/posix.c index 68d47840c5e20..09a8961196fd9 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -906,12 +906,9 @@ PHP_FUNCTION(posix_getgrgid) } /* }}} */ -int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */ +static void php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */ { - if (NULL == pw) - return 0; - if (NULL == return_value || Z_TYPE_P(return_value) != IS_ARRAY) - return 0; + ZEND_ASSERT(Z_TYPE_P(return_value) == IS_ARRAY); add_assoc_string(return_value, "name", pw->pw_name); add_assoc_string(return_value, "passwd", pw->pw_passwd); @@ -920,7 +917,6 @@ int php_posix_passwd_to_array(struct passwd *pw, zval *return_value) /* {{{ */ add_assoc_string(return_value, "gecos", pw->pw_gecos); add_assoc_string(return_value, "dir", pw->pw_dir); add_assoc_string(return_value, "shell", pw->pw_shell); - return 1; } /* }}} */ @@ -973,11 +969,7 @@ PHP_FUNCTION(posix_getpwnam) #endif array_init(return_value); - if (!php_posix_passwd_to_array(pw, return_value)) { - zend_array_destroy(Z_ARR_P(return_value)); - php_error_docref(NULL, E_WARNING, "Unable to convert posix passwd struct to array"); - RETVAL_FALSE; - } + php_posix_passwd_to_array(pw, return_value); #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWNAM_R) efree(buf); #endif @@ -1033,11 +1025,7 @@ PHP_FUNCTION(posix_getpwuid) #endif array_init(return_value); - if (!php_posix_passwd_to_array(pw, return_value)) { - zend_array_destroy(Z_ARR_P(return_value)); - php_error_docref(NULL, E_WARNING, "Unable to convert posix passwd struct to array"); - RETVAL_FALSE; - } + php_posix_passwd_to_array(pw, return_value); #if defined(ZTS) && defined(_SC_GETPW_R_SIZE_MAX) && defined(HAVE_GETPWUID_R) efree(pwbuf); #endif