Skip to content

Commit 03be2a2

Browse files
committed
getenv
1 parent b6cfe32 commit 03be2a2

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ PHP NEWS
170170
argument value is passed. (Girgias)
171171
. linkinfo() now raises a ValueError when the argument is an empty string.
172172
(Weilin Du)
173+
. getenv() now raises a ValueError when the $name argument contains null
174+
bytes. (Weilin Du)
173175
. putenv() now raises a ValueError when the $assignment argument contains
174176
null bytes. (Weilin Du)
175177

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ PHP 8.6 UPGRADE NOTES
8888
argument value is passed.
8989
. array_change_key_case() now raises a ValueError when an invalid $case
9090
argument value is passed.
91+
. getenv() now raises a ValueError when the $name argument contains null
92+
bytes.
9193
. putenv() now raises a ValueError when the $assignment argument contains
9294
null bytes.
9395
. linkinfo() now raises a ValueError when the $path argument is empty.

ext/standard/basic_functions.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,11 @@ PHP_FUNCTION(getenv)
706706
return;
707707
}
708708

709+
if (UNEXPECTED(memchr(str, '\0', str_len) != NULL)) {
710+
zend_argument_value_error(1, "must not contain any null bytes");
711+
RETURN_THROWS();
712+
}
713+
709714
if (!local_only) {
710715
/* SAPI method returns an emalloc()'d string */
711716
char *ptr = sapi_getenv(str, str_len);
@@ -742,7 +747,7 @@ PHP_FUNCTION(putenv)
742747
Z_PARAM_STRING(setting, setting_len)
743748
ZEND_PARSE_PARAMETERS_END();
744749

745-
if (memchr(setting, '\0', setting_len) != NULL) {
750+
if (UNEXPECTED(memchr(setting, '\0', setting_len) != NULL)) {
746751
zend_argument_value_error(1, "must not contain any null bytes");
747752
RETURN_THROWS();
748753
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
getenv() rejects null bytes
3+
--FILE--
4+
<?php
5+
6+
foreach ([false, true] as $local_only) {
7+
try {
8+
getenv("PHP_GETENV_NUL_TEST\0SUFFIX", $local_only);
9+
} catch (ValueError $exception) {
10+
echo $exception->getMessage() . "\n";
11+
}
12+
}
13+
14+
?>
15+
--EXPECT--
16+
getenv(): Argument #1 ($name) must not contain any null bytes
17+
getenv(): Argument #1 ($name) must not contain any null bytes

0 commit comments

Comments
 (0)