Skip to content

Commit

Permalink
Fix out of bound writes to SafeArray data
Browse files Browse the repository at this point in the history
Converting PHP arrays to Variants originally supported almost arbitrary
numeric arrays, possibly filling gaps with NULL values.  This is broken
as of PHP 7.0.0[1] so that the SafeArray only has as many elements as
the PHP array.  Thus, unless the array is a list, some elements may be
written outside of the SafeArray data.

To avoid breaking userland code after that long time, we do not restore
the original behavior, but instead only suppress the erroneous writes.

To avoid the need to split the regression test for 32bit and 64bit
Windows, we suppress the "max number 4294967295 of elements in safe
array exceeded" warning, which only occurs for 64bit versions.

[1] <php@c865472>

Closes phpGH-16309.
  • Loading branch information
cmb69 committed Oct 9, 2024
1 parent e49d732 commit 1ee56bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.2.26

- COM:
. Fixed out of bound writes to SafeArray data. (cmb)

- Curl:
. Fixed bug GH-16302 (CurlMultiHandle holds a reference to CurlHandle if
curl_multi_add_handle fails). (timwolla)
Expand Down
7 changes: 4 additions & 3 deletions ext/com_dotnet/com_variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

/* create an automation SafeArray from a PHP array.
* Only creates a single-dimensional array of variants.
* The keys of the PHP hash MUST be numeric. If the array
* is sparse, then the gaps will be filled with NULL variants */
* The keys of the PHP hash MUST be numeric. */
static void safe_array_from_zval(VARIANT *v, zval *z, int codepage)
{
SAFEARRAY *sa = NULL;
Expand Down Expand Up @@ -71,7 +70,9 @@ static void safe_array_from_zval(VARIANT *v, zval *z, int codepage)
break;
}
zend_hash_get_current_key_ex(Z_ARRVAL_P(z), &strindex, &intindex, &pos);
php_com_variant_from_zval(&va[intindex], item, codepage);
if (intindex < bound.cElements) {
php_com_variant_from_zval(&va[intindex], item, codepage);
}
}

/* Unlock it and stuff it into our variant */
Expand Down
30 changes: 30 additions & 0 deletions ext/com_dotnet/tests/variant_variation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Testing variant arrays
--EXTENSIONS--
com_dotnet
--FILE--
<?php
$arrays = [
"order" => [2 => 1, 1 => 2, 0 => 3],
"off" => [2 => 1, 1 => 2, 3],
"negative" => [-1 => 42],
];
foreach ($arrays as $desc => $array) {
echo "-- $desc --\n";
$v = new variant($array);
foreach ($v as $val) {
var_dump($val);
}
}
?>
--EXPECTF--
-- order --
int(3)
int(2)
int(1)
-- off --
NULL
int(2)
int(1)
-- negative --
%ANULL

0 comments on commit 1ee56bd

Please sign in to comment.