Skip to content

Commit

Permalink
Fixed bug #42820 (defined() on constant with namespace prefixes tries…
Browse files Browse the repository at this point in the history
… to load class).
  • Loading branch information
dstogov committed Oct 3, 2007
1 parent 6805501 commit eb0c56a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -35,6 +35,8 @@ PHP NEWS
- Improved and cleaned CGI code. FastCGI is now always enabled and can not be
disabled. See sapi/cgi/CHANGES for more details. (Dmitry)

- Fixed bug #42820 (defined() on constant with namespace prefixes tries to load
class). (Dmitry)
- Fixed bug #42798 (__autoload() not triggered for classes used in method
signature). (Dmitry)
- Fixed bug #42657 (ini_get() returns incorrect value when default is NULL).
Expand Down
31 changes: 31 additions & 0 deletions Zend/tests/bug42820.phpt
@@ -0,0 +1,31 @@
--TEST--
Bug #42820 (defined() on constant with namespace prefixes tries to load class)
--FILE--
<?php
namespace ns;
const ok = 0;
class foo {
const ok = 0;
}
var_dump(defined('ns::ok'));
var_dump(defined('ns::bug'));
var_dump(defined('::ns::ok'));
var_dump(defined('::ns::bug'));
var_dump(defined('ns::foo::ok'));
var_dump(defined('ns::foo::bug'));
var_dump(defined('::ns::foo::ok'));
var_dump(defined('::ns::foo::bug'));
var_dump(defined('ns::bar::bug'));
var_dump(defined('::ns::bar::bug'));
--EXPECT--
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)

2 changes: 1 addition & 1 deletion Zend/zend_builtin_functions.c
Expand Up @@ -545,7 +545,7 @@ ZEND_FUNCTION(defined)
}

convert_to_string_ex(var);
if (zend_get_constant_ex(Z_STRVAL_PP(var), Z_STRLEN_PP(var), &c, NULL, 0 TSRMLS_CC)) {
if (zend_get_constant_ex(Z_STRVAL_PP(var), Z_STRLEN_PP(var), &c, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) {
zval_dtor(&c);
RETURN_TRUE;
} else {
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_compile.h
Expand Up @@ -597,9 +597,11 @@ int zendlex(znode *zendlval TSRMLS_DC);
#define ZEND_FETCH_CLASS_AUTO 5
#define ZEND_FETCH_CLASS_INTERFACE 6
#define ZEND_FETCH_CLASS_STATIC 7
#define ZEND_FETCH_CLASS_MASK 0x0f
#define ZEND_FETCH_CLASS_RT_NS_CHECK 0x20
#define ZEND_FETCH_CLASS_RT_NS_NAME 0x40
#define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
#define ZEND_FETCH_CLASS_SILENT 0x0100

/* variable parsing type (compile-time) */
#define ZEND_PARSED_MEMBER (1<<0)
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_constants.c
Expand Up @@ -274,7 +274,7 @@ ZEND_API int zend_get_constant_ex(char *name, uint name_len, zval *result, zend_
if (name[0] == ':' && name[1] == ':') {
name += 2;
name_len -= 2;
flags = 0;
flags &= ZEND_FETCH_CLASS_SILENT;
}


Expand Down Expand Up @@ -374,7 +374,9 @@ ZEND_API int zend_get_constant_ex(char *name, uint name_len, zval *result, zend_
retval = 1;
return zend_get_constant(name, name_len, result TSRMLS_CC);
}
zend_error(E_ERROR, "Class '%s' not found", class_name);
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
zend_error(E_ERROR, "Class '%s' not found", class_name);
}
}
retval = 0;
}
Expand Down
13 changes: 8 additions & 5 deletions Zend/zend_execute_API.c
Expand Up @@ -1513,8 +1513,9 @@ zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len,
zend_class_entry **pce;
int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
int rt_ns_check = (fetch_type & ZEND_FETCH_CLASS_RT_NS_CHECK) ? 1 : 0;
int silent = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0;

fetch_type = fetch_type & ~ZEND_FETCH_CLASS_NO_AUTOLOAD;
fetch_type &= ZEND_FETCH_CLASS_MASK;
check_fetch_type:
switch (fetch_type) {
case ZEND_FETCH_CLASS_SELF:
Expand Down Expand Up @@ -1568,10 +1569,12 @@ zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len,
zend_lookup_class_ex(class_name, class_name_len, 1, &pce TSRMLS_CC)==SUCCESS) {
return *pce;
}
if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
zend_error(E_ERROR, "Interface '%s' not found", class_name);
} else {
zend_error(E_ERROR, "Class '%s' not found", class_name);
if (!silent) {
if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
zend_error(E_ERROR, "Interface '%s' not found", class_name);
} else {
zend_error(E_ERROR, "Class '%s' not found", class_name);
}
}
}
return NULL;
Expand Down

0 comments on commit eb0c56a

Please sign in to comment.