@@ -279,19 +279,23 @@ PHP_FUNCTION(passthru)
279
279
280
280
*NOT* safe for binary strings
281
281
*/
282
- PHPAPI zend_string * php_escape_shell_cmd (const char * str )
282
+ PHPAPI zend_string * php_escape_shell_cmd (const zend_string * unescaped_cmd )
283
283
{
284
284
size_t x , y ;
285
- size_t l = strlen (str );
286
- uint64_t estimate = (2 * (uint64_t )l ) + 1 ;
287
285
zend_string * cmd ;
288
286
#ifndef PHP_WIN32
289
287
char * p = NULL ;
290
288
#endif
291
289
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
+
292
296
/* max command line length - two single quotes - \0 byte length */
293
297
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 );
295
299
return ZSTR_EMPTY_ALLOC ();
296
300
}
297
301
@@ -367,7 +371,7 @@ PHPAPI zend_string *php_escape_shell_cmd(const char *str)
367
371
ZSTR_VAL (cmd )[y ] = '\0' ;
368
372
369
373
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 );
371
375
zend_string_release_ex (cmd , 0 );
372
376
return ZSTR_EMPTY_ALLOC ();
373
377
}
@@ -385,16 +389,20 @@ PHPAPI zend_string *php_escape_shell_cmd(const char *str)
385
389
/* }}} */
386
390
387
391
/* {{{ 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 )
389
393
{
390
394
size_t x , y = 0 ;
391
- size_t l = strlen (str );
392
395
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
+
393
401
uint64_t estimate = (4 * (uint64_t )l ) + 3 ;
394
402
395
403
/* max command line length - two single quotes - \0 byte length */
396
404
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 );
398
406
return ZSTR_EMPTY_ALLOC ();
399
407
}
400
408
@@ -453,7 +461,7 @@ PHPAPI zend_string *php_escape_shell_arg(const char *str)
453
461
ZSTR_VAL (cmd )[y ] = '\0' ;
454
462
455
463
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 );
457
465
zend_string_release_ex (cmd , 0 );
458
466
return ZSTR_EMPTY_ALLOC ();
459
467
}
@@ -471,18 +479,13 @@ PHPAPI zend_string *php_escape_shell_arg(const char *str)
471
479
/* {{{ Escape shell metacharacters */
472
480
PHP_FUNCTION (escapeshellcmd )
473
481
{
474
- char * command ;
475
- size_t command_len ;
482
+ zend_string * command ;
476
483
477
484
ZEND_PARSE_PARAMETERS_START (1 , 1 )
478
- Z_PARAM_STRING (command , command_len )
485
+ Z_PARAM_PATH_STR (command )
479
486
ZEND_PARSE_PARAMETERS_END ();
480
487
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 )) {
486
489
RETVAL_STR (php_escape_shell_cmd (command ));
487
490
} else {
488
491
RETVAL_EMPTY_STRING ();
@@ -493,18 +496,12 @@ PHP_FUNCTION(escapeshellcmd)
493
496
/* {{{ Quote and escape an argument for use in a shell command */
494
497
PHP_FUNCTION (escapeshellarg )
495
498
{
496
- char * argument ;
497
- size_t argument_len ;
499
+ zend_string * argument ;
498
500
499
501
ZEND_PARSE_PARAMETERS_START (1 , 1 )
500
- Z_PARAM_STRING (argument , argument_len )
502
+ Z_PARAM_PATH_STR (argument )
501
503
ZEND_PARSE_PARAMETERS_END ();
502
504
503
- if (argument_len != strlen (argument )) {
504
- zend_argument_value_error (1 , "must not contain any null bytes" );
505
- RETURN_THROWS ();
506
- }
507
-
508
505
RETVAL_STR (php_escape_shell_arg (argument ));
509
506
}
510
507
/* }}} */
0 commit comments