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