Skip to content

Commit f7c1a38

Browse files
committed
Zend: redefine scanner_mode type to be zend_ini_scanner_mode
And move error handling to the call sites in PHP_FUNCTIONs
1 parent 8e6a5f9 commit f7c1a38

7 files changed

Lines changed: 29 additions & 31 deletions

File tree

‎Zend/tests/bug61087.phpt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Bug #61087 (Memory leak in parse_ini_file when specifying invalid scanner mode)
77
parse_ini_file(__FILE__, false, 100);
88
?>
99
--EXPECTF--
10-
Warning: Invalid scanner mode in %s on line %d
10+
Warning: parse_ini_file(): Invalid scanner mode in %s on line %d

‎Zend/zend_ini.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ END_EXTERN_C()
232232
/* INI parsing engine */
233233
typedef void (*zend_ini_parser_cb_t)(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg);
234234
BEGIN_EXTERN_C()
235-
ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);
236-
ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);
235+
ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, zend_ini_scanner_mode scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);
236+
ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, zend_ini_scanner_mode scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg);
237237
END_EXTERN_C()
238238

239239
/* INI entries */

‎Zend/zend_ini_parser.y‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ static ZEND_COLD void ini_error(const char *msg)
221221
}
222222
/* }}} */
223223

224-
/* {{{ zend_parse_ini_file() */
225-
ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
224+
ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_errors, zend_ini_scanner_mode scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
226225
{
227226
int retval;
228227
zend_ini_parser_param ini_parser_param;
@@ -246,10 +245,8 @@ ZEND_API zend_result zend_parse_ini_file(zend_file_handle *fh, bool unbuffered_e
246245
return FAILURE;
247246
}
248247
}
249-
/* }}} */
250248

251-
/* {{{ zend_parse_ini_string() */
252-
ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, int scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
249+
ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_errors, zend_ini_scanner_mode scanner_mode, zend_ini_parser_cb_t ini_parser_cb, void *arg)
253250
{
254251
int retval;
255252
zend_ini_parser_param ini_parser_param;
@@ -273,7 +270,6 @@ ZEND_API zend_result zend_parse_ini_string(const char *str, bool unbuffered_erro
273270
return FAILURE;
274271
}
275272
}
276-
/* }}} */
277273

278274
/* {{{ zval_ini_dtor() */
279275
static void zval_ini_dtor(zval *zv)

