@@ -544,6 +544,7 @@ int phpdbg_compile_stdin(zend_string *code) {
544544 PHPDBG_G (exec_len ) = 1 ;
545545 { /* remove leading ?> from source */
546546 int i ;
547+ /* remove trailing data after zero byte, used for avoiding conflicts in eval()'ed code snippets */
547548 zend_string * source_path = strpprintf (0 , "-%c%p" , 0 , PHPDBG_G (ops )-> opcodes );
548549 phpdbg_file_source * data = zend_hash_find_ptr (& PHPDBG_G (file_sources ), source_path );
549550 dtor_func_t dtor = PHPDBG_G (file_sources ).pDestructor ;
@@ -553,9 +554,6 @@ int phpdbg_compile_stdin(zend_string *code) {
553554 zend_hash_str_update_ptr (& PHPDBG_G (file_sources ), "-" , 1 , data );
554555 zend_string_release (source_path );
555556
556- efree (data -> filename );
557- data -> filename = estrdup ("-" );
558-
559557 for (i = 1 ; i <= data -> lines ; i ++ ) {
560558 data -> line [i ] -= 2 ;
561559 }
@@ -572,7 +570,10 @@ int phpdbg_compile(void) /* {{{ */
572570{
573571 zend_file_handle fh ;
574572 char * buf ;
573+ char * start_line = NULL ;
575574 size_t len ;
575+ size_t start_line_len ;
576+ int i ;
576577
577578 if (!PHPDBG_G (exec )) {
578579 phpdbg_error ("inactive" , "type=\"nocontext\"" , "No execution context" );
@@ -591,14 +592,40 @@ int phpdbg_compile(void) /* {{{ */
591592 }
592593 case '\n' :
593594 CG (start_lineno ) = 2 ;
594- fh .handle .stream .mmap .len -= fh .handle .stream .mmap .buf - buf ;
595+ start_line_len = fh .handle .stream .mmap .buf - buf ;
596+ start_line = emalloc (start_line_len );
597+ memcpy (start_line , buf , start_line_len );
598+ fh .handle .stream .mmap .len -= start_line_len ;
595599 end = fh .handle .stream .mmap .buf ;
596600 }
597601 } while (fh .handle .stream .mmap .buf + 1 < end );
598602 }
599603
600604 PHPDBG_G (ops ) = zend_compile_file (& fh , ZEND_INCLUDE );
601605
606+ /* prepend shebang line to file_source */
607+ if (start_line ) {
608+ phpdbg_file_source * data = zend_hash_find_ptr (& PHPDBG_G (file_sources ), PHPDBG_G (ops )-> filename );
609+
610+ dtor_func_t dtor = PHPDBG_G (file_sources ).pDestructor ;
611+ PHPDBG_G (file_sources ).pDestructor = NULL ;
612+ zend_hash_del (& PHPDBG_G (file_sources ), PHPDBG_G (ops )-> filename );
613+ PHPDBG_G (file_sources ).pDestructor = dtor ;
614+
615+ data = erealloc (data , sizeof (phpdbg_file_source ) + sizeof (uint ) * ++ data -> lines );
616+ memmove (data -> line + 1 , data -> line , sizeof (uint ) * data -> lines );
617+ data -> line [0 ] = 0 ;
618+ data -> buf = erealloc (data -> buf , data -> len + start_line_len );
619+ memmove (data -> buf + start_line_len , data -> buf , data -> len * sizeof (uint ));
620+ memcpy (data -> buf , start_line , start_line_len );
621+ efree (start_line );
622+ data -> len += start_line_len ;
623+ for (i = 1 ; i <= data -> lines ; i ++ ) {
624+ data -> line [i ] += start_line_len ;
625+ }
626+ zend_hash_update_ptr (& PHPDBG_G (file_sources ), PHPDBG_G (ops )-> filename , data );
627+ }
628+
602629 fh .handle .stream .mmap .buf = buf ;
603630 fh .handle .stream .mmap .len = len ;
604631 zend_destroy_file_handle (& fh );
0 commit comments