@@ -136,11 +136,11 @@ int openpty(int *master, int *slave, char *name, struct termios *termp, struct w
136
136
137
137
static int le_proc_open ; /* Resource number for `proc` resources */
138
138
139
- /* {{{ _php_array_to_envp
139
+ /* {{{ php_array_to_envp
140
140
* Process the `environment` argument to `proc_open`
141
141
* Convert into data structures which can be passed to underlying OS APIs like `exec` on POSIX or
142
142
* `CreateProcessW` on Win32 */
143
- static php_process_env _php_array_to_envp ( zval * environment )
143
+ static php_process_env php_array_to_envp ( ZEND_ATTRIBUTE_NONNULL const HashTable * environment )
144
144
{
145
145
zval * element ;
146
146
php_process_env env ;
@@ -154,13 +154,9 @@ static php_process_env _php_array_to_envp(zval *environment)
154
154
155
155
memset (& env , 0 , sizeof (env ));
156
156
157
- if (!environment ) {
158
- return env ;
159
- }
160
-
161
- uint32_t cnt = zend_hash_num_elements (Z_ARRVAL_P (environment ));
157
+ uint32_t cnt = zend_hash_num_elements (environment );
162
158
163
- if (cnt < 1 ) {
159
+ if (cnt == 0 ) {
164
160
#ifndef PHP_WIN32
165
161
env .envarray = (char * * ) ecalloc (1 , sizeof (char * ));
166
162
#endif
@@ -172,7 +168,7 @@ static php_process_env _php_array_to_envp(zval *environment)
172
168
zend_hash_init (env_hash , cnt , NULL , NULL , 0 );
173
169
174
170
/* first, we have to get the size of all the elements in the hash */
175
- ZEND_HASH_FOREACH_STR_KEY_VAL (Z_ARRVAL_P ( environment ) , key , element ) {
171
+ ZEND_HASH_FOREACH_STR_KEY_VAL (environment , key , element ) {
176
172
str = zval_get_string (element );
177
173
178
174
if (ZSTR_LEN (str ) == 0 ) {
@@ -221,7 +217,7 @@ static php_process_env _php_array_to_envp(zval *environment)
221
217
/* }}} */
222
218
223
219
/* {{{ _php_free_envp
224
- * Free the structures allocated by `_php_array_to_envp` */
220
+ * Free the structures allocated by php_array_to_envp */
225
221
static void _php_free_envp (php_process_env env )
226
222
{
227
223
#ifndef PHP_WIN32
@@ -506,7 +502,7 @@ typedef struct _descriptorspec_item {
506
502
int mode_flags ; /* mode for opening FDs: r/o, r/w, binary (on Win32), etc */
507
503
} descriptorspec_item ;
508
504
509
- static zend_string * get_valid_arg_string (zval * zv , int elem_num ) {
505
+ static zend_string * get_valid_arg_string (zval * zv , uint32_t elem_num ) {
510
506
zend_string * str = zval_get_string (zv );
511
507
if (!str ) {
512
508
return NULL ;
@@ -518,7 +514,7 @@ static zend_string *get_valid_arg_string(zval *zv, int elem_num) {
518
514
return NULL ;
519
515
}
520
516
521
- if (strlen ( ZSTR_VAL ( str )) != ZSTR_LEN (str )) {
517
+ if (zend_str_has_nul_byte (str )) {
522
518
zend_value_error ("Command array element %d contains a null byte" , elem_num );
523
519
zend_string_release (str );
524
520
return NULL ;
@@ -630,7 +626,7 @@ static zend_string *create_win_command_from_args(HashTable *args)
630
626
zval * arg_zv ;
631
627
bool is_prog_name = true;
632
628
bool is_cmd_execution = false;
633
- int elem_num = 0 ;
629
+ uint32_t elem_num = 0 ;
634
630
635
631
ZEND_HASH_FOREACH_VAL (args , arg_zv ) {
636
632
zend_string * arg_str = get_valid_arg_string (arg_zv , ++ elem_num );
@@ -778,11 +774,11 @@ static zend_result convert_command_to_use_shell(wchar_t **cmdw, size_t cmdw_len)
778
774
779
775
#ifndef PHP_WIN32
780
776
/* Convert command parameter array passed as first argument to `proc_open` into command string */
781
- static zend_string * get_command_from_array (HashTable * array , char * * * argv , int num_elems )
777
+ static zend_string * get_command_from_array (const HashTable * array , char * * * argv , uint32_t num_elems )
782
778
{
783
779
zval * arg_zv ;
784
780
zend_string * command = NULL ;
785
- int i = 0 ;
781
+ uint32_t i = 0 ;
786
782
787
783
* argv = safe_emalloc (sizeof (char * ), num_elems + 1 , 0 );
788
784
@@ -810,16 +806,16 @@ static zend_string* get_command_from_array(HashTable *array, char ***argv, int n
810
806
}
811
807
#endif
812
808
813
- static descriptorspec_item * alloc_descriptor_array (HashTable * descriptorspec )
809
+ static descriptorspec_item * alloc_descriptor_array (const HashTable * descriptorspec )
814
810
{
815
811
uint32_t ndescriptors = zend_hash_num_elements (descriptorspec );
816
812
return ecalloc (ndescriptors , sizeof (descriptorspec_item ));
817
813
}
818
814
819
- static zend_string * get_string_parameter (zval * array , int index , char * param_name )
815
+ static zend_string * get_string_parameter (const HashTable * ht , unsigned int index , const char * param_name )
820
816
{
821
817
zval * array_item ;
822
- if ((array_item = zend_hash_index_find (Z_ARRVAL_P ( array ) , index )) == NULL ) {
818
+ if ((array_item = zend_hash_index_find (ht , index )) == NULL ) {
823
819
zend_value_error ("Missing %s" , param_name );
824
820
return NULL ;
825
821
}
@@ -995,7 +991,7 @@ static zend_result dup_proc_descriptor(php_file_descriptor_t from, php_file_desc
995
991
}
996
992
997
993
static zend_result redirect_proc_descriptor (descriptorspec_item * desc , int target ,
998
- descriptorspec_item * descriptors , int ndesc , int nindex )
994
+ const descriptorspec_item * descriptors , int ndesc , int nindex )
999
995
{
1000
996
php_file_descriptor_t redirect_to = PHP_INVALID_FD ;
1001
997
@@ -1030,9 +1026,9 @@ static zend_result redirect_proc_descriptor(descriptorspec_item *desc, int targe
1030
1026
}
1031
1027
1032
1028
/* Process one item from `$descriptorspec` argument to `proc_open` */
1033
- static zend_result set_proc_descriptor_from_array (zval * descitem , descriptorspec_item * descriptors ,
1029
+ static zend_result set_proc_descriptor_from_array (const HashTable * ht , descriptorspec_item * descriptors ,
1034
1030
int ndesc , int nindex , int * pty_master_fd , int * pty_slave_fd ) {
1035
- zend_string * ztype = get_string_parameter (descitem , 0 , "handle qualifier" );
1031
+ zend_string * ztype = get_string_parameter (ht , 0 , "handle qualifier" );
1036
1032
if (!ztype ) {
1037
1033
return FAILURE ;
1038
1034
}
@@ -1042,7 +1038,7 @@ static zend_result set_proc_descriptor_from_array(zval *descitem, descriptorspec
1042
1038
1043
1039
if (zend_string_equals_literal (ztype , "pipe" )) {
1044
1040
/* Set descriptor to pipe */
1045
- zmode = get_string_parameter (descitem , 1 , "mode parameter for 'pipe'" );
1041
+ zmode = get_string_parameter (ht , 1 , "mode parameter for 'pipe'" );
1046
1042
if (zmode == NULL ) {
1047
1043
goto finish ;
1048
1044
}
@@ -1052,16 +1048,16 @@ static zend_result set_proc_descriptor_from_array(zval *descitem, descriptorspec
1052
1048
retval = set_proc_descriptor_to_socket (& descriptors [ndesc ]);
1053
1049
} else if (zend_string_equals (ztype , ZSTR_KNOWN (ZEND_STR_FILE ))) {
1054
1050
/* Set descriptor to file */
1055
- if ((zfile = get_string_parameter (descitem , 1 , "file name parameter for 'file'" )) == NULL ) {
1051
+ if ((zfile = get_string_parameter (ht , 1 , "file name parameter for 'file'" )) == NULL ) {
1056
1052
goto finish ;
1057
1053
}
1058
- if ((zmode = get_string_parameter (descitem , 2 , "mode parameter for 'file'" )) == NULL ) {
1054
+ if ((zmode = get_string_parameter (ht , 2 , "mode parameter for 'file'" )) == NULL ) {
1059
1055
goto finish ;
1060
1056
}
1061
1057
retval = set_proc_descriptor_to_file (& descriptors [ndesc ], zfile , zmode );
1062
1058
} else if (zend_string_equals_literal (ztype , "redirect" )) {
1063
1059
/* Redirect descriptor to whatever another descriptor is set to */
1064
- zval * ztarget = zend_hash_index_find_deref (Z_ARRVAL_P ( descitem ) , 1 );
1060
+ zval * ztarget = zend_hash_index_find_deref (ht , 1 );
1065
1061
if (!ztarget ) {
1066
1062
zend_value_error ("Missing redirection target" );
1067
1063
goto finish ;
@@ -1116,7 +1112,7 @@ static zend_result set_proc_descriptor_from_resource(zval *resource, descriptors
1116
1112
1117
1113
#ifndef PHP_WIN32
1118
1114
#if defined(USE_POSIX_SPAWN )
1119
- static zend_result close_parentends_of_pipes (posix_spawn_file_actions_t * actions , descriptorspec_item * descriptors , int ndesc )
1115
+ static zend_result close_parentends_of_pipes (posix_spawn_file_actions_t * actions , const descriptorspec_item * descriptors , int ndesc )
1120
1116
{
1121
1117
int r ;
1122
1118
for (int i = 0 ; i < ndesc ; i ++ ) {
@@ -1200,9 +1196,10 @@ PHP_FUNCTION(proc_open)
1200
1196
HashTable * command_ht ;
1201
1197
HashTable * descriptorspec ; /* Mandatory argument */
1202
1198
zval * pipes ; /* Mandatory argument */
1203
- char * cwd = NULL ; /* Optional argument */
1204
- size_t cwd_len = 0 ; /* Optional argument */
1205
- zval * environment = NULL , * other_options = NULL ; /* Optional arguments */
1199
+ char * cwd = NULL ; /* Optional argument */
1200
+ size_t cwd_len = 0 ; /* Optional argument */
1201
+ HashTable * environment = NULL ; /* Optional arguments */
1202
+ zval * other_options = NULL ; /* Optional arguments */
1206
1203
1207
1204
php_process_env env ;
1208
1205
int ndesc = 0 ;
@@ -1239,7 +1236,7 @@ PHP_FUNCTION(proc_open)
1239
1236
Z_PARAM_ZVAL (pipes )
1240
1237
Z_PARAM_OPTIONAL
1241
1238
Z_PARAM_STRING_OR_NULL (cwd , cwd_len )
1242
- Z_PARAM_ARRAY_OR_NULL (environment )
1239
+ Z_PARAM_ARRAY_HT_OR_NULL (environment )
1243
1240
Z_PARAM_ARRAY_OR_NULL (other_options )
1244
1241
ZEND_PARSE_PARAMETERS_END ();
1245
1242
@@ -1282,7 +1279,7 @@ PHP_FUNCTION(proc_open)
1282
1279
#endif
1283
1280
1284
1281
if (environment ) {
1285
- env = _php_array_to_envp (environment );
1282
+ env = php_array_to_envp (environment );
1286
1283
}
1287
1284
1288
1285
descriptors = alloc_descriptor_array (descriptorspec );
@@ -1302,7 +1299,7 @@ PHP_FUNCTION(proc_open)
1302
1299
goto exit_fail ;
1303
1300
}
1304
1301
} else if (Z_TYPE_P (descitem ) == IS_ARRAY ) {
1305
- if (set_proc_descriptor_from_array (descitem , descriptors , ndesc , (int )nindex ,
1302
+ if (set_proc_descriptor_from_array (Z_ARRVAL_P ( descitem ) , descriptors , ndesc , (int )nindex ,
1306
1303
& pty_master_fd , & pty_slave_fd ) == FAILURE ) {
1307
1304
goto exit_fail ;
1308
1305
}
0 commit comments