Skip to content

Commit 492af9f

Browse files
authored
Add ini_parse_quantity function to convert ini quantities shorthand notation to int (#8454)
1 parent 61ad0d9 commit 492af9f

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

ext/standard/basic_functions.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "ext/session/php_session.h"
3131
#include "zend_exceptions.h"
3232
#include "zend_attributes.h"
33+
#include "zend_ini.h"
3334
#include "zend_operators.h"
3435
#include "ext/standard/php_dns.h"
3536
#include "ext/standard/php_uuencode.h"
@@ -1971,6 +1972,25 @@ PHP_FUNCTION(highlight_string)
19711972
}
19721973
/* }}} */
19731974

1975+
/* {{{ Get interpreted size from the ini shorthand syntax */
1976+
PHP_FUNCTION(ini_parse_quantity)
1977+
{
1978+
zend_string *shorthand;
1979+
zend_string *errstr;
1980+
1981+
ZEND_PARSE_PARAMETERS_START(1, 1)
1982+
Z_PARAM_STR(shorthand)
1983+
ZEND_PARSE_PARAMETERS_END();
1984+
1985+
RETVAL_LONG(zend_ini_parse_quantity(shorthand, &errstr));
1986+
1987+
if (errstr) {
1988+
zend_error(E_WARNING, "%s", ZSTR_VAL(errstr));
1989+
zend_string_release(errstr);
1990+
}
1991+
}
1992+
/* }}} */
1993+
19741994
/* {{{ Get a configuration option */
19751995
PHP_FUNCTION(ini_get)
19761996
{

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ function ini_alter(string $option, string|int|float|bool|null $value): string|fa
488488

489489
function ini_restore(string $option): void {}
490490

491+
function ini_parse_quantity(string $shorthand): int {}
492+
491493
/** @refcount 1 */
492494
function set_include_path(string $include_path): string|false {}
493495

ext/standard/basic_functions_arginfo.h

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
ini_parse_quantity() function - basic test for ini_parse_quantity()
3+
--INI--
4+
error_reporting = E_ALL ^ E_WARNING
5+
--FILE--
6+
<?php
7+
foreach (array(
8+
'-1',
9+
'-0x412',
10+
'0',
11+
'1',
12+
'1b',
13+
'1k',
14+
'1m',
15+
'1g',
16+
'1gb',
17+
'14.2mb',
18+
'14.2bm',
19+
'boat'
20+
) as $input) {
21+
echo ini_parse_quantity( $input ) . PHP_EOL;
22+
}
23+
?>
24+
--EXPECT--
25+
-1
26+
-1042
27+
0
28+
1
29+
1
30+
1024
31+
1048576
32+
1073741824
33+
1
34+
14
35+
14680064
36+
0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
ini_parse_quantity() function - warns when given inappropriate values
3+
--INI--
4+
error_reporting = E_ALL
5+
--FILE--
6+
<?php
7+
ini_parse_quantity('-1');
8+
ini_parse_quantity('1mb');
9+
ini_parse_quantity('256 then skip a few then g')
10+
?>
11+
--EXPECTF--
12+
Warning: Invalid quantity "1mb": unknown multipler "b", interpreting as "1" for backwards compatibility in %sini_parse_quantity_warnings.php on line 3
13+
14+
Warning: Invalid quantity "256 then skip a few then g", interpreting as "256 g" for backwards compatibility in %sini_parse_quantity_warnings.php on line 4

0 commit comments

Comments
 (0)