[php-src] master: Cleanup php_posix_passwd_to_array() (#18496)

From: Date: Sun, 04 May 2025 18:17:44 +0000
Subject: [php-src] master: Cleanup php_posix_passwd_to_array() (#18496)
Groups: php.cvs 
Request: Send a blank email to [email protected] to get a copy of this message
Author: Niels Dossche (nielsdos)
Committer: GitHub (web-flow)
Pusher: nielsdos
Date: 2025-05-04T20:07:06+02:00

Commit: https://github.com/php/php-src/commit/1fa076e187f06738a2b267a9a801af6ab27d8677
Raw diff: https://github.com/php/php-src/commit/1fa076e187f06738a2b267a9a801af6ab27d8677.diff

Cleanup php_posix_passwd_to_array() (#18496)

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.

Changed paths:
  M  ext/posix/posix.c


Diff:

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


Thread (1 message)

  • Niels Dossche via GitHub
« previous php.cvs (#134063) next »