Skip to content

Commit c931840

Browse files
committed
Pass parameter instead of using global flag
1 parent 6a5a19c commit c931840

15 files changed

+93
-19
lines changed

Zend/zend_compile.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static inline uint32_t zend_alloc_cache_slot(void) {
8080
}
8181

8282
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
83-
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
83+
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, bool skip_initial);
8484

8585
#ifndef ZTS
8686
ZEND_API zend_compiler_globals compiler_globals;
@@ -375,7 +375,6 @@ void zend_init_compiler_data_structures(void) /* {{{ */
375375
CG(active_class_entry) = NULL;
376376
CG(in_compilation) = 0;
377377
CG(skip_shebang) = 0;
378-
CG(skip_initial) = 0;
379378

380379
CG(encoding_declared) = 0;
381380
CG(memoized_exprs) = NULL;

Zend/zend_compile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void zend_file_context_begin(zend_file_context *prev_context);
752752
void zend_file_context_end(zend_file_context *prev_context);
753753

754754
extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
755-
extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
755+
extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, bool skip_initial);
756756

757757
ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
758758
void startup_scanner(void);
@@ -807,7 +807,7 @@ zend_string *zval_make_interned_string(zval *zv);
807807
struct _zend_arena;
808808

809809
ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
810-
ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename);
810+
ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename, bool skip_initial);
811811
ZEND_API zend_op_array *compile_filename(int type, zend_string *filename);
812812
ZEND_API zend_ast *zend_compile_string_to_ast(
813813
zend_string *code, struct _zend_arena **ast_arena, zend_string *filename);

Zend/zend_execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4405,7 +4405,7 @@ static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval
44054405
break;
44064406
case ZEND_EVAL: {
44074407
char *eval_desc = zend_make_compiled_string_description("eval()'d code");
4408-
new_op_array = zend_compile_string(inc_filename, eval_desc);
4408+
new_op_array = zend_compile_string(inc_filename, eval_desc, 0);
44094409
efree(eval_desc);
44104410
}
44114411
break;

Zend/zend_execute_API.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *re
12231223

12241224
original_compiler_options = CG(compiler_options);
12251225
CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
1226-
new_op_array = zend_compile_string(code_str, string_name);
1226+
new_op_array = zend_compile_string(code_str, string_name, 0);
12271227
CG(compiler_options) = original_compiler_options;
12281228

