Skip to content

Commit 93b183e

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
* PHP-7.3: Fix #79254: getenv() w/o arguments not showing changes
2 parents 038ca4b + 7b464ce commit 93b183e

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ PHP NEWS
2222
and unicode). (Nikita)
2323
. Fixed bug #79241 (Segmentation fault on preg_match()). (Nikita)
2424

25+
- Standard:
26+
. Fixed bug #79254 (getenv() w/o arguments not showing changes). (cmb)
27+
2528
?? ??? ????, PHP 7.4.3
2629

2730
- Core:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #79254 (getenv() w/o arguments not showing changes)
3+
--FILE--
4+
<?php
5+
6+
$old = getenv();
7+
var_dump(getenv("PHP_BUG_79254", true));
8+
9+
putenv("PHP_BUG_79254=BAR");
10+
11+
$new = getenv();
12+
var_dump(array_diff($new, $old));
13+
var_dump(getenv("PHP_BUG_79254", true));
14+
15+
?>
16+
--EXPECT--
17+
bool(false)
18+
array(1) {
19+
["PHP_BUG_79254"]=>
20+
string(3) "BAR"
21+
}
22+
string(3) "BAR"

main/php_variables.c

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -540,39 +540,58 @@ static zend_always_inline int valid_environment_name(const char *name, const cha
540540
return 1;
541541
}
542542

543-
void _php_import_environment_variables(zval *array_ptr)
543+
static zend_always_inline void import_environment_variable(HashTable *ht, char *env)
544544
{
545-
char **env, *p;
545+
char *p;
546546
size_t name_len, len;
547547
zval val;
548548
zend_ulong idx;
549549

550+
p = strchr(env, '=');
551+
if (!p
552+
|| p == env
553+
|| !valid_environment_name(env, p)) {
554+
/* malformed entry? */
555+
return;
556+
}
557+
name_len = p - env;
558+
p++;
559+
len = strlen(p);
560+
if (len == 0) {
561+
ZVAL_EMPTY_STRING(&val);
562+
} else if (len == 1) {
563+
ZVAL_INTERNED_STR(&val, ZSTR_CHAR((zend_uchar)*p));
564+
} else {
565+
ZVAL_NEW_STR(&val, zend_string_init(p, len, 0));
566+
}
567+
if (ZEND_HANDLE_NUMERIC_STR(env, name_len, idx)) {
568+
zend_hash_index_update(ht, idx, &val);
569+
} else {
570+
php_register_variable_quick(env, name_len, &val, ht);
571+
}
572+
}
573+
574+
void _php_import_environment_variables(zval *array_ptr)
575+
{
576+
#ifndef PHP_WIN32
577+
char **env;
578+
#else
579+
char *environment, *env;
580+
#endif
581+
550582
tsrm_env_lock();
551583

584+
#ifndef PHP_WIN32
552585
for (env = environ; env != NULL && *env != NULL; env++) {
553-
p = strchr(*env, '=');
554-
if (!p
555-
|| p == *env
556-
|| !valid_environment_name(*env, p)) {
557-
/* malformed entry? */
558-
continue;
559-
}
560-
name_len = p - *env;
561-
p++;
562-
len = strlen(p);
563-
if (len == 0) {
564-
ZVAL_EMPTY_STRING(&val);
565-
} else if (len == 1) {
566-
ZVAL_INTERNED_STR(&val, ZSTR_CHAR((zend_uchar)*p));
567-
} else {
568-
ZVAL_NEW_STR(&val, zend_string_init(p, len, 0));
569-
}
570-
if (ZEND_HANDLE_NUMERIC_STR(*env, name_len, idx)) {
571-
zend_hash_index_update(Z_ARRVAL_P(array_ptr), idx, &val);
572-
} else {
573-
php_register_variable_quick(*env, name_len, &val, Z_ARRVAL_P(array_ptr));
574-
}
586+
import_environment_variable(Z_ARRVAL_P(array_ptr), *env);
575587
}
588+
#else
589+
environment = GetEnvironmentStringsA();
590+
for (env = environment; env != NULL && *env; env += strlen(env) + 1) {
591+
import_environment_variable(Z_ARRVAL_P(array_ptr), env);
592+
}
593+
FreeEnvironmentStringsA(environment);
594+
#endif
576595

577596
tsrm_env_unlock();
578597
}

0 commit comments

Comments
 (0)