Skip to content

Commit ad60549

Browse files
committed
remove hardcoded limit on number of pipes in proc_open()
1 parent 6f46fa3 commit ad60549

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

ext/standard/proc_open.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static void proc_open_rsrc_dtor(zend_resource *rsrc)
240240
FG(pclose_ret) = -1;
241241
#endif
242242
_php_free_envp(proc->env, proc->is_persistent);
243+
pefree(proc->pipes, proc->is_persistent);
243244
pefree(proc->command, proc->is_persistent);
244245
pefree(proc, proc->is_persistent);
245246

@@ -434,7 +435,8 @@ PHP_FUNCTION(proc_open)
434435
zval *descitem = NULL;
435436
zend_string *str_index;
436437
zend_ulong nindex;
437-
struct php_proc_open_descriptor_item descriptors[PHP_PROC_OPEN_MAX_DESCRIPTORS];
438+
struct php_proc_open_descriptor_item *descriptors = NULL;
439+
int ndescriptors_array;
438440
#ifdef PHP_WIN32
439441
PROCESS_INFORMATION pi;
440442
HANDLE childHandle;
@@ -499,7 +501,11 @@ PHP_FUNCTION(proc_open)
499501
memset(&env, 0, sizeof(env));
500502
}
501503

502-
memset(descriptors, 0, sizeof(descriptors));
504+
ndescriptors_array = zend_hash_num_elements(Z_ARRVAL_P(descriptorspec));
505+
506+
descriptors = safe_emalloc(sizeof(struct php_proc_open_descriptor_item), ndescriptors_array, 0);
507+
508+
memset(descriptors, 0, sizeof(struct php_proc_open_descriptor_item) * ndescriptors_array);
503509

504510
#ifdef PHP_WIN32
505511
/* we use this to allow the child to inherit handles */
@@ -669,9 +675,7 @@ PHP_FUNCTION(proc_open)
669675
goto exit_fail;
670676
}
671677
}
672-
673-
if (++ndesc == PHP_PROC_OPEN_MAX_DESCRIPTORS)
674-
break;
678+
ndesc++;
675679
} ZEND_HASH_FOREACH_END();
676680

677681
#ifdef PHP_WIN32
@@ -875,6 +879,7 @@ PHP_FUNCTION(proc_open)
875879
proc = (struct php_process_handle*)pemalloc(sizeof(struct php_process_handle), is_persistent);
876880
proc->is_persistent = is_persistent;
877881
proc->command = command;
882+
proc->pipes = pemalloc(sizeof(zend_resource *) * ndesc, is_persistent);
878883
proc->npipes = ndesc;
879884
proc->child = child;
880885
#ifdef PHP_WIN32
@@ -952,10 +957,12 @@ PHP_FUNCTION(proc_open)
952957
}
953958
}
954959

960+
efree(descriptors);
955961
ZVAL_RES(return_value, zend_register_resource(proc, le_proc_open));
956962
return;
957963

958964
exit_fail:
965+
efree(descriptors);
959966
_php_free_envp(env, is_persistent);
960967
pefree(command, is_persistent);
961968
#if PHP_CAN_DO_PTS

ext/standard/proc_open.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ typedef int php_file_descriptor_t;
2525
typedef pid_t php_process_id_t;
2626
#endif
2727

28-
#define PHP_PROC_OPEN_MAX_DESCRIPTORS 16
29-
3028
/* Environment block under win32 is a NUL terminated sequence of NUL terminated
3129
* name=value strings.
3230
* Under unix, it is an argv style array.
@@ -44,7 +42,7 @@ struct php_process_handle {
4442
HANDLE childHandle;
4543
#endif
4644
int npipes;
47-
zend_resource *pipes[PHP_PROC_OPEN_MAX_DESCRIPTORS];
45+
zend_resource **pipes;
4846
char *command;
4947
int is_persistent;
5048
php_process_env_t env;

0 commit comments

Comments
 (0)