Skip to content

Commit 2c1cdf1

Browse files
committed
Do not run autoloader when resolving unqualified call
1 parent fd92892 commit 2c1cdf1

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

Zend/tests/autoloading/function/exceptions_during_autoloading004.phpt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,46 @@ namespace {
1717
autoload_register_function('autoload_second');
1818
}
1919
namespace bar {
20+
echo "Try-catch around function_exists()\n";
2021
try {
2122
\function_exists('foo');
2223
} catch (\Exception $e) {
2324
do {
2425
echo $e->getMessage()."\n";
2526
} while ($e = $e->getPrevious());
2627
}
28+
echo "Try-catch around unqualified function call\n";
2729
try {
2830
foo();
29-
} catch (\Exception $e) {
31+
} catch (\Throwable $e) {
32+
/* No autoloading for unqualified function names */
3033
do {
31-
echo $e->getMessage()."\n";
34+
echo $e::class, ': ', $e->getMessage(), "\n";
35+
} while ($e = $e->getPrevious());
36+
}
37+
echo "Try-catch around qualified function call\n";
38+
try {
39+
\foo();
40+
} catch (\Throwable $e) {
41+
do {
42+
echo $e::class, ': ', $e->getMessage(), "\n";
3243
} while ($e = $e->getPrevious());
3344
}
3445

46+
echo "function_exists() without try-catch\n";
3547
\function_exists('foo');
3648
}
3749
?>
3850
--EXPECTF--
51+
Try-catch around function_exists()
3952
autoload_first
4053
first
54+
Try-catch around unqualified function call
55+
Error: Call to undefined function bar\foo()
56+
Try-catch around qualified function call
4157
autoload_first
42-
first
58+
Exception: first
59+
function_exists() without try-catch
4360
autoload_first
4461

4562
Fatal error: Uncaught Exception: first in %s:%d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Fallback to global function should not trigger autoloading.
3+
--FILE--
4+
<?php
5+
namespace {
6+
function loader($name) {
7+
echo $name, \PHP_EOL;
8+
}
9+
10+
autoload_register_function('loader');
11+
}
12+
13+
namespace bar {
14+
var_dump('Hello');
15+
}
16+
?>
17+
--EXPECT--
18+
string(5) "Hello"

Zend/zend_vm_def.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,13 +3859,13 @@ ZEND_VM_HOT_HANDLER(69, ZEND_INIT_NS_FCALL_BY_NAME, ANY, CONST, NUM|CACHE_SLOT)
38593859
if (UNEXPECTED(fbc == NULL)) {
38603860
zval *function_name = (zval *)RT_CONSTANT(opline, opline->op2);
38613861
/* Fetch lowercase name stored in the next literal slot */
3862-
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ true);
3862+
fbc = zend_lookup_function_ex(Z_STR_P(function_name), Z_STR_P(function_name+1), /* use_autoload */ false);
38633863
if (UNEXPECTED(fbc == NULL)) {
38643864
if (UNEXPECTED(EG(exception))) {
38653865
HANDLE_EXCEPTION();
38663866
}
38673867
/* Fallback onto global namespace, by fetching the unqualified lowercase name stored in the second literal slot */
3868-
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ true);
3868+
fbc = zend_lookup_function_ex(Z_STR_P(function_name+2), Z_STR_P(function_name+2), /* use_autoload */ false);
38693869
if (fbc == NULL) {
38703870
if (UNEXPECTED(EG(exception))) {
38713871
HANDLE_EXCEPTION();

Zend/zend_vm_execute.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)