Skip to content

Check parameters on compact() and throw TypeError if not string or array of strings #6921

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
Show file tree
Hide file tree
Changes from 4 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
9 changes: 6 additions & 3 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ PHP_FUNCTION(extract)
}
/* }}} */

static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_value, zval *entry) /* {{{ */
static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_value, zval *entry, uint32_t pos) /* {{{ */
{
zval *value_ptr, data;

Expand Down Expand Up @@ -2475,11 +2475,14 @@ static void php_compact_var(HashTable *eg_active_symbol_table, zval *return_valu
Z_PROTECT_RECURSION_P(entry);
}
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(entry), value_ptr) {
php_compact_var(eg_active_symbol_table, return_value, value_ptr);
php_compact_var(eg_active_symbol_table, return_value, value_ptr, pos);
} ZEND_HASH_FOREACH_END();
if (Z_REFCOUNTED_P(entry)) {
Z_UNPROTECT_RECURSION_P(entry);
}
} else {
zend_throw_error(zend_ce_type_error, "Parameter %d in compact() must be string or array of strings", pos);
return;
}
}
/* }}} */
Expand Down Expand Up @@ -2512,7 +2515,7 @@ PHP_FUNCTION(compact)
}

for (i = 0; i < num_args; i++) {
php_compact_var(symbol_table, return_value, &args[i]);
php_compact_var(symbol_table, return_value, &args[i], i + 1);
}
}
/* }}} */
Expand Down
16 changes: 16 additions & 0 deletions ext/standard/tests/array/compact.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ $location_vars = array("c\\u0327ity", "state");

$result = compact("event", $location_vars);
var_dump($result);

try {
$result = compact(true);
} catch (TypeError $e) {
echo "TypeError: ".$e->getMessage()."\n";
}

try {
$foo = 'bar';
$bar = 'baz';
$result = compact($foo, [true]);
} catch (TypeError $e) {
echo "TypeError: ".$e->getMessage();
}
?>
--EXPECTF--
Warning: compact(): Undefined variable $c\u0327ity in %s on line %d
Expand All @@ -20,3 +34,5 @@ array(2) {
["state"]=>
string(2) "CA"
}
TypeError: Parameter 1 in compact() must be string or array of strings
TypeError: Parameter 2 in compact() must be string or array of strings
15 changes: 1 addition & 14 deletions ext/standard/tests/array/compact_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ $f="string";
var_dump (compact(array("a", "b", "c", "d", "e", "f")));
// simple parameter test
var_dump (compact("a", "b", "c", "d", "e", "f"));
var_dump (compact(array("keyval"=>"a", "b"=>"b", "c"=>1)));

// cases which should not yield any output.
var_dump (compact(array(10, 0.3, true, array(20), NULL)));
var_dump (compact(10, 0.3, true, array(20), NULL));
var_dump (compact(array("g")));
var_dump (compact(array("keyval"=>"a", "b"=>"b")));

echo "Done";
?>
Expand Down Expand Up @@ -70,12 +65,4 @@ array(2) {
["b"]=>
float(0.2)
}
array(0) {
}
array(0) {
}

Warning: compact(): Undefined variable $g in %s on line %d
array(0) {
}
Done