Skip to content

Commit

Permalink
Merge pull request #71 from BitOne/GITHUB-70_duplicate_referenced_sca…
Browse files Browse the repository at this point in the history
…lar_php7

Fixes #70 by indirecting before dereferecing zval
  • Loading branch information
BitOne committed Aug 8, 2018
2 parents 63fd85a + 7b7ce54 commit 79dfa7c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
15 changes: 9 additions & 6 deletions extension/php7/meminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ void meminfo_hash_dump(php_stream *stream, HashTable *ht, zend_bool is_object, H
void meminfo_zval_dump(php_stream * stream, char * frame_label, zend_string * symbol_name, zval * zv, HashTable *visited_items, int *first_element)
{
char zval_id[16];

if (Z_TYPE_P(zv) == IS_INDIRECT) {
zv = Z_INDIRECT_P(zv);
}

if (Z_ISREF_P(zv)) {
ZVAL_DEREF(zv);
}

sprintf(zval_id, "%p", zv);

if (meminfo_visit_item(zval_id, visited_items)) {
Expand All @@ -225,12 +234,6 @@ void meminfo_zval_dump(php_stream * stream, char * frame_label, zend_string * sy
*first_element = 0;
}

if (Z_TYPE_P(zv) == IS_INDIRECT) {
zv = Z_INDIRECT_P(zv);
} else if (Z_ISREF_P(zv)) {
ZVAL_DEREF(zv);
}

php_stream_printf(stream TSRMLS_CC, " \"%s\" : {\n", zval_id);
php_stream_printf(stream TSRMLS_CC, " \"type\" : \"%s\",\n", zend_get_type_by_const(Z_TYPE_P(zv)));
php_stream_printf(stream TSRMLS_CC, " \"size\" : \"%ld\",\n", meminfo_get_element_size(zv));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
Check that a scalar with multiple references is detected only once by meminfo.
--SKIPIF--
<?php
if (!extension_loaded('json')) die('skip json ext not loaded');
?>
--FILE--
<?php
$dump = fopen('php://memory', 'rw');

$a = "foo";

$b =& $a;

$c = strlen($b);

meminfo_dump($dump);

rewind($dump);
$meminfoData = json_decode(stream_get_contents($dump), true);
fclose($dump);

$objectsCount = 0;
$bReferenceFound = false;

foreach($meminfoData['items'] as $item) {
if (isset($item['symbol_name']) && $item['symbol_name'] === 'b') {
$bReferenceFound = true;
}
}

if ($bReferenceFound) {
echo "'b' reference found although it shouldn't be accounted.\n";
} else {
echo "Reference 'b' not found.\n";
}
?>
--EXPECT--
Reference 'b' not found.

0 comments on commit 79dfa7c

Please sign in to comment.