Skip to content

Promote warnings to errors in extract() #4588

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

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 4 additions & 4 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ static zend_long php_extract_skip(zend_array *arr, zend_array *symbol_table) /*
}
/* }}} */

/* {{{ proto int|null extract(array var_array [, int extract_type [, string prefix]])
/* {{{ proto int extract(array var_array [, int extract_type [, string prefix]])
Imports variables into symbol table from an array */
PHP_FUNCTION(extract)
{
Expand All @@ -2452,18 +2452,18 @@ PHP_FUNCTION(extract)
extract_type &= 0xff;

if (extract_type < EXTR_OVERWRITE || extract_type > EXTR_IF_EXISTS) {
php_error_docref(NULL, E_WARNING, "Invalid extract type");
zend_throw_error(NULL, "Invalid extract type");
return;
}

if (extract_type > EXTR_SKIP && extract_type <= EXTR_PREFIX_IF_EXISTS && ZEND_NUM_ARGS() < 3) {
php_error_docref(NULL, E_WARNING, "specified extract type requires the prefix parameter");
zend_throw_error(NULL, "Specified extract type requires the prefix parameter");
return;
}

if (prefix) {
if (ZSTR_LEN(prefix) && !php_valid_var_name(ZSTR_VAL(prefix), ZSTR_LEN(prefix))) {
php_error_docref(NULL, E_WARNING, "prefix is not a valid identifier");
zend_throw_error(NULL, "Prefix is not a valid identifier");
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function in_array($needle, array $haystack, bool $strict = false): bool {}
function array_search($needle, array $haystack, bool $strict = false) {}

/** @prefer-ref $arg */
function extract(array &$arg, int $extract_type = EXTR_OVERWRITE, string $prefix = ""): ?int {}
function extract(array &$arg, int $extract_type = EXTR_OVERWRITE, string $prefix = ""): int {}

function compact($var_name, ...$var_names): array {}

Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions_arginfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_array_search, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, strict, _IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_extract, 0, 1, IS_LONG, 1)
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_extract, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(ZEND_SEND_PREFER_REF, arg, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, extract_type, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0)
Expand Down
32 changes: 20 additions & 12 deletions ext/standard/tests/array/extract_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ echo "*** Testing Error Conditions ***\n";

/* Invalid second argument ( only 0-6 is valid) */
$arr = array(1);
var_dump( extract($arr, -1 . "wddr") );
var_dump( extract($arr, 7 , "wddr") );

try {
var_dump( extract($arr, -1 . "wddr") );
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

try {
var_dump( extract($arr, 7 , "wddr") );
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

/* Two Arguments, second as prefix but without prefix string as third argument */
var_dump( extract($arr,EXTR_PREFIX_IF_EXISTS) );
try {
var_dump( extract($arr,EXTR_PREFIX_IF_EXISTS) );
} catch (\Error $e) {
echo $e->getMessage() . "\n";
}

echo "Done\n";
?>
--EXPECTF--
*** Testing Error Conditions ***

Notice: A non well formed numeric value encountered in %s on line %d

Warning: extract(): Invalid extract type in %s on line %d
NULL

Warning: extract(): Invalid extract type in %s on line %d
NULL

Warning: extract(): specified extract type requires the prefix parameter in %s on line %d
NULL
Invalid extract type
Invalid extract type
Specified extract type requires the prefix parameter
Done
15 changes: 15 additions & 0 deletions ext/standard/tests/array/extract_error_variation1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Test extract() function - error condition - Invalid flag argument.
--FILE--
<?php
$a = ["1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five"];

try {
extract($a, 10000);
} catch (\Error $e) {
echo $e->getMessage();
}

?>
--EXPECT--
Invalid extract type
15 changes: 15 additions & 0 deletions ext/standard/tests/array/extract_error_variation2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Test extract() function - error condition - Prefix flag without supplying prefix.
--FILE--
<?php
$a = ["1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five"];

try {
extract($a, EXTR_PREFIX_ALL);
} catch (\Error $e) {
echo $e->getMessage();
}

?>
--EXPECT--
Specified extract type requires the prefix parameter
14 changes: 14 additions & 0 deletions ext/standard/tests/array/extract_error_variation3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Test extract() function - error condition - Invalid prefix.
--FILE--
<?php
$a = ["1" => "one", "2" => "two", "3" => "three", "4" => "four", "5" => "five"];

try {
extract($a, EXTR_PREFIX_ALL, '85bogus');
} catch (\Error $e) {
echo $e->getMessage();
}
?>
--EXPECT--
Prefix is not a valid identifier