Skip to content

Commit db5f85e

Browse files
committed
Cleanup argument handling of Zend functions and methods
1 parent 1c967df commit db5f85e

23 files changed

+199
-155
lines changed

Zend/tests/002.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,26 @@ try {
9696
echo "Done\n";
9797
?>
9898
--EXPECTF--
99-
func_get_arg(): Argument #1 ($arg_num) must be greater than or equal to 0
100-
func_get_arg(): Argument 0 not passed to function
101-
func_get_arg(): Argument 1 not passed to function
102-
func_get_arg(): Argument #1 ($arg_num) must be greater than or equal to 0
99+
func_get_arg(): Argument #1 ($argument_number) must be greater than or equal to 0
100+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
101+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
102+
func_get_arg(): Argument #1 ($argument_number) must be greater than or equal to 0
103103
int(10)
104-
func_get_arg(): Argument 1 not passed to function
104+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
105105
int(1)
106-
func_get_arg(): Argument 1 not passed to function
106+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
107107
Exception: Too few arguments to function test2(), 0 passed in %s002.php on line %d and exactly 1 expected
108108
int(1)
109109
int(2)
110-
func_get_arg(): Argument 2 not passed to function
111-
func_get_arg(): Argument #1 ($arg_num) must be greater than or equal to 0
112-
func_get_arg(): Argument 0 not passed to function
113-
func_get_arg(): Argument 1 not passed to function
110+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
111+
func_get_arg(): Argument #1 ($argument_number) must be greater than or equal to 0
112+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
113+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
114114
Exception: Too few arguments to function test3(), 1 passed in %s on line %d and exactly 2 expected
115115
int(1)
116116
int(2)
117-
func_get_arg(): Argument 2 not passed to function
117+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
118118
int(1)
119-
func_get_arg(): Argument 1 not passed to function
119+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function
120120
func_get_arg() cannot be called from the global scope
121121
Done

Zend/tests/004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var_dump(strncmp("qwerty", "qwerty123", 7));
1616
?>
1717
--EXPECT--
1818
int(0)
19-
strncmp(): Argument #3 ($len) must be greater than or equal to 0
19+
strncmp(): Argument #3 ($length) must be greater than or equal to 0
2020
int(0)
2121
int(0)
2222
int(-1)

Zend/tests/006.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var_dump(strncasecmp("01", "01", 1000));
1919

2020
?>
2121
--EXPECT--
22-
strncasecmp(): Argument #3 ($len) must be greater than or equal to 0
22+
strncasecmp(): Argument #3 ($length) must be greater than or equal to 0
2323
int(0)
2424
int(-3)
2525
int(0)

Zend/tests/008.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ var_dump(define("test const", 3));
1616
var_dump(define("test const", 3));
1717
var_dump(define("test", array(1)));
1818
var_dump(define("test1", fopen(__FILE__, 'r')));
19+
20+
try {
1921
var_dump(define("test2", new stdclass));
22+
} catch (TypeError $exception) {
23+
echo $exception->getMessage() . "\n";
24+
}
2025

2126
var_dump(constant(" "));
2227
var_dump(constant("[[["));
@@ -37,9 +42,7 @@ Notice: Constant test const already defined in %s on line %d
3742
bool(false)
3843
bool(true)
3944
bool(true)
40-
41-
Warning: Constants may only evaluate to scalar values, arrays or resources in %s on line %d
42-
bool(false)
45+
define(): Argument #2 ($value) must be of type bool|int|float|string|array|resource|null, stdClass given
4346
int(1)
4447
int(2)
4548
int(3)

Zend/tests/020.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ try {
2626
?>
2727
--EXPECT--
2828
func_get_arg() cannot be called from the global scope
29-
func_get_arg(): Argument 1 not passed to function
29+
func_get_arg(): Argument #1 ($argument_number) must be less than the number of the arguments passed to the currently executed function

Zend/tests/bug37811.phpt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ class TestClass
1111
}
1212
}
1313

