Add ini_parse_quantity function to convert ini quantities shorthand notation to int (#8454)

This commit is contained in:
Dennis Snell 2022-07-10 14:48:52 +02:00 committed by GitHub
parent 61ad0d9136
commit 492af9f88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 1 deletions

View File

@ -30,6 +30,7 @@
#include "ext/session/php_session.h"
#include "zend_exceptions.h"
#include "zend_attributes.h"
#include "zend_ini.h"
#include "zend_operators.h"
#include "ext/standard/php_dns.h"
#include "ext/standard/php_uuencode.h"
@ -1971,6 +1972,25 @@ PHP_FUNCTION(highlight_string)
}
/* }}} */
/* {{{ Get interpreted size from the ini shorthand syntax */
PHP_FUNCTION(ini_parse_quantity)
{
zend_string *shorthand;
zend_string *errstr;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(shorthand)
ZEND_PARSE_PARAMETERS_END();
RETVAL_LONG(zend_ini_parse_quantity(shorthand, &errstr));
if (errstr) {
zend_error(E_WARNING, "%s", ZSTR_VAL(errstr));
zend_string_release(errstr);
}
}
/* }}} */
/* {{{ Get a configuration option */
PHP_FUNCTION(ini_get)
{

View File

@ -488,6 +488,8 @@ function ini_alter(string $option, string|int|float|bool|null $value): string|fa
function ini_restore(string $option): void {}
function ini_parse_quantity(string $shorthand): int {}
/** @refcount 1 */
function set_include_path(string $include_path): string|false {}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 90093c63384f7ba56b1c4073b60219cb82843b98 */
* Stub hash: 978052ddd734a50694d59f6e92bbf5f1cc946bd2 */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
@ -505,6 +505,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ini_restore, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, option, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ini_parse_quantity, 0, 1, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, shorthand, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_set_include_path, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, include_path, IS_STRING, 0)
ZEND_END_ARG_INFO()
@ -2362,6 +2366,7 @@ ZEND_FUNCTION(ini_get);
ZEND_FUNCTION(ini_get_all);
ZEND_FUNCTION(ini_set);
ZEND_FUNCTION(ini_restore);
ZEND_FUNCTION(ini_parse_quantity);
ZEND_FUNCTION(set_include_path);
ZEND_FUNCTION(get_include_path);
ZEND_FUNCTION(print_r);
@ -2994,6 +2999,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(ini_set, arginfo_ini_set)
ZEND_FALIAS(ini_alter, ini_set, arginfo_ini_alter)
ZEND_FE(ini_restore, arginfo_ini_restore)
ZEND_FE(ini_parse_quantity, arginfo_ini_parse_quantity)
ZEND_FE(set_include_path, arginfo_set_include_path)
ZEND_FE(get_include_path, arginfo_get_include_path)
ZEND_FE(print_r, arginfo_print_r)

View File

@ -0,0 +1,36 @@
--TEST--
ini_parse_quantity() function - basic test for ini_parse_quantity()
--INI--
error_reporting = E_ALL ^ E_WARNING
--FILE--
<?php
foreach (array(
'-1',
'-0x412',
'0',
'1',
'1b',
'1k',
'1m',
'1g',
'1gb',
'14.2mb',
'14.2bm',
'boat'
) as $input) {
echo ini_parse_quantity( $input ) . PHP_EOL;
}
?>
--EXPECT--
-1
-1042
0
1
1
1024
1048576
1073741824
1
14
14680064
0

View File

@ -0,0 +1,14 @@
--TEST--
ini_parse_quantity() function - warns when given inappropriate values
--INI--
error_reporting = E_ALL
--FILE--
<?php
ini_parse_quantity('-1');
ini_parse_quantity('1mb');
ini_parse_quantity('256 then skip a few then g')
?>
--EXPECTF--
Warning: Invalid quantity "1mb": unknown multipler "b", interpreting as "1" for backwards compatibility in %sini_parse_quantity_warnings.php on line 3
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