Skip to content

Commit

Permalink
Do not run autoloader when resolving unqualified call
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Nov 28, 2022
1 parent 9ef2a81 commit ee63c9b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,46 @@ namespace {
autoload_register_function('autoload_second');
}
namespace bar {
echo "Try-catch around function_exists()\n";
try {
\function_exists('foo');
} catch (\Exception $e) {
do {
echo $e->getMessage()."\n";
} while ($e = $e->getPrevious());
}
echo "Try-catch around unqualified function call\n";
try {
foo();
} catch (\Exception $e) {
} catch (\Throwable $e) {
/* No autoloading for unqualified function names */
do {
echo $e->getMessage()."\n";
echo $e::class, ': ', $e->getMessage(), "\n";
} while ($e = $e->getPrevious());
}
echo "Try-catch around qualified function call\n";
try {
\foo();
} catch (\Throwable $e) {
do {
echo $e::class, ': ', $e->getMessage(), "\n";
} while ($e = $e->getPrevious());
}

echo "function_exists() without try-catch\n";
\function_exists('foo');
}
?>
--EXPECTF--
Try-catch around function_exists()
autoload_first
first
Try-catch around unqualified function call
Error: Call to undefined function bar\foo()
Try-catch around qualified function call
autoload_first
first
Exception: first
function_exists() without try-catch
autoload_first

Fatal error: Uncaught Exception: first in %s:%d
Expand Down
18 changes: 18 additions & 0 deletions Zend/tests/autoloading/function/global_fallback002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Fallback to global function should not trigger autoloading.
--FILE--
<?php
namespace {
function loader($name) {
echo $name, \PHP_EOL;
}

autoload_register_function('loader');
}

namespace bar {
var_dump('Hello');
}
?>
--EXPECT--
string(5) "Hello"
4 changes: 2 additions & 2 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -3859,13 +3859,13 @@ ZEND_VM_HOT_HANDLER(69, ZEND_INIT_NS_FCALL_BY_NAME, ANY, CONST, NUM|CACHE_SLOT)
if (UNEXPECTED(fbc == NULL)) {
zval *function_name = (zval *)RT_CONSTANT(opline, opline->op2);
/* Fetch lowercase name stored in the next literal slot */
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ true);
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ false);
if (UNEXPECTED(fbc == NULL)) {
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
}
/* Fallback onto global namespace, by fetching the unqualified lowercase name stored in the second literal slot */
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ true);
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ false);
if (fbc == NULL) {
if (UNEXPECTED(EG(exception))) {
HANDLE_EXCEPTION();
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee63c9b

Please sign in to comment.