Skip to content

Commit 6534ff1

Browse files
committed
Avoid strncat use in proc_open
Instead manually manage the insertion position.
1 parent 724a466 commit 6534ff1

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

ext/standard/proc_open.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent
7575
char **ep;
7676
#endif
7777
char *p;
78-
size_t cnt, l, sizeenv = 0;
78+
size_t cnt, sizeenv = 0;
7979
HashTable *env_hash;
8080

8181
memset(&env, 0, sizeof(env));
@@ -122,25 +122,20 @@ static php_process_env_t _php_array_to_envp(zval *environment, int is_persistent
122122
p = env.envp = (char *) pecalloc(sizeenv + 4, 1, is_persistent);
123123

124124
ZEND_HASH_FOREACH_STR_KEY_PTR(env_hash, key, str) {
125-
if (key) {
126-
l = ZSTR_LEN(key) + ZSTR_LEN(str) + 2;
127-
memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
128-
strncat(p, "=", 1);
129-
strncat(p, ZSTR_VAL(str), ZSTR_LEN(str));
130-
131-
#ifndef PHP_WIN32
132-
*ep = p;
133-
++ep;
134-
#endif
135-
p += l;
136-
} else {
137-
memcpy(p, ZSTR_VAL(str), ZSTR_LEN(str));
138125
#ifndef PHP_WIN32
139-
*ep = p;
140-
++ep;
126+
*ep = p;
127+
++ep;
141128
#endif
142-
p += ZSTR_LEN(str) + 1;
129+
130+
if (key) {
131+
memcpy(p, ZSTR_VAL(key), ZSTR_LEN(key));
132+
p += ZSTR_LEN(key);
133+
*p++ = '=';
143134
}
135+
136+
memcpy(p, ZSTR_VAL(str), ZSTR_LEN(str));
137+
p += ZSTR_LEN(str);
138+
*p++ = '\0';
144139
zend_string_release_ex(str, 0);
145140
} ZEND_HASH_FOREACH_END();
146141

0 commit comments

Comments
 (0)