[php-src] PHP-8.5: ext/ldap: fix ldap_explode_dn result ordering

arshidkv12 via Côme Chilliet <[email protected]>
Newsgroups gmane.comp.php.cvs.general
Message-ID <[email protected]>
Author: arshidkv12 (arshidkv12)
Committer: Côme Chilliet
Pusher: MCMic
Date: 2026-07-13T08:40:42+02:00

Commit: https://github.com/php/php-src/commit/7809f94b1f5f29031eb12551438da5c5db432a55
Raw diff: https://github.com/php/php-src/commit/7809f94b1f5f29031eb12551438da5c5db432a55.diff

ext/ldap: fix ldap_explode_dn result ordering

Closes GH-22550

Changed paths:
  M  ext/ldap/ldap.c
  M  ext/ldap/tests/ldap_explode_dn.phpt


Diff:

diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c
index 2c9fdb8e3761..8c0a04729deb 100644
--- a/ext/ldap/ldap.c
+++ b/ext/ldap/ldap.c
@@ -2209,6 +2209,8 @@ PHP_FUNCTION(ldap_explode_dn)
 	zend_long with_attrib;
 	char *dn, **ldap_value;
 	size_t dn_len;
+	int i, count;
+
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) {
 		RETURN_THROWS();
@@ -2220,11 +2222,15 @@ PHP_FUNCTION(ldap_explode_dn)
 	}
 
 	array_init(return_value);
-	int i;
-	for (i = 0; ldap_value[i] != NULL; i++) {
+	i = 0;
+	while (ldap_value[i] != NULL) i++;
+	count = i;
+
+	add_assoc_long(return_value, "count", count);
+
+	for (i = 0; i < count; i++) {
 		add_index_string(return_value, i, ldap_value[i]);
 	}
-	add_assoc_long(return_value, "count", i);
 
 	ldap_memvfree((void **)ldap_value);
 }
diff --git a/ext/ldap/tests/ldap_explode_dn.phpt b/ext/ldap/tests/ldap_explode_dn.phpt
index 047078c7beb7..2012506c3ef0 100644
--- a/ext/ldap/tests/ldap_explode_dn.phpt
+++ b/ext/ldap/tests/ldap_explode_dn.phpt
@@ -34,16 +34,18 @@ echo "Done\n";
 ?>
 --EXPECT--
 array(4) {
+  ["count"]=>
+  int(3)
   [0]=>
   string(6) "cn=bob"
   [1]=>
   string(10) "dc=example"
   [2]=>
   string(6) "dc=com"
-  ["count"]=>
-  int(3)
 }
 array(5) {
+  ["count"]=>
+  int(4)
   [0]=>
   string(6) "cn=bob"
   [1]=>
@@ -52,20 +54,20 @@ array(5) {
   string(10) "dc=example"
   [3]=>
   string(6) "dc=com"
-  ["count"]=>
-  int(4)
 }
 array(4) {
+  ["count"]=>
+  int(3)
   [0]=>
   string(3) "bob"
   [1]=>
   string(7) "example"
   [2]=>
   string(3) "com"
-  ["count"]=>
-  int(3)
 }
 array(5) {
+  ["count"]=>
+  int(4)
   [0]=>
   string(3) "bob"
   [1]=>
@@ -74,8 +76,6 @@ array(5) {
   string(7) "example"
   [3]=>
   string(3) "com"
-  ["count"]=>
-  int(4)
 }
 bool(false)
 bool(false)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.