Skip to content

Commit 91da96b

Browse files
committed
MFH: change E_NOTICE to E_ERROR when using a class constant from non-existent class
(noticed by Jani) add tests
1 parent 20b5c66 commit 91da96b

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

Zend/tests/class_constants_001.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
class constants basic tests
3+
--FILE--
4+
<?php
5+
6+
class test {
7+
const val = "string";
8+
const val2 = 1;
9+
}
10+
11+
var_dump(test::val);
12+
var_dump(test::val2);
13+
14+
var_dump(test::val3);
15+
16+
echo "Done\n";
17+
?>
18+
--EXPECTF--
19+
string(6) "string"
20+
int(1)
21+
22+
Fatal error: Undefined class constant 'val3' in %s on line %d

Zend/tests/class_constants_002.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
class constants as default function arguments
3+
--FILE--
4+
<?php
5+
6+
class test {
7+
const val = 1;
8+
}
9+
10+
function foo($v = test::val) {
11+
var_dump($v);
12+
}
13+
14+
function bar($b = NoSuchClass::val) {
15+
var_dump($b);
16+
}
17+
18+
foo();
19+
foo(5);
20+
21+
bar(10);
22+
bar();
23+
24+
echo "Done\n";
25+
?>
26+
--EXPECTF--
27+
int(1)
28+
int(5)
29+
int(10)
30+
31+
Fatal error: Class 'NoSuchClass' not found in %s on line %d

Zend/tests/class_constants_003.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
class constants as default function arguments and dynamically loaded classes
3+
--FILE--
4+
<?php
5+
6+
$class_data = <<<DATA
7+
<?php
8+
class test {
9+
const val = 1;
10+
}
11+
?>
12+
DATA;
13+
14+
$filename = dirname(__FILE__)."/cc003.dat";
15+
file_put_contents($filename, $class_data);
16+
17+
function foo($v = test::val) {
18+
var_dump($v);
19+
}
20+
21+
include $filename;
22+
23+
foo();
24+
foo(5);
25+
26+
unlink($filename);
27+
28+
echo "Done\n";
29+
?>
30+
--EXPECTF--
31+
int(1)
32+
int(5)
33+
Done

Zend/zend_constants.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,16 @@ ZEND_API int zend_get_constant_ex(char *name, uint name_len, zval *result, zend_
259259
retval = 0;
260260
}
261261
}
262-
efree(class_name);
263262

264263
if (retval && ce) {
265264
if (zend_hash_find(&((*ce)->constants_table), constant_name, const_name_len+1, (void **) &ret_constant) != SUCCESS) {
266265
retval = 0;
267266
}
268267
} else {
268+
zend_error(E_ERROR, "Class '%s' not found", class_name);
269269
retval = 0;
270270
}
271+
efree(class_name);
271272

272273
if (retval) {
273274
zval_update_constant(ret_constant, (void*)1 TSRMLS_CC);

0 commit comments

Comments
 (0)