1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

ext/standard/info.c: Throw ValueErrors on invalid inputs to php_uname() (#15385)

This commit is contained in:
Gina Peter Banyard
2024-08-14 13:20:26 +01:00
committed by GitHub
parent d100caa476
commit 74bf894950
4 changed files with 43 additions and 13 deletions

3
NEWS
View File

@@ -11,6 +11,9 @@ PHP NEWS
As such, passing invalid types to exit/die may now result in a TypeError
being thrown. (Girgias)
- Standard:
. php_uname() now throws ValueErrors on invalid inputs. (Girgias)
15 Aug 2024, PHP 8.4.0beta1
- Core:

View File

@@ -200,6 +200,7 @@ PHP 8.4 UPGRADE NOTES
$enclosure arguments are not one byte long, or if the $escape is not one
byte long or the empty string. This aligns the behaviour to be identical
to that of fputcsv() and fgetcsv().
. php_uname() now throws ValueErrors on invalid inputs.
- Tidy:
. Failures in the constructor now throw exceptions rather than emitting

View File

@@ -656,10 +656,16 @@ static void php_get_windows_cpu(char *buf, size_t bufsize)
/* }}} */
#endif
static inline bool php_is_valid_uname_mode(char mode) {
return mode == 'a' || mode == 'm' || mode == 'n' || mode == 'r' || mode == 's' || mode == 'v';
}
/* {{{ php_get_uname */
PHPAPI zend_string *php_get_uname(char mode)
{
char *php_uname;
ZEND_ASSERT(php_is_valid_uname_mode(mode));
#ifdef PHP_WIN32
char tmp_uname[256];
DWORD dwBuild=0;
@@ -1313,15 +1319,26 @@ PHP_FUNCTION(php_sapi_name)
/* {{{ Return information about the system PHP was built on */
PHP_FUNCTION(php_uname)
{
char *mode = "a";
char *mode_str = "a";
size_t modelen = sizeof("a")-1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(mode, modelen)
Z_PARAM_STRING(mode_str, modelen)
ZEND_PARSE_PARAMETERS_END();
RETURN_STR(php_get_uname(*mode));
if (modelen != 1) {
zend_argument_value_error(1, "must be a single character");
RETURN_THROWS();
}
char mode = *mode_str;
if (!php_is_valid_uname_mode(mode)) {
zend_argument_value_error(1, "must be one of \"a\", \"m\", \"n\", \"r\", \"s\", or \"v\"");
RETURN_THROWS();
}
RETURN_STR(php_get_uname(mode));
}
/* }}} */

View File

@@ -1,17 +1,26 @@
--TEST--
Test php_uname() function - error conditions - pass function incorrect arguments
php_uname(): Invalid arguments
--FILE--
<?php
echo "*** Testing php_uname() - error test\n";
echo "\n-- Testing php_uname() function with invalid mode --\n";
// am invalid mode should result in same o/p as mode 'a'
var_dump( php_uname('z') == php_uname('z') );
try {
var_dump(php_uname(''));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
try {
var_dump(php_uname('test'));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
try {
var_dump(php_uname('z'));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
*** Testing php_uname() - error test
-- Testing php_uname() function with invalid mode --
bool(true)
ValueError: php_uname(): Argument #1 ($mode) must be a single character
ValueError: php_uname(): Argument #1 ($mode) must be a single character
ValueError: php_uname(): Argument #1 ($mode) must be one of "a", "m", "n", "r", "s", or "v"