Skip to content

Commit d803080

Browse files
committed
ext/standard/info.c: Throw ValueErrors on invalid inputs to php_uname()
1 parent 0b8fbac commit d803080

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP NEWS
55
- Core:
66
. Exiting a namespace now clears seen symbols. (ilutov)
77

8+
- Standard:
9+
. php_uname() now throws ValueErrors on invalid inputs. (Girgias)
10+
811
15 Aug 2024, PHP 8.4.0beta1
912

1013
- Core:

UPGRADING

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

197198
- Tidy:
198199
. Failures in the constructor now throw exceptions rather than emitting

ext/standard/info.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ static void php_get_windows_cpu(char *buf, size_t bufsize)
660660
PHPAPI zend_string *php_get_uname(char mode)
661661
{
662662
char *php_uname;
663+
664+
ZEND_ASSERT(mode == 'a' || mode == 'm' || mode == 'n' || mode == 'r' || mode == 's' || mode == 'v');
663665
#ifdef PHP_WIN32
664666
char tmp_uname[256];
665667
DWORD dwBuild=0;
@@ -1313,15 +1315,26 @@ PHP_FUNCTION(php_sapi_name)
13131315
/* {{{ Return information about the system PHP was built on */
13141316
PHP_FUNCTION(php_uname)
13151317
{
1316-
char *mode = "a";
1318+
char *mode_str = "a";
13171319
size_t modelen = sizeof("a")-1;
13181320

13191321
ZEND_PARSE_PARAMETERS_START(0, 1)
13201322
Z_PARAM_OPTIONAL
1321-
Z_PARAM_STRING(mode, modelen)
1323+
Z_PARAM_STRING(mode_str, modelen)
13221324
ZEND_PARSE_PARAMETERS_END();
13231325

1324-
RETURN_STR(php_get_uname(*mode));
1326+
if (modelen != 1) {
1327+
zend_argument_value_error(1, "must be a single character");
1328+
RETURN_THROWS();
1329+
}
1330+
1331+
char mode = *mode_str;
1332+
if (mode != 'a' && mode != 'm' && mode != 'n' && mode != 'r' && mode != 's' && mode != 'v') {
1333+
zend_argument_value_error(1, "must be one of \"a\", \"m\", \"n\", \"r\", \"s\", or \"v\"");
1334+
RETURN_THROWS();
1335+
}
1336+
1337+
RETURN_STR(php_get_uname(mode));
13251338
}
13261339

13271340
/* }}} */
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)