Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ PHP NEWS
- Core:
. Exiting a namespace now clears seen symbols. (ilutov)

- Standard:
. php_uname() now throws ValueErrors on invalid inputs. (Girgias)

15 Aug 2024, PHP 8.4.0beta1

- Core:
Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,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
Expand Down
23 changes: 20 additions & 3 deletions ext/standard/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

/* }}} */
Expand Down
29 changes: 19 additions & 10 deletions ext/standard/tests/general_functions/php_uname_error.phpt
Original file line number Diff line number Diff line change
@@ -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"
Loading