12291229
if (new_op_array) {

Zend/zend_globals.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ struct _zend_compiler_globals {
9494
struct _zend_ini_parser_param *ini_parser_param;
9595

9696
bool skip_shebang;
97-
bool skip_initial;
9897
bool increment_lineno;
9998

10099
bool variable_width_locale; /* UTF-8, Shift-JIS, Big5, ISO 2022, EUC, etc */

Zend/zend_language_scanner.l

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ ZEND_API size_t zend_get_scanned_file_offset(void)
782782
return offset;
783783
}
784784

785-
zend_op_array *compile_string(zend_string *source_string, const char *filename)
785+
zend_op_array *compile_string(zend_string *source_string, const char *filename, bool skip_initial)
786786
{
787787
zend_lex_state original_lex_state;
788788
zend_op_array *op_array = NULL;
@@ -799,7 +799,7 @@ zend_op_array *compile_string(zend_string *source_string, const char *filename)
799799
filename_str = zend_string_init(filename, strlen(filename), 0);
800800
zend_prepare_string_for_scanning(&tmp, filename_str);
801801
zend_string_release(filename_str);
802-
if (CG(skip_initial)) {
802+
if (skip_initial) {
803803
BEGIN(INITIAL);
804804
} else {
805805
BEGIN(ST_IN_SCRIPTING);

ext/zend_test/test.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,41 @@ static ZEND_FUNCTION(zend_string_or_stdclass)
199199
}
200200
}
201201

202+
static ZEND_FUNCTION(zend_test_compile_string)
203+
{
204+
zend_string *source_string = NULL;
205+
zend_string *filename = NULL;
206+
bool skip_initial = 0;
207+
208+
ZEND_PARSE_PARAMETERS_START(3, 3)
209+
Z_PARAM_STR(source_string)
210+
Z_PARAM_STR(filename)
211+
Z_PARAM_BOOL(skip_initial)
212+
ZEND_PARSE_PARAMETERS_END();
213+
214+
zend_op_array *op_array = NULL;
215+
216+
op_array = compile_string(source_string, ZSTR_VAL(filename), skip_initial);
217+
218+
if (op_array) {
219+
zval retval;
220+
221+
zend_try {
222+
ZVAL_UNDEF(&retval);
223+
zend_execute(op_array, &retval);
224+
} zend_catch {
225+
destroy_op_array(op_array);
226+
efree_size(op_array, sizeof(zend_op_array));
227+
zend_bailout();
228+
} zend_end_try();
229+
230+
destroy_op_array(op_array);
231+
efree_size(op_array, sizeof(zend_op_array));
232+
}
233+
234+
return;
235+
}
236+
202237
/* Tests Z_PARAM_OBJ_OF_CLASS_OR_STR_OR_NULL */
203238
static ZEND_FUNCTION(zend_string_or_stdclass_or_null)
204239
{

ext/zend_test/test.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ function zend_test_nullable_array_return(): ?array {}
6262

6363
function zend_test_void_return(): void {}
6464

65+
function zend_test_compile_string(string $source_string, string $filename, bool $skip_initial): void {}
66+
6567
/** @deprecated */
6668
function zend_test_deprecated(mixed $arg = null): void {}
6769

ext/zend_test/test_arginfo.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 53832c784e59195e8ef41710c1e4e778f396c99b */
2+
* Stub hash: 7432f512564a359ecac72fb2cfc9718f5728166a */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_array_return, 0, 0, IS_ARRAY, 0)
55
ZEND_END_ARG_INFO()
@@ -10,6 +10,12 @@ ZEND_END_ARG_INFO()
1010
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_void_return, 0, 0, IS_VOID, 0)
1111
ZEND_END_ARG_INFO()
1212

13+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_compile_string, 0, 3, IS_VOID, 0)
14+
ZEND_ARG_TYPE_INFO(0, source_string, IS_STRING, 0)
15+
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
16+
ZEND_ARG_TYPE_INFO(0, skip_initial, _IS_BOOL, 0)
17+
ZEND_END_ARG_INFO()
18+
1319
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_test_deprecated, 0, 0, IS_VOID, 0)
1420
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, arg, IS_MIXED, 0, "null")
1521
ZEND_END_ARG_INFO()
@@ -84,6 +90,7 @@ ZEND_END_ARG_INFO()
8490
static ZEND_FUNCTION(zend_test_array_return);
8591
static ZEND_FUNCTION(zend_test_nullable_array_return);
8692
static ZEND_FUNCTION(zend_test_void_return);
93+
static ZEND_FUNCTION(zend_test_compile_string);
8794
static ZEND_FUNCTION(zend_test_deprecated);
8895
static ZEND_FUNCTION(zend_create_unterminated_string);
8996
static ZEND_FUNCTION(zend_terminate_string);
@@ -111,6 +118,7 @@ static const zend_function_entry ext_functions[] = {
111118
ZEND_FE(zend_test_array_return, arginfo_zend_test_array_return)
112119
ZEND_FE(zend_test_nullable_array_return, arginfo_zend_test_nullable_array_return)
113120
ZEND_FE(zend_test_void_return, arginfo_zend_test_void_return)
121+
ZEND_FE(zend_test_compile_string, arginfo_zend_test_compile_string)
114122
ZEND_DEP_FE(zend_test_deprecated, arginfo_zend_test_deprecated)
115123
ZEND_FE(zend_create_unterminated_string, arginfo_zend_create_unterminated_string)
116124
ZEND_FE(zend_terminate_string, arginfo_zend_terminate_string)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Zend: Test compile string
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
$source_string = <<<EOF
8+
<?php
9+
var_dump('php');
10+
EOF;
11+
12+
zend_test_compile_string($source_string, 'Source string', 1);
13+
14+
$source_string = <<<EOF
15+
var_dump('php');
16+
EOF;
17+
18+
zend_test_compile_string($source_string, 'Source string', 0);
19+
20+
$source_string = <<<EOF
21+
<?php
22+
var_dump('php');
23+
EOF;
24+
25+
zend_test_compile_string($source_string, 'Source string', 0);
26+
?>
27+
--EXPECT--
28+
string(3) "php"
29+
string(3) "php"
30+
31+
Parse error: syntax error, unexpected token "<", expecting end of file in Source string on line 1

sapi/fuzzer/fuzzer-execute.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ static void fuzzer_execute_ex(zend_execute_data *execute_data) {
5050
}
5151
}
5252

53-
static zend_op_array *(*orig_compile_string)(zend_string *source_string, const char *filename);
53+
static zend_op_array *(*orig_compile_string)(zend_string *source_string, const char *filename, bool skip_initial);
5454

55-
static zend_op_array *fuzzer_compile_string(zend_string *str, const char *filename) {
55+
static zend_op_array *fuzzer_compile_string(zend_string *str, const char *filename, bool skip_initial) {
5656
if (ZSTR_LEN(str) > MAX_SIZE) {
5757
/* Avoid compiling huge inputs via eval(). */
5858
zend_bailout();
5959
}
6060

61-
return orig_compile_string(str, filename);
61+
return orig_compile_string(str, filename, skip_initial);
6262
}
6363

6464
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);

sapi/phpdbg/phpdbg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
270270

271271
zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type);
272272
zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type);
273-
zend_op_array *(*compile_string)(zend_string *source_string, const char *filename);
273+
zend_op_array *(*compile_string)(zend_string *source_string, const char *filename, bool skip_initial);
274274
HashTable file_sources;
275275

276276
zend_arena *oplog_arena; /* arena for storing oplog */

sapi/phpdbg/phpdbg_bp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, co
878878

879879
bp_code = zend_string_concat3(
880880
"return ", sizeof("return ")-1, expr, expr_len, ";", sizeof(";")-1);
881-
new_break.ops = zend_compile_string(bp_code, "Conditional Breakpoint Code");
881+
new_break.ops = zend_compile_string(bp_code, "Conditional Breakpoint Code", 0);
882882
zend_string_release(bp_code);
883883

884884
if (new_break.ops) {

sapi/phpdbg/phpdbg_list.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,15 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
309309
return op_array;
310310
}
311311

312-
zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *filename) {
312+
zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *filename, bool skip_initial) {
313313
zend_string *fake_name;
314314
zend_op_array *op_array;
315315
phpdbg_file_source *dataptr;
316316
uint32_t line;
317317
char *bufptr, *endptr;
318318

319319
if (PHPDBG_G(flags) & PHPDBG_IN_EVAL) {
320-
return PHPDBG_G(compile_string)(source_string, filename);
320+
return PHPDBG_G(compile_string)(source_string, filename, skip_initial);
321321
}
322322

323323
dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * ZSTR_LEN(source_string));
@@ -332,7 +332,7 @@ zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *fil
332332
dataptr->lines = ++line;
333333
dataptr->line[line] = endptr - dataptr->buf;
334334

335-
op_array = PHPDBG_G(compile_string)(source_string, filename);
335+
op_array = PHPDBG_G(compile_string)(source_string, filename, skip_initial);
336336

337337
if (op_array == NULL) {
338338
efree(dataptr->buf);

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ PHPDBG_COMMAND(stdin)
514514
} /* }}} */
515515

516516
int phpdbg_compile_stdin(zend_string *code) {
517-
PHPDBG_G(ops) = zend_compile_string(code, "Standard input code");
517+
PHPDBG_G(ops) = zend_compile_string(code, "Standard input code", 0);
518518
zend_string_release(code);
519519

520520
if (EG(exception)) {

0 commit comments

Comments
 (0)