Skip to content

Commit

Permalink
Replace zend_atol with zend_ini_prse_quantity (#5477)
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 13, 2024
1 parent 51ead69 commit 81fd4e8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,18 @@ SW_API bool php_swoole_is_enable_coroutine() {

SW_API zend_long php_swoole_parse_to_size(zval *zv) {
if (ZVAL_IS_STRING(zv)) {
#if PHP_VERSION_ID >= 80200
zend_string *errstr;
auto size = zend_ini_parse_quantity(Z_STR_P(zv), &errstr);
if (errstr) {
php_swoole_fatal_error(
E_ERROR, "failed to parse '%s' to size, Error: %s", Z_STRVAL_P(zv), ZSTR_VAL(errstr));
zend_string_release(errstr);
}
return size;
#else
return zend_atol(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
#endif
} else {
return zval_get_long(zv);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/swoole_server/parse_option_to_size.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
swoole_server: parse option value to size
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Server;

$server = new Server('127.0.0.1', 0);
$server->set([
'buffer_output_size' => '2M',
]);
$server->set([
'buffer_output_size' => 2 * 1024 * 1024,
]);
$server->set([
'buffer_output_size' => 'xxx--2M',
]);
?>
--EXPECTF--
Fatal error: Swoole\Server::set(): failed to parse 'xxx--2M' to size, Error: Invalid quantity "xxx--2M": no valid leading digits, interpreting as "0" for backwards compatibility in %s on line %d

0 comments on commit 81fd4e8

Please sign in to comment.