php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12572 sort() doesn't reindex 1 element arrays at 0
Submitted: 2001-08-05 01:21 UTC Modified: 2002-08-17 14:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: adam at trachtenberg dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.2.2 OS: Redhat Linux 7.3
Private report: No CVE-ID:
 [2001-08-05 01:21 UTC] adam at trachtenberg dot com
sort() reindexes arrays of length > 1 at 0, but doesn't do
this for 1 element arrays. The following patch, fixes this
minor inconsistancy. Please review it carefully, my C is
rusty and I don't normally play with these files. It does
build and seem to run correctly for me, with limited testing.

-adam

Here is an example:

$array = array(2 => 'foo', 'bar');
sort($array);
print_r($array);

Array
(
    [0] => bar
    [1] => foo
)

But, 

$array = array(2 => 'foo');
sort($array);
print_r($array);

Array
(
    [2] => foo
)

--- zend_hash.c	Sun Aug  5 01:17:22 2001
+++ /home/adam/zend_hash.c	Sun Aug  5 00:54:21 2001
@@ -1097,6 +1097,13 @@
 	IS_CONSISTENT(ht);
 
 	if (ht->nNumOfElements <= 1) {	/* Doesn't require sorting */
+
	if (ht->nNumOfElements == 1 && renumber) { /* Renumber 1
element arrays to O */
+
		p = ht->pListHead;
+
		p->nKeyLength = 0;
+
		p->h = 0;
+
		ht->nNextFreeElement = 1;
+
		zend_hash_rehash(ht);
+
	}
 
	return SUCCESS;
 	}
 	arTmp = (Bucket **) pemalloc(ht->nNumOfElements *
sizeof(Bucket *), ht->persistent);


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-21 01:47 UTC] sniper@php.net
Reproduced with PHP 4.1.0RC1

 [2002-08-13 23:45 UTC] kalowsky@php.net
This bug has been fixed in CVS. You can grab a snapshot of the
CVS version at http://snaps.php.net/. In case this was a documentation 
problem, the fix will show up soon at http://www.php.net/manual/.
In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites.
Thank you for the report, and for helping us make PHP better.

I believe rodif_bl fixed this in the current CVS.  Give it a try if you can.
 [2002-08-14 01:17 UTC] adam at trachtenberg dot com
I just tested it with the latest CVS and it still happens.
 [2002-08-14 09:22 UTC] kalowsky@php.net
Updating the version
 [2002-08-14 11:44 UTC] adam at trachtenberg dot com
Updating OS; I'm now running RH 7.3
 [2002-08-17 02:09 UTC] kalowsky@php.net
adam you may want to throw this patch out onto phpdev for comments/inclusion options.
 [2002-08-17 02:39 UTC] adam at trachtenberg dot com
Okay. I will propose a slightly less complicated patch:

Index: zend_hash.c
===================================================================
RCS file: /repository/Zend/zend_hash.c,v
retrieving revision 1.86
diff -u -u -r1.86 zend_hash.c
--- zend_hash.c	1 Aug 2002 16:03:21 -0000	1.86
+++ zend_hash.c	17 Aug 2002 06:38:14 -0000
@@ -1115,7 +1115,7 @@
 
 	IS_CONSISTENT(ht);
 
-	if (ht->nNumOfElements <= 1) {	/* Doesn't require sorting */
+	if (ht->nNumOfElements < 1) {	/* Doesn't require sorting */
 		return SUCCESS;
 	}
 	arTmp = (Bucket **) pemalloc(ht->nNumOfElements * sizeof(Bucket *), ht->persistent);
 [2002-08-17 14:51 UTC] sniper@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 07:02:14 2014 UTC