Skip to content

Commit 0f00170

Browse files
committed
Fixed bug #61453.
The "hash" function used strncpy on data that would have NUL bytes, ending the copy prematurely and causing collisions between objects.
1 parent 1e18f11 commit 0f00170

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ PHP NEWS
9696
ReflectionMethod::invokeArgs()). (Laruence)
9797

9898
- SPL:
99+
. Fixed bug #61453 (SplObjectStorage does not identify objects correctly).
100+
(Gustavo)
99101
. Fixed bug #61347 (inconsistent isset behavior of Arrayobject). (Laruence)
100102

101103
- Standard:

ext/spl/spl_observer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ static char *spl_object_storage_get_hash(spl_SplObjectStorage *intern, zval *thi
146146

147147
return (char*)&Z_OBJVAL_P(obj);
148148
#else
149-
char *hash = emalloc((hash_len+1)*sizeof(char));
149+
char *hash = emalloc(hash_len + 1);
150150

151151
zend_object_value zvalue;
152152
memset(&zvalue, 0, sizeof(zend_object_value));
153153
zvalue.handle = Z_OBJ_HANDLE_P(obj);
154154
zvalue.handlers = Z_OBJ_HT_P(obj);
155155

156-
strncpy(hash, (char *)&zvalue, hash_len);
156+
memcpy(hash, (char *)&zvalue, hash_len);
157157
hash[hash_len] = 0;
158158

159159
if (hash_len_ptr) {

ext/spl/tests/bug61453.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #61453: SplObjectStorage does not identify objects correctly
3+
--FILE--
4+
<?php
5+
$limit = 1000;
6+
$objects = new SplObjectStorage;
7+
for($i = 0; $i < $limit; $i++){
8+
$object = new StdClass;
9+
10+
if(isset($objects[$object])){
11+
die("this should never happen, but did after $i iteration");
12+
}
13+
14+
$objects[$object] = 1;
15+
}
16+
?>
17+
==DONE==
18+
--EXPECT--
19+
==DONE==

0 commit comments

Comments
 (0)