diff --git a/Zend/zend_ini_parser.y b/Zend/zend_ini_parser.y index 5f788f152fb6..aa95d643666f 100644 --- a/Zend/zend_ini_parser.y +++ b/Zend/zend_ini_parser.y @@ -230,12 +230,12 @@ ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_e ini_parser_param.ini_parser_cb = ini_parser_cb; ini_parser_param.arg = arg; - CG(ini_parser_param) = &ini_parser_param; if (zend_ini_open_file_for_scanning(fh, scanner_mode) == FAILURE) { return FAILURE; } + CG(ini_parser_param) = &ini_parser_param; CG(ini_parser_unbuffered_errors) = unbuffered_errors; retval = ini_parse(); diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 3c970735a538..46ede2503aa1 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2526,6 +2526,7 @@ PHP_FUNCTION(parse_ini_file) zend_long scanner_mode = ZEND_INI_SCANNER_NORMAL; zend_file_handle fh; zend_ini_parser_cb_t ini_parser_cb; + zval prev_ini_file_section; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_PATH_STR(filename) @@ -2540,6 +2541,7 @@ PHP_FUNCTION(parse_ini_file) } /* Set callback function */ + ZVAL_COPY_VALUE(&prev_ini_file_section, &BG(active_ini_file_section)); if (process_sections) { ZVAL_UNDEF(&BG(active_ini_file_section)); ini_parser_cb = (zend_ini_parser_cb_t) php_ini_parser_cb_with_sections; @@ -2555,6 +2557,7 @@ PHP_FUNCTION(parse_ini_file) zend_array_destroy(Z_ARR_P(return_value)); RETVAL_FALSE; } + ZVAL_COPY_VALUE(&BG(active_ini_file_section), &prev_ini_file_section); zend_destroy_file_handle(&fh); } /* }}} */ @@ -2567,6 +2570,7 @@ PHP_FUNCTION(parse_ini_string) bool process_sections = 0; zend_long scanner_mode = ZEND_INI_SCANNER_NORMAL; zend_ini_parser_cb_t ini_parser_cb; + zval prev_ini_file_section; ZEND_PARSE_PARAMETERS_START(1, 3) Z_PARAM_STRING(str, str_len) @@ -2580,6 +2584,7 @@ PHP_FUNCTION(parse_ini_string) } /* Set callback function */ + ZVAL_COPY_VALUE(&prev_ini_file_section, &BG(active_ini_file_section)); if (process_sections) { ZVAL_UNDEF(&BG(active_ini_file_section)); ini_parser_cb = (zend_ini_parser_cb_t) php_ini_parser_cb_with_sections; @@ -2597,6 +2602,7 @@ PHP_FUNCTION(parse_ini_string) zend_array_destroy(Z_ARR_P(return_value)); RETVAL_FALSE; } + ZVAL_COPY_VALUE(&BG(active_ini_file_section), &prev_ini_file_section); efree(string); } /* }}} */ diff --git a/ext/standard/tests/general_functions/gh25691.phpt b/ext/standard/tests/general_functions/gh25691.phpt new file mode 100644 index 000000000000..d3d2cb9487ab --- /dev/null +++ b/ext/standard/tests/general_functions/gh25691.phpt @@ -0,0 +1,51 @@ +--TEST-- +GH-25691 (Stack-use-after-return in ini_parse() with nested parse_ini_*() from a stream wrapper) +--CREDITS-- +xuziqiang98 +--FILE-- +done) { + return ""; + } + $this->done = true; + return "a=1\n"; + } + + public function stream_eof() { + return true; + } + + public function stream_stat() { + return []; + } + + public function stream_set_option($option, $arg1, $arg2) { + return false; + } +} + +stream_wrapper_register('ww', W::class); +var_dump(parse_ini_file('ww://x', true)); +?> +--EXPECT-- +array(1) { + ["sec"]=> + array(1) { + ["b"]=> + string(1) "2" + } +} +array(1) { + ["a"]=> + string(1) "1" +}