Skip to content

Commit

Permalink
Fixed bug #75546
Browse files Browse the repository at this point in the history
By respecting the SILENT flag when checking the visibility of a
class constant.
  • Loading branch information
DanielCiochiu authored and nikic committed Feb 12, 2019
1 parent 8e34de4 commit 07877c4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PHP NEWS
- Core:
. Fixed bug #77589 (Core dump using parse_ini_string with numeric sections).
(Laruence)
. Fixed bug #75546 (function "defined" should ignore class constant
visibility). (Daniel Ciochiu)

- Exif:
. Fixed bug #77564 (Memory leak in exif_process_IFD_TAG). (Ben Ramsey)
Expand Down
4 changes: 3 additions & 1 deletion Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
ret_constant = NULL;
} else {
if (!zend_verify_const_access(c, scope)) {
zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
zend_throw_error(NULL, "Cannot access %s const %s::%s", zend_visibility_string(Z_ACCESS_FLAGS(c->value)), ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
}
goto failure;
}
ret_constant = &c->value;
Expand Down
11 changes: 4 additions & 7 deletions ext/standard/tests/general_functions/bug72920.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ class Foo {
private const C1 = "a";
}

try {
var_dump(constant('Foo::C1'));
} catch (Error $e) {
var_dump($e->getMessage());
}
--EXPECT--
string(35) "Cannot access private const Foo::C1"
var_dump(constant('Foo::C1'));
--EXPECTF--
Warning: constant(): Couldn't find constant Foo::C1 in %s on line %d
NULL
6 changes: 1 addition & 5 deletions tests/classes/constants_visibility_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ constant('A::protectedConst');
string(14) "protectedConst"
string(14) "protectedConst"

Fatal error: Uncaught Error: Cannot access protected const A::protectedConst in %s:14
Stack trace:
#0 %s(14): constant('A::protectedCon...')
#1 {main}
thrown in %s on line 14
Warning: constant(): Couldn't find constant A::protectedConst in %s on line %d
6 changes: 1 addition & 5 deletions tests/classes/constants_visibility_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ constant('A::privateConst');
string(12) "privateConst"
string(12) "privateConst"

Fatal error: Uncaught Error: Cannot access private const A::privateConst in %s:14
Stack trace:
#0 %s(14): constant('A::privateConst')
#1 {main}
thrown in %s on line 14
Warning: constant(): Couldn't find constant A::privateConst in %s on line %d
12 changes: 12 additions & 0 deletions tests/classes/constants_visibility_008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Defined on private constant should not raise exception
--FILE--
<?php

class Foo
{
private const BAR = 1;
}
echo (int)defined('Foo::BAR');
--EXPECTF--
0

0 comments on commit 07877c4

Please sign in to comment.