14-
define("Bar",new TestClass);
14+
define("Bar", new TestClass);
1515
var_dump(Bar);
16-
define("Baz",new stdClass);
17-
var_dump(Baz);
1816

19-
?>
20-
===DONE===
21-
--EXPECTF--
22-
string(3) "Foo"
17+
try {
18+
define("Baz", new stdClass);
19+
} catch (TypeError $exception) {
20+
echo $exception->getMessage() . "\n";
21+
}
2322

24-
Warning: Constants may only evaluate to scalar values, arrays or resources in %sbug37811.php on line %d
23+
try {
24+
var_dump(Baz);
25+
} catch (Error $exception) {
26+
echo $exception->getMessage() . "\n";
27+
}
2528

26-
Fatal error: Uncaught Error: Undefined constant "Baz" in %s:%d
27-
Stack trace:
28-
#0 {main}
29-
thrown in %s on line %d
29+
?>
30+
--EXPECT--
31+
string(3) "Foo"
32+
define(): Argument #2 ($value) must be of type bool|int|float|string|array|resource|null, stdClass given
33+
Undefined constant "Baz"

Zend/tests/bug44827.phpt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
Bug #44827 (define() allows :: in constant names)
33
--FILE--
44
<?php
5-
define('foo::bar', 1);
6-
define('::', 1);
7-
?>
8-
--EXPECTF--
9-
Warning: Class constants cannot be defined or redefined in %sbug44827.php on line %d
105

11-
Warning: Class constants cannot be defined or redefined in %sbug44827.php on line %d
6+
try {
7+
define('foo::bar', 1);
8+
} catch (ValueError $exception) {
9+
echo $exception->getMessage() . "\n";
10+
}
11+
12+
try {
13+
define('::', 1);
14+
} catch (ValueError $exception) {
15+
echo $exception->getMessage() . "\n";
16+
}
17+
18+
?>
19+
--EXPECT--
20+
define(): Argument #1 ($constant_name) cannot be a class constant
21+
define(): Argument #1 ($constant_name) cannot be a class constant

Zend/tests/bug72162.phpt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ Bug #72162 (use-after-free - error_reporting)
44
<?php
55
error_reporting(E_ALL);
66
$var11 = new StdClass();
7-
$var16 = error_reporting($var11);
7+
8+
try {
9+
$var16 = error_reporting($var11);
10+
} catch (TypeError $exception) {
11+
echo $exception->getMessage() . "\n";
12+
}
813
?>
9-
--EXPECTF--
10-
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in %s:%d
11-
Stack trace:
12-
#0 %s(%d): error_reporting(Object(stdClass))
13-
#1 {main}
14-
thrown in %s on line %d
14+
--EXPECT--
15+
error_reporting(): Argument #1 ($error_level) must be of type ?int, stdClass given

Zend/tests/closure_040.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ $a = new A(20);
2424
$ca = $a->getIncrementor();
2525
$cas = $a->getStaticIncrementor();
2626

27-
$ca->bindTo($a, array());
27+
try {
28+
$ca->bindTo($a, array());
29+
} catch (TypeError $exception) {
30+
echo $exception->getMessage() . "\n";
31+
}
32+
2833
$cas->bindTo($a, 'A');
2934

3035
?>

Zend/tests/constant_arrays.phpt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@ $y[0] = 3;
2424
var_dump($x, $y, QUX);
2525

2626
// ensure objects not allowed in arrays
27-
var_dump(define('ELEPHPANT', [new StdClass]));
27+
try {
28+
define('ELEPHPANT', [new StdClass]);
29+
} catch (TypeError $exception) {
30+
echo $exception->getMessage() . "\n";
31+
}
2832

2933
// ensure recursion doesn't crash
3034
$recursive = [];
3135
$recursive[0] = &$recursive;
32-
var_dump(define('RECURSION', $recursive));
36+
37+
try {
38+
define('RECURSION', $recursive);
39+
} catch (ValueError $exception) {
40+
echo $exception->getMessage() . "\n";
41+
}
3342
--EXPECTF--
3443
array(4) {
3544
[0]=>
@@ -92,9 +101,5 @@ array(1) {
92101
[0]=>
93102
int(7)
94103
}
95-
96-
Warning: Constants may only evaluate to scalar values, arrays or resources in %s on line %d
97-
bool(false)
98-
99-
Warning: Constants cannot be recursive arrays in %s on line %d
100-
bool(false)
104+
define(): Argument #2 ($value) must be of type bool|int|float|string|array|resource|null, stdClass given
105+
define(): Argument #2 ($value) cannot be a recursive array

Zend/tests/constants_002.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ Defining constants with non-scalar values
33
--FILE--
44
<?php
55

6-
define('foo', new stdClass);
6+
try {
7+
define('foo', new stdClass);
8+
} catch (TypeError $exception) {
9+
echo $exception->getMessage() . "\n";
10+
}
11+
712
try {
813
var_dump(foo);
914
} catch (Error $e) {
@@ -14,7 +19,7 @@ define('foo', fopen(__FILE__, 'r'));
1419
var_dump(foo);
1520

1621
?>
17-
--EXPECTF--
18-
Warning: Constants may only evaluate to scalar values, arrays or resources in %s on line %d
22+
--EXPECT--
23+
define(): Argument #2 ($value) must be of type bool|int|float|string|array|resource|null, stdClass given
1924
Undefined constant "foo"
2025
resource(5) of type (stream)

Zend/zend_builtin_functions.c

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ ZEND_FUNCTION(func_get_arg)
197197
arg_count = ZEND_CALL_NUM_ARGS(ex);
198198

199199
if ((zend_ulong)requested_offset >= arg_count) {
200-
zend_throw_error(NULL, "func_get_arg(): Argument " ZEND_LONG_FMT " not passed to function", requested_offset);
200+
zend_argument_value_error(1, "must be less than the number of the arguments passed to the currently executed function");
201201
RETURN_THROWS();
202202
}
203203

@@ -369,17 +369,19 @@ ZEND_FUNCTION(strncasecmp)
369369
/* {{{ Return the current error_reporting level, and if an argument was passed - change to the new level */
370370
ZEND_FUNCTION(error_reporting)
371371
{
372-
zval *err = NULL;
372+
zend_long err;
373+
zend_bool err_is_null = 1;
373374
int old_error_reporting;
374375

375376
ZEND_PARSE_PARAMETERS_START(0, 1)
376377
Z_PARAM_OPTIONAL
377-
Z_PARAM_ZVAL(err)
378+
Z_PARAM_LONG_OR_NULL(err, err_is_null)
378379
ZEND_PARSE_PARAMETERS_END();
379380

380381
old_error_reporting = EG(error_reporting);
381-
if (ZEND_NUM_ARGS() != 0) {
382-
zend_string *new_val = zval_try_get_string(err);
382+
383+
if (!err_is_null) {
384+
zend_string *new_val = zend_long_to_str(err);
383385
if (UNEXPECTED(!new_val)) {
384386
RETURN_THROWS();
385387
}
@@ -410,19 +412,15 @@ ZEND_FUNCTION(error_reporting)
410412
}
411413

412414
p->value = new_val;
413-
if (Z_TYPE_P(err) == IS_LONG) {
414-
EG(error_reporting) = Z_LVAL_P(err);
415-
} else {
416-
EG(error_reporting) = atoi(ZSTR_VAL(p->value));
417-
}
415+
EG(error_reporting) = err;
418416
} while (0);
419417
}
420418

421419
RETVAL_LONG(old_error_reporting);
422420
}
423421
/* }}} */
424422

425-
static int validate_constant_array(HashTable *ht) /* {{{ */
423+
static int validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
426424
{
427425
int ret = 1;
428426
zval *val;
@@ -434,16 +432,18 @@ static int validate_constant_array(HashTable *ht) /* {{{ */
434432
if (Z_TYPE_P(val) == IS_ARRAY) {
435433
if (Z_REFCOUNTED_P(val)) {
436434
if (Z_IS_RECURSIVE_P(val)) {
437-
zend_error(E_WARNING, "Constants cannot be recursive arrays");
435+
zend_argument_value_error(argument_number, "cannot be a recursive array");
438436
ret = 0;
439437
break;
440-
} else if (!validate_constant_array(Z_ARRVAL_P(val))) {
438+
} else if (!validate_constant_array_argument(Z_ARRVAL_P(val), argument_number)) {
441439
ret = 0;
442440
break;
443441
}
444442
}
445443
} else if (Z_TYPE_P(val) != IS_STRING && Z_TYPE_P(val) != IS_RESOURCE) {
446-
zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
444+
zend_argument_type_error(2, "must be of type bool|int|float|string|array|resource|null, %s given",
445+
zend_zval_type_name(val)
446+
);
447447
ret = 0;
448448
break;
449449
}
@@ -485,25 +485,16 @@ ZEND_FUNCTION(define)
485485
{
486486
zend_string *name;
487487
zval *val, val_free;
488-
zend_bool non_cs = 0;
489488
zend_constant c;
490489

491-
ZEND_PARSE_PARAMETERS_START(2, 3)
490+
ZEND_PARSE_PARAMETERS_START(2, 2)
492491
Z_PARAM_STR(name)
493492
Z_PARAM_ZVAL(val)
494-
Z_PARAM_OPTIONAL
495-
Z_PARAM_BOOL(non_cs)
496493
ZEND_PARSE_PARAMETERS_END();
497494

498495
if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
499-
zend_error(E_WARNING, "Class constants cannot be defined or redefined");
500-
RETURN_FALSE;
501-
}
502-
503-
if (non_cs) {
504-
zend_error(E_WARNING,
505-
"define(): Declaration of case-insensitive constants is no longer supported");
506-
RETURN_FALSE;
496+
zend_argument_value_error(1, "cannot be a class constant");
497+
RETURN_THROWS();
507498
}
508499

509500
ZVAL_UNDEF(&val_free);
@@ -519,8 +510,8 @@ ZEND_FUNCTION(define)
519510
break;
520511
case IS_ARRAY:
521512
if (Z_REFCOUNTED_P(val)) {
522-
if (!validate_constant_array(Z_ARRVAL_P(val))) {
523-
RETURN_FALSE;
513+
if (!validate_constant_array_argument(Z_ARRVAL_P(val), 2)) {
514+
RETURN_THROWS();
524515
} else {
525516
copy_constant_array(&c.value, val);
526517
goto register_constant;
@@ -534,9 +525,11 @@ ZEND_FUNCTION(define)
534525
}
535526
/* no break */
536527
default:
537-
zend_error(E_WARNING, "Constants may only evaluate to scalar values, arrays or resources");
538528
zval_ptr_dtor(&val_free);
539-
RETURN_FALSE;
529+
zend_argument_type_error(2, "must be of type bool|int|float|string|array|resource|null, %s given",
530+
zend_zval_type_name(val)
531+
);
532+
RETURN_THROWS();
540533
}
541534

542535
ZVAL_COPY(&c.value, val);
@@ -1436,7 +1429,7 @@ ZEND_FUNCTION(get_resources)
14361429
zend_ulong index;
14371430
zval *val;
14381431

1439-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &type) == FAILURE) {
1432+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &type) == FAILURE) {
14401433
RETURN_THROWS();
14411434
}
14421435

@@ -1460,7 +1453,7 @@ ZEND_FUNCTION(get_resources)
14601453
int id = zend_fetch_list_dtor_id(ZSTR_VAL(type));
14611454

14621455
if (id <= 0) {
1463-
zend_error(E_WARNING, "get_resources(): Unknown resource type '%s'", ZSTR_VAL(type));
1456+
zend_error(E_WARNING, "get_resources(): Unknown resource type \"%s\"", ZSTR_VAL(type));
14641457
RETURN_FALSE;
14651458
}
14661459

0 commit comments

Comments
 (0)