Skip to content

PHPC-1839 Fix accessing referenced strings #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/contrib/php_array_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,12 @@ PHP_ARRAY_FETCH_TYPE_MAP(zend_bool, bool)
*/
static inline
PAA_LONG php_array_zval_to_long(zval *z) {
try_again:
if (!z) { return 0; }
switch(Z_TYPE_P(z)) {
case IS_REFERENCE:
ZVAL_DEREF(z);
goto try_again;
case IS_NULL: return 0;
#ifdef ZEND_ENGINE_3
case IS_FALSE: return 0;
Expand Down Expand Up @@ -315,8 +319,12 @@ PHP_ARRAY_FETCH_TYPE_MAP(PAA_LONG, long)
*/
static inline
double php_array_zval_to_double(zval *z) {
try_again:
if (!z) { return 0.0; }
switch (Z_TYPE_P(z)) {
case IS_REFERENCE:
ZVAL_DEREF(z);
goto try_again;
case IS_NULL: return 0.0;
#ifdef ZEND_ENGINE_3
case IS_FALSE: return 0.0;
Expand Down Expand Up @@ -357,10 +365,14 @@ static inline
char *php_array_zval_to_string(zval *z, int *plen, zend_bool *pfree) {
*plen = 0;
*pfree = 0;
try_again:
if (!z) { return NULL; }
switch (Z_TYPE_P(z)) {
case IS_NULL:
return (char *)"";
case IS_REFERENCE:
ZVAL_DEREF(z);
goto try_again;
case IS_STRING:
*plen = Z_STRLEN_P(z);
return Z_STRVAL_P(z);
Expand Down
48 changes: 48 additions & 0 deletions tests/bson/bug1839-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
PHPC-1839: Referenced, out-of-scope, non-interned string in typeMap
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

function createTypemap()
{
// Assemble the string so as to not have an interned string
$rootValue = chr(ord('a')) . 'rray';
$documentValue = chr(ord('a')) . 'rray';

// Use a reference to this non-interned string in the type map
$typemap = ['root' => &$rootValue, 'document' => &$documentValue];

return $typemap;
}

$typemap = createTypemap();
$bson = MongoDB\BSON\fromPhp((object) []);

echo "Before:\n";
debug_zval_dump($typemap);

MongoDB\BSON\toPHP($bson, $typemap);

echo "After:\n";
debug_zval_dump($typemap);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Before:
array(2) refcount(2){
["root"]=>
string(5) "array" refcount(1)
["document"]=>
string(5) "array" refcount(1)
}
After:
array(2) refcount(2){
["root"]=>
string(5) "array" refcount(1)
["document"]=>
string(5) "array" refcount(1)
}
===DONE===
40 changes: 40 additions & 0 deletions tests/bson/bug1839-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
PHPC-1839: Referenced, local, non-interned string in typeMap
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

// Assemble the string so as to not have an interned string
$rootValue = chr(ord('a')) . 'rray';
$documentValue = chr(ord('a')) . 'rray';

$typemap = ['root' => &$rootValue, 'document' => &$documentValue];
$bson = MongoDB\BSON\fromPhp((object) []);

echo "Before:\n";
debug_zval_dump($typemap);

MongoDB\BSON\toPHP($bson, $typemap);

echo "After:\n";
debug_zval_dump($typemap);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Before:
array(2) refcount(2){
["root"]=>
&string(5) "array" refcount(1)
["document"]=>
&string(5) "array" refcount(1)
}
After:
array(2) refcount(2){
["root"]=>
&string(5) "array" refcount(1)
["document"]=>
&string(5) "array" refcount(1)
}
===DONE===
46 changes: 46 additions & 0 deletions tests/bson/bug1839-003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
PHPC-1839: Referenced, out-of-scope, interned string in typeMap
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

function createTypemap()
{
$rootValue = 'array';
$documentValue = 'array';

$typemap = ['root' => &$rootValue, 'document' => &$documentValue];

return $typemap;
}

$typemap = createTypemap();
$bson = MongoDB\BSON\fromPhp((object) []);

echo "Before:\n";
debug_zval_dump($typemap);

MongoDB\BSON\toPHP($bson, $typemap);

echo "After:\n";
debug_zval_dump($typemap);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Before:
array(2) refcount(2){
["root"]=>
string(5) "array" refcount(1)
["document"]=>
string(5) "array" refcount(1)
}
After:
array(2) refcount(2){
["root"]=>
string(5) "array" refcount(1)
["document"]=>
string(5) "array" refcount(1)
}
===DONE===
39 changes: 39 additions & 0 deletions tests/bson/bug1839-004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
PHPC-1839: Referenced, local, interned string in typeMap
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$rootValue = 'array';
$documentValue = 'array';

$typemap = ['root' => &$rootValue, 'document' => &$documentValue];
$bson = MongoDB\BSON\fromPhp((object) []);

echo "Before:\n";
debug_zval_dump($typemap);

MongoDB\BSON\toPHP($bson, $typemap);

echo "After:\n";
debug_zval_dump($typemap);

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Before:
array(2) refcount(2){
["root"]=>
&string(5) "array" refcount(1)
["document"]=>
&string(5) "array" refcount(1)
}
After:
array(2) refcount(2){
["root"]=>
&string(5) "array" refcount(1)
["document"]=>
&string(5) "array" refcount(1)
}
===DONE===