Skip to content

Commit dd2e693

Browse files
authored
Merge pull request #77 from BitOne/GITHUB-76_children_items_not_linked
Fixes #76 on not maching children and item identifier
2 parents 4b01357 + e18f762 commit dd2e693

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.0.3 Ygramul (2018-08-13)
2+
- [GITHUB-76](https://github.com/BitOne/php-meminfo/issues/76) Fixes wrong non matching item reference from children, introduced in v1.0.2
3+
14
# 1.0.2 Ygramul (2018-08-08)
25

36
## Bug fixes

extension/php7/meminfo.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ void meminfo_hash_dump(php_stream *stream, HashTable *ht, zend_bool is_object, H
162162

163163
zend_hash_internal_pointer_reset_ex(ht, &pos);
164164
while (zval = zend_hash_get_current_data_ex(ht, &pos)) {
165+
char zval_id[16];
166+
167+
if (Z_TYPE_P(zval) == IS_INDIRECT) {
168+
zval = Z_INDIRECT_P(zval);
169+
}
170+
171+
if (Z_ISREF_P(zval)) {
172+
ZVAL_DEREF(zval);
173+
}
174+
175+
if (Z_TYPE_P(zval) == IS_OBJECT) {
176+
sprintf(zval_id, "%p", zval->value.obj);
177+
} else {
178+
sprintf(zval_id, "%p", zval);
179+
}
165180

166181
if (!first_child) {
167182
php_stream_printf(stream TSRMLS_CC, ",\n");
@@ -180,22 +195,22 @@ void meminfo_hash_dump(php_stream *stream, HashTable *ht, zend_bool is_object, H
180195

181196
escaped_property_name = meminfo_escape_for_json(property_name);
182197

183-
php_stream_printf(stream TSRMLS_CC, " \"%s\":\"%p\"", ZSTR_VAL(escaped_property_name), zval);
198+
php_stream_printf(stream TSRMLS_CC, " \"%s\":\"%s\"", ZSTR_VAL(escaped_property_name), zval_id);
184199

185200
zend_string_release(escaped_property_name);
186201
} else {
187202
zend_string * escaped_key;
188203

189204
escaped_key = meminfo_escape_for_json(ZSTR_VAL(key));
190205

191-
php_stream_printf(stream TSRMLS_CC, " \"%s\":\"%p\"", ZSTR_VAL(escaped_key), zval);
206+
php_stream_printf(stream TSRMLS_CC, " \"%s\":\"%s\"", ZSTR_VAL(escaped_key), zval_id);
192207

193208
zend_string_release(escaped_key);
194209
}
195210

196211
break;
197212
case HASH_KEY_IS_LONG:
198-
php_stream_printf(stream TSRMLS_CC, " \"%ld\":\"%p\"", num_key, zval);
213+
php_stream_printf(stream TSRMLS_CC, " \"%ld\":\"%s\"", num_key, zval_id);
199214
break;
200215
}
201216

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Check that all children items are properly linked through their identifiers
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('json')) die('skip json ext not loaded');
6+
?>
7+
--FILE--
8+
9+
<?php
10+
$dump = fopen('php://memory', 'rw');
11+
12+
class MyClass {
13+
public $myDeclaredVar;
14+
}
15+
16+
$myObject = new MyClass();
17+
$myObject->myDeclaredVar = "My declared var content";
18+
$myObject->myUndeclaredVar = "My undeclared content";
19+
$myObject->childObject = new stdClass();
20+
21+
$myArray = [
22+
"objectInArray" => new StdClass(),
23+
"arrayInArray" => ["foo" => "bar"]
24+
];
25+
26+
meminfo_dump($dump);
27+
28+
rewind($dump);
29+
$meminfoData = json_decode(stream_get_contents($dump), true);
30+
fclose($dump);
31+
32+
foreach($meminfoData['items'] as $item) {
33+
if (isset($item['children'])) {
34+
foreach($item['children'] as $child) {
35+
if (!isset($meminfoData['items'][$child])) {
36+
echo "Child $child not found!\n";
37+
}
38+
}
39+
}
40+
}
41+
?>
42+
--EXPECT--

0 commit comments

Comments
 (0)