Skip to content

Commit c3acfb1

Browse files
committed
Fix phpGH-13931: Applying zero offset to null pointer in Zend/zend_opcode.c
In the test cases, the compiler bails out due to a fatal error. The data structures used by the compiler will contain stale values. In particular, for the test case CG(loop_var_stack) will contain data. The next compilation will incorrectly use elements from the previous stack. To solve this, we reset part of the compiler data structures. We don't do a full re-initialization via init_compiler() because that will also reset streams and resources. Closes phpGH-13938.
1 parent 5f9b9c4 commit c3acfb1

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed bug GH-13772 (Invalid execute_data->opline pointers in observer fcall
77
handlers when JIT is enabled). (Bob)
8+
. Fixed bug GH-13931 (Applying zero offset to null pointer in
9+
Zend/zend_opcode.c). (nielsdos)
810

911
- Fibers:
1012
. Fixed bug GH-13903 (ASAN false positive underflow when executing copy()).

Zend/tests/gh13931.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
GH-13931 (Applying zero offset to null pointer in Zend/zend_opcode.c)
3+
--FILE--
4+
<?php
5+
6+
register_shutdown_function(function() {
7+
var_dump(eval("return 1+3;"));
8+
});
9+
10+
eval(<<<EVAL
11+
function foo () {
12+
try {
13+
break;
14+
} finally {
15+
}
16+
}
17+
foo();
18+
EVAL);
19+
20+
?>
21+
--EXPECTF--
22+
Fatal error: 'break' not in the 'loop' or 'switch' context in %s on line %d
23+
int(4)

main/main.c

+8
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,14 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
14051405
/* restore memory limit */
14061406
zend_set_memory_limit(PG(memory_limit));
14071407
zend_objects_store_mark_destructed(&EG(objects_store));
1408+
if (CG(in_compilation) && (type == E_COMPILE_ERROR || type == E_PARSE)) {
1409+
/* We bailout during compilation which may for example leave stale entries in CG(loop_var_stack).
1410+
* If code is compiled during shutdown, we need to make sure the compiler is reset to a clean state,
1411+
* otherwise this will lead to incorrect compilation during shutdown.
1412+
* We don't do a full re-initialization via init_compiler() because that will also reset streams and resources. */
1413+
shutdown_compiler();
1414+
zend_init_compiler_data_structures();
1415+
}
14081416
zend_bailout();
14091417
return;
14101418
}

sapi/phpdbg/tests/gh13931.phpt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Applying zero offset to null pointer in Zend/zend_opcode.c
3+
--FILE--
4+
<?php
5+
function foo () {
6+
try {
7+
break;
8+
} finally {
9+
}
10+
}
11+
foo();
12+
?>
13+
--PHPDBG--
14+
ev 1 + 3
15+
ev 2 ** 3
16+
q
17+
--EXPECTF--
18+
Fatal error: 'break' not in the 'loop' or 'switch' context in %s on line %d
19+
prompt> 4
20+
prompt> 8
21+
prompt>

0 commit comments

Comments
 (0)