‎Zend/zend_ini_scanner.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ C23_ENUM(zend_ini_scanner_mode, uint8_t) {
3333
BEGIN_EXTERN_C()
3434
ZEND_COLD uint32_t zend_ini_scanner_get_lineno(void);
3535
ZEND_COLD const char *zend_ini_scanner_get_filename(void);
36-
zend_result zend_ini_open_file_for_scanning(zend_file_handle *fh, int scanner_mode);
37-
zend_result zend_ini_prepare_string_for_scanning(const char *str, int scanner_mode);
36+
zend_result zend_ini_open_file_for_scanning(zend_file_handle *fh, zend_ini_scanner_mode scanner_mode);
37+
zend_result zend_ini_prepare_string_for_scanning(const char *str, zend_ini_scanner_mode scanner_mode);
3838
int ini_lex(zval *ini_lval);
3939
void shutdown_ini_scanner(void);
4040
END_EXTERN_C()

‎Zend/zend_ini_scanner.l‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,8 @@ static void yy_scan_buffer(const char *str, unsigned int len)
192192

193193
#define ini_filename SCNG(filename)
194194

195-
/* {{{ init_ini_scanner() */
196-
static zend_result init_ini_scanner(int scanner_mode, zend_file_handle *fh)
195+
static zend_result init_ini_scanner(zend_ini_scanner_mode scanner_mode, zend_file_handle *fh)
197196
{
198-
/* Sanity check */
199-
if (scanner_mode != ZEND_INI_SCANNER_NORMAL && scanner_mode != ZEND_INI_SCANNER_RAW && scanner_mode != ZEND_INI_SCANNER_TYPED) {
200-
zend_error(E_WARNING, "Invalid scanner mode");
201-
return FAILURE;
202-
}
203-
204197
SCNG(lineno) = 1;
205198
SCNG(scanner_mode) = scanner_mode;
206199
SCNG(yy_in) = fh;
@@ -216,7 +209,6 @@ static zend_result init_ini_scanner(int scanner_mode, zend_file_handle *fh)
216209

217210
return SUCCESS;
218211
}
219-
/* }}} */
220212

221213
/* {{{ shutdown_ini_scanner() */
222214
void shutdown_ini_scanner(void)
@@ -243,7 +235,7 @@ ZEND_COLD const char *zend_ini_scanner_get_filename(void)
243235
/* }}} */
244236

245237
/* {{{ zend_ini_open_file_for_scanning() */
246-
zend_result zend_ini_open_file_for_scanning(zend_file_handle *fh, int scanner_mode)
238+
zend_result zend_ini_open_file_for_scanning(zend_file_handle *fh, zend_ini_scanner_mode scanner_mode)
247239
{
248240
char *buf;
249241
size_t size;
@@ -262,8 +254,7 @@ zend_result zend_ini_open_file_for_scanning(zend_file_handle *fh, int scanner_mo
262254
}
263255
/* }}} */
264256

265-
/* {{{ zend_ini_prepare_string_for_scanning() */
266-
zend_result zend_ini_prepare_string_for_scanning(const char *str, int scanner_mode)
257+
zend_result zend_ini_prepare_string_for_scanning(const char *str, zend_ini_scanner_mode scanner_mode)
267258
{
268259
int len = (int)strlen(str);
269260

@@ -275,7 +266,6 @@ zend_result zend_ini_prepare_string_for_scanning(const char *str, int scanner_mo
275266

276267
return SUCCESS;
277268
}
278-
/* }}} */
279269

280270
/* {{{ zend_ini_escape_string() */
281271
static void zend_ini_escape_string(zval *lval, const char *str, int len, char quote_type)

‎ext/standard/basic_functions.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,12 @@ PHP_FUNCTION(parse_ini_file)
25082508
RETURN_THROWS();
25092509
}
25102510

2511+
/* Sanity check */
2512+
if (UNEXPECTED(scanner_mode != ZEND_INI_SCANNER_NORMAL && scanner_mode != ZEND_INI_SCANNER_RAW && scanner_mode != ZEND_INI_SCANNER_TYPED)) {
2513+
php_error_docref(NULL, E_WARNING, "Invalid scanner mode");
2514+
RETURN_FALSE;
2515+
}
2516+
25112517
/* Set callback function */
25122518
if (process_sections) {
25132519
ZVAL_UNDEF(&BG(active_ini_file_section));
@@ -2520,7 +2526,7 @@ PHP_FUNCTION(parse_ini_file)
25202526
zend_stream_init_filename_ex(&fh, filename);
25212527

25222528
array_init(return_value);
2523-
if (zend_parse_ini_file(&fh, 0, (int)scanner_mode, ini_parser_cb, return_value) == FAILURE) {
2529+
if (zend_parse_ini_file(&fh, 0, (zend_ini_scanner_mode)scanner_mode, ini_parser_cb, return_value) == FAILURE) {
25242530
zend_array_destroy(Z_ARR_P(return_value));
25252531
RETVAL_FALSE;
25262532
}
@@ -2544,6 +2550,12 @@ PHP_FUNCTION(parse_ini_string)
25442550
Z_PARAM_LONG(scanner_mode)
25452551
ZEND_PARSE_PARAMETERS_END();
25462552

2553+
/* Sanity check */
2554+
if (UNEXPECTED(scanner_mode != ZEND_INI_SCANNER_NORMAL && scanner_mode != ZEND_INI_SCANNER_RAW && scanner_mode != ZEND_INI_SCANNER_TYPED)) {
2555+
php_error_docref(NULL, E_WARNING, "Invalid scanner mode");
2556+
RETURN_FALSE;
2557+
}
2558+
25472559
if (INT_MAX - str_len < ZEND_MMAP_AHEAD) {
25482560
RETVAL_FALSE;
25492561
}
@@ -2562,7 +2574,7 @@ PHP_FUNCTION(parse_ini_string)
25622574
memset(string + str_len, 0, ZEND_MMAP_AHEAD);
25632575

25642576
array_init(return_value);
2565-
if (zend_parse_ini_string(string, 0, (int)scanner_mode, ini_parser_cb, return_value) == FAILURE) {
2577+
if (zend_parse_ini_string(string, 0, (zend_ini_scanner_mode)scanner_mode, ini_parser_cb, return_value) == FAILURE) {
25662578
zend_array_destroy(Z_ARR_P(return_value));
25672579
RETVAL_FALSE;
25682580
}

‎ext/standard/tests/file/parse_ini_file_error.phpt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Test parse_ini_file() function : error conditions
55
echo "*** Testing parse_ini_file() : error conditions ***\n";
66

77
//Test parse_ini_file with one more than the expected number of arguments
8-
echo "\n-- Testing parse_ini_file() function with more than expected no. of arguments --\n";
8+
echo "\n-- Testing parse_ini_file() function with invalid scanner mode --\n";
99
$filename = 'string_val';
1010
$process_sections = true;
11-
$extra_arg = 10;
12-
var_dump( parse_ini_file($filename, $process_sections, $extra_arg) );
11+
$scanner_mode = 10;
12+
var_dump( parse_ini_file($filename, $process_sections, $scanner_mode) );
1313

1414
echo "\n-- Testing parse_ini_file() function with a non-existent file --\n";
1515
$filename = __FILE__ . 'invalidfilename';
@@ -20,9 +20,9 @@ echo "Done";
2020
--EXPECTF--
2121
*** Testing parse_ini_file() : error conditions ***
2222

23-
-- Testing parse_ini_file() function with more than expected no. of arguments --
23+
-- Testing parse_ini_file() function with invalid scanner mode --
2424

25-
Warning: parse_ini_file(): Failed to open stream: No such file or directory in %s on line %d
25+
Warning: parse_ini_file(): Invalid scanner mode in %s on line %d
2626
bool(false)
2727

2828
-- Testing parse_ini_file() function with a non-existent file --

0 commit comments

Comments
 (0)