Skip to content

Commit 69819ba

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Fix #79254: getenv() w/o arguments not showing changes
2 parents 8ceac81 + 93b183e commit 69819ba

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed
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: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -538,39 +538,52 @@ static zend_always_inline int valid_environment_name(const char *name, const cha
538538
return 1;
539539
}
540540

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

548+
p = strchr(env, '=');
549+
if (!p
550+
|| p == env
551+
|| !valid_environment_name(env, p)) {
552+
/* malformed entry? */
553+
return;
554+
}
555+
name_len = p - env;
556+
p++;
557+
len = strlen(p);
558+
if (len == 0) {
559+
ZVAL_EMPTY_STRING(&val);
560+
} else if (len == 1) {
561+
ZVAL_INTERNED_STR(&val, ZSTR_CHAR((zend_uchar)*p));
562+
} else {
563+
ZVAL_NEW_STR(&val, zend_string_init(p, len, 0));
564+
}
565+
if (ZEND_HANDLE_NUMERIC_STR(env, name_len, idx)) {
566+
zend_hash_index_update(ht, idx, &val);
567+
} else {
568+
php_register_variable_quick(env, name_len, &val, ht);
569+
}
570+
}
571+
572+
void _php_import_environment_variables(zval *array_ptr)
573+
{
548574
tsrm_env_lock();
549575

550-
for (env = environ; env != NULL && *env != NULL; env++) {
551-
p = strchr(*env, '=');
552-
if (!p
553-
|| p == *env
554-
|| !valid_environment_name(*env, p)) {
555-
/* malformed entry? */
556-
continue;
557-
}
558-
name_len = p - *env;
559-
p++;
560-
len = strlen(p);
561-
if (len == 0) {
562-
ZVAL_EMPTY_STRING(&val);
563-
} else if (len == 1) {
564-
ZVAL_INTERNED_STR(&val, ZSTR_CHAR((zend_uchar)*p));
565-
} else {
566-
ZVAL_NEW_STR(&val, zend_string_init(p, len, 0));
567-
}
568-
if (ZEND_HANDLE_NUMERIC_STR(*env, name_len, idx)) {
569-
zend_hash_index_update(Z_ARRVAL_P(array_ptr), idx, &val);
570-
} else {
571-
php_register_variable_quick(*env, name_len, &val, Z_ARRVAL_P(array_ptr));
572-
}
576+
#ifndef PHP_WIN32
577+
for (char **env = environ; env != NULL && *env != NULL; env++) {
578+
import_environment_variable(Z_ARRVAL_P(array_ptr), *env);
573579
}
580+
#else
581+
char *environment = GetEnvironmentStringsA();
582+
for (char *env = environment; env != NULL && *env; env += strlen(env) + 1) {
583+
import_environment_variable(Z_ARRVAL_P(array_ptr), env);
584+
}
585+
FreeEnvironmentStringsA(environment);
586+
#endif
574587

575588
tsrm_env_unlock();
576589
}

0 commit comments

Comments
 (0)