@@ -279,19 +279,23 @@ PHP_FUNCTION(passthru)
279279
280280 *NOT* safe for binary strings
281281*/
282- PHPAPI zend_string * php_escape_shell_cmd (const char * str )
282+ PHPAPI zend_string * php_escape_shell_cmd (const zend_string * unescaped_cmd )
283283{
284284 size_t x , y ;
285- size_t l = strlen (str );
286- uint64_t estimate = (2 * (uint64_t )l ) + 1 ;
287285 zend_string * cmd ;
288286#ifndef PHP_WIN32
289287 char * p = NULL ;
290288#endif
291289
290+ ZEND_ASSERT (ZSTR_LEN (unescaped_cmd ) == strlen (ZSTR_VAL (unescaped_cmd )) && "Must be a binary safe string" );
291+ size_t l = ZSTR_LEN (unescaped_cmd );
292+ const char * str = ZSTR_VAL (unescaped_cmd );
293+
294+ uint64_t estimate = (2 * (uint64_t )l ) + 1 ;
295+
292296 /* max command line length - two single quotes - \0 byte length */
293297 if (l > cmd_max_len - 2 - 1 ) {
294- php_error_docref ( NULL , E_ERROR , "Command exceeds the allowed length of %zu bytes" , cmd_max_len );
298+ zend_value_error ( "Command exceeds the allowed length of %zu bytes" , cmd_max_len );
295299 return ZSTR_EMPTY_ALLOC ();
296300 }
297301
@@ -367,7 +371,7 @@ PHPAPI zend_string *php_escape_shell_cmd(const char *str)
367371 ZSTR_VAL (cmd )[y ] = '\0' ;
368372
369373 if (y > cmd_max_len + 1 ) {
370- php_error_docref ( NULL , E_ERROR , "Escaped command exceeds the allowed length of %zu bytes" , cmd_max_len );
374+ zend_value_error ( "Escaped command exceeds the allowed length of %zu bytes" , cmd_max_len );
371375 zend_string_release_ex (cmd , 0 );
372376 return ZSTR_EMPTY_ALLOC ();
373377 }
@@ -385,16 +389,20 @@ PHPAPI zend_string *php_escape_shell_cmd(const char *str)
385389/* }}} */
386390
387391/* {{{ php_escape_shell_arg */
388- PHPAPI zend_string * php_escape_shell_arg (const char * str )
392+ PHPAPI zend_string * php_escape_shell_arg (const zend_string * unescaped_arg )
389393{
390394 size_t x , y = 0 ;
391- size_t l = strlen (str );
392395 zend_string * cmd ;
396+
397+ ZEND_ASSERT (ZSTR_LEN (unescaped_arg ) == strlen (ZSTR_VAL (unescaped_arg )) && "Must be a binary safe string" );
398+ size_t l = ZSTR_LEN (unescaped_arg );
399+ const char * str = ZSTR_VAL (unescaped_arg );
400+
393401 uint64_t estimate = (4 * (uint64_t )l ) + 3 ;
394402
395403 /* max command line length - two single quotes - \0 byte length */
396404 if (l > cmd_max_len - 2 - 1 ) {
397- php_error_docref ( NULL , E_ERROR , "Argument exceeds the allowed length of %zu bytes" , cmd_max_len );
405+ zend_value_error ( "Argument exceeds the allowed length of %zu bytes" , cmd_max_len );
398406 return ZSTR_EMPTY_ALLOC ();
399407 }
400408
@@ -453,7 +461,7 @@ PHPAPI zend_string *php_escape_shell_arg(const char *str)
453461 ZSTR_VAL (cmd )[y ] = '\0' ;
454462
455463 if (y > cmd_max_len + 1 ) {
456- php_error_docref ( NULL , E_ERROR , "Escaped argument exceeds the allowed length of %zu bytes" , cmd_max_len );
464+ zend_value_error ( "Escaped argument exceeds the allowed length of %zu bytes" , cmd_max_len );
457465 zend_string_release_ex (cmd , 0 );
458466 return ZSTR_EMPTY_ALLOC ();
459467 }
@@ -471,18 +479,13 @@ PHPAPI zend_string *php_escape_shell_arg(const char *str)
471479/* {{{ Escape shell metacharacters */
472480PHP_FUNCTION (escapeshellcmd )
473481{
474- char * command ;
475- size_t command_len ;
482+ zend_string * command ;
476483
477484 ZEND_PARSE_PARAMETERS_START (1 , 1 )
478- Z_PARAM_STRING (command , command_len )
485+ Z_PARAM_PATH_STR (command )
479486 ZEND_PARSE_PARAMETERS_END ();
480487
481- if (command_len ) {
482- if (command_len != strlen (command )) {
483- zend_argument_value_error (1 , "must not contain any null bytes" );
484- RETURN_THROWS ();
485- }
488+ if (ZSTR_LEN (command )) {
486489 RETVAL_STR (php_escape_shell_cmd (command ));
487490 } else {
488491 RETVAL_EMPTY_STRING ();
@@ -493,18 +496,12 @@ PHP_FUNCTION(escapeshellcmd)
493496/* {{{ Quote and escape an argument for use in a shell command */
494497PHP_FUNCTION (escapeshellarg )
495498{
496- char * argument ;
497- size_t argument_len ;
499+ zend_string * argument ;
498500
499501 ZEND_PARSE_PARAMETERS_START (1 , 1 )
500- Z_PARAM_STRING (argument , argument_len )
502+ Z_PARAM_PATH_STR (argument )
501503 ZEND_PARSE_PARAMETERS_END ();
502504
503- if (argument_len != strlen (argument )) {
504- zend_argument_value_error (1 , "must not contain any null bytes" );
505- RETURN_THROWS ();
506- }
507-
508505 RETVAL_STR (php_escape_shell_arg (argument ));
509506}
510507/* }}} */
0 commit comments