Skip to content

Commit 74bf894

Browse files
authored
ext/standard/info.c: Throw ValueErrors on invalid inputs to php_uname() (php#15385)
1 parent d100caa commit 74bf894

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ PHP NEWS
1111
As such, passing invalid types to exit/die may now result in a TypeError
1212
being thrown. (Girgias)
1313

14+
- Standard:
15+
. php_uname() now throws ValueErrors on invalid inputs. (Girgias)
16+
1417
15 Aug 2024, PHP 8.4.0beta1
1518

1619
- Core:

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ PHP 8.4 UPGRADE NOTES
200200
$enclosure arguments are not one byte long, or if the $escape is not one
201201
byte long or the empty string. This aligns the behaviour to be identical
202202
to that of fputcsv() and fgetcsv().
203+
. php_uname() now throws ValueErrors on invalid inputs.
203204

204205
- Tidy:
205206
. Failures in the constructor now throw exceptions rather than emitting

ext/standard/info.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,16 @@ static void php_get_windows_cpu(char *buf, size_t bufsize)
656656
/* }}} */
657657
#endif
658658

659+
static inline bool php_is_valid_uname_mode(char mode) {
660+
return mode == 'a' || mode == 'm' || mode == 'n' || mode == 'r' || mode == 's' || mode == 'v';
661+
}
662+
659663
/* {{{ php_get_uname */
660664
PHPAPI zend_string *php_get_uname(char mode)
661665
{
662666
char *php_uname;
667+
668+
ZEND_ASSERT(php_is_valid_uname_mode(mode));
663669
#ifdef PHP_WIN32
664670
char tmp_uname[256];
665671
DWORD dwBuild=0;
@@ -1313,15 +1319,26 @@ PHP_FUNCTION(php_sapi_name)
13131319
/* {{{ Return information about the system PHP was built on */
13141320
PHP_FUNCTION(php_uname)
13151321
{
1316-
char *mode = "a";
1322+
char *mode_str = "a";
13171323
size_t modelen = sizeof("a")-1;
13181324

13191325
ZEND_PARSE_PARAMETERS_START(0, 1)
13201326
Z_PARAM_OPTIONAL
1321-
Z_PARAM_STRING(mode, modelen)
1327+
Z_PARAM_STRING(mode_str, modelen)
13221328
ZEND_PARSE_PARAMETERS_END();
13231329

1324-
RETURN_STR(php_get_uname(*mode));
1330+
if (modelen != 1) {
1331+
zend_argument_value_error(1, "must be a single character");
1332+
RETURN_THROWS();
1333+
}
1334+
1335+
char mode = *mode_str;
1336+
if (!php_is_valid_uname_mode(mode)) {
1337+
zend_argument_value_error(1, "must be one of \"a\", \"m\", \"n\", \"r\", \"s\", or \"v\"");
1338+
RETURN_THROWS();
1339+
}
1340+
1341+
RETURN_STR(php_get_uname(mode));
13251342
}
13261343

13271344
/* }}} */
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
--TEST--
2-
Test php_uname() function - error conditions - pass function incorrect arguments
2+
php_uname(): Invalid arguments
33
--FILE--
44
<?php
55

6-
echo "*** Testing php_uname() - error test\n";
7-
8-
echo "\n-- Testing php_uname() function with invalid mode --\n";
9-
// am invalid mode should result in same o/p as mode 'a'
10-
var_dump( php_uname('z') == php_uname('z') );
6+
try {
7+
var_dump(php_uname(''));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
10+
}
11+
try {
12+
var_dump(php_uname('test'));
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
15+
}
16+
try {
17+
var_dump(php_uname('z'));
18+
} catch (Throwable $e) {
19+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
20+
}
1121

1222
?>
1323
--EXPECT--
14-
*** Testing php_uname() - error test
15-
16-
-- Testing php_uname() function with invalid mode --
17-
bool(true)
24+
ValueError: php_uname(): Argument #1 ($mode) must be a single character
25+
ValueError: php_uname(): Argument #1 ($mode) must be a single character
26+
ValueError: php_uname(): Argument #1 ($mode) must be one of "a", "m", "n", "r", "s", or "v"

0 commit comments

Comments
 (0)