@@ -452,9 +452,10 @@ PHP_FUNCTION(escapeshellarg)
452
452
PHP_FUNCTION (shell_exec )
453
453
{
454
454
FILE * in ;
455
- int readbytes , total_readbytes = 0 , allocated_space ;
455
+ size_t total_readbytes ;
456
456
pval * * cmd ;
457
457
char * ret ;
458
+ php_stream * stream ;
458
459
459
460
if (ZEND_NUM_ARGS ()!= 1 || zend_get_parameters_ex (1 , & cmd )== FAILURE ) {
460
461
WRONG_PARAM_COUNT ;
@@ -474,21 +475,16 @@ PHP_FUNCTION(shell_exec)
474
475
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Unable to execute '%s'" , Z_STRVAL_PP (cmd ));
475
476
RETURN_FALSE ;
476
477
}
477
- allocated_space = EXEC_INPUT_BUF ;
478
- ret = (char * ) emalloc (allocated_space );
479
- while (1 ) {
480
- readbytes = fread (ret + total_readbytes , 1 , EXEC_INPUT_BUF , in );
481
- if (readbytes <=0 ) {
482
- break ;
483
- }
484
- total_readbytes += readbytes ;
485
- allocated_space = total_readbytes + EXEC_INPUT_BUF ;
486
- ret = (char * ) erealloc (ret , allocated_space );
487
- }
488
- pclose (in );
478
+
479
+ stream = php_stream_fopen_from_pipe (in , "rb" );
480
+ total_readbytes = php_stream_copy_to_mem (stream , & ret , PHP_STREAM_COPY_ALL , 0 );
481
+ php_stream_close (stream );
489
482
490
- RETVAL_STRINGL (ret , total_readbytes , 0 );
491
- Z_STRVAL_P (return_value )[total_readbytes ] = '\0' ;
483
+ if (total_readbytes > 0 ) {
484
+ RETURN_STRINGL (ret , total_readbytes , 0 );
485
+ } else {
486
+ RETURN_NULL ();
487
+ }
492
488
}
493
489
/* }}} */
494
490
0 commit comments