@@ -433,7 +433,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
433433 }
434434
435435 if (!phar -> donotflush ) {
436- phar_flush (phar , 0 , 0 , 0 , error );
436+ phar_flush (phar , error );
437437 }
438438}
439439/* }}} */
@@ -2520,27 +2520,30 @@ zend_string *phar_create_default_stub(const char *index_php, const char *web_ind
25202520}
25212521/* }}} */
25222522
2523+ int phar_flush (phar_archive_data * phar , char * * error ) {
2524+ return phar_flush_ex (phar , NULL , false, error );
2525+ }
2526+
25232527/**
25242528 * Save phar contents to disk
25252529 *
2526- * user_stub contains either a string, or a resource pointer, if len is a negative length.
2527- * user_stub and len should be both 0 if the default or existing stub should be used
2530+ * if user_stub is NULL the default or existing stub should be used
25282531 */
2529- int phar_flush (phar_archive_data * phar , char * user_stub , zend_long len , int convert , char * * error ) /* {{{ */
2532+ int phar_flush_ex (phar_archive_data * phar , zend_string * user_stub , bool is_default_stub , char * * error ) /* {{{ */
25302533{
25312534 char halt_stub [] = "__HALT_COMPILER();" ;
25322535 zend_string * newstub ;
25332536 phar_entry_info * entry , * newentry ;
25342537 size_t halt_offset ;
25352538 int restore_alias_len , global_flags = 0 , closeoldfile ;
2536- char * pos , has_dirs = 0 ;
2539+ bool has_dirs = 0 ;
25372540 char manifest [18 ], entry_buffer [24 ];
25382541 zend_off_t manifest_ftell ;
25392542 zend_long offset ;
25402543 size_t wrote ;
25412544 uint32_t manifest_len , mytime , new_manifest_count ;
25422545 uint32_t newcrc32 ;
2543- php_stream * file , * oldfile , * newfile , * stubfile ;
2546+ php_stream * file , * oldfile , * newfile ;
25442547 php_stream_filter * filter ;
25452548 php_serialize_data_t metadata_hash ;
25462549 smart_str main_metadata_str = {0 };
@@ -2566,11 +2569,11 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25662569 zend_hash_clean (& phar -> virtual_dirs );
25672570
25682571 if (phar -> is_zip ) {
2569- return phar_zip_flush (phar , user_stub , len , convert , error );
2572+ return phar_zip_flush (phar , user_stub , is_default_stub , error );
25702573 }
25712574
25722575 if (phar -> is_tar ) {
2573- return phar_tar_flush (phar , user_stub , len , convert , error );
2576+ return phar_tar_flush (phar , user_stub , is_default_stub , error );
25742577 }
25752578
25762579 if (PHAR_G (readonly )) {
@@ -2597,75 +2600,37 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv
25972600 }
25982601
25992602 if (user_stub ) {
2600- zend_string * suser_stub ;
2601- bool free_user_stub = false;
2602-
2603- if (len < 0 ) {
2604- /* resource passed in */
2605- if (!(php_stream_from_zval_no_verify (stubfile , (zval * )user_stub ))) {
2606- if (closeoldfile ) {
2607- php_stream_close (oldfile );
2608- }
2609- php_stream_close (newfile );
2610- if (error ) {
2611- spprintf (error , 0 , "unable to access resource to copy stub to new phar \"%s\"" , phar -> fname );
2612- }
2613- return EOF ;
2614- }
2615- if (len == -1 ) {
2616- len = PHP_STREAM_COPY_ALL ;
2617- } else {
2618- len = - len ;
2619- }
2620- user_stub = 0 ;
2621-
2622- if (!(suser_stub = php_stream_copy_to_mem (stubfile , len , 0 ))) {
2623- if (closeoldfile ) {
2624- php_stream_close (oldfile );
2625- }
2626- php_stream_close (newfile );
2627- if (error ) {
2628- spprintf (error , 0 , "unable to read resource to copy stub to new phar \"%s\"" , phar -> fname );
2629- }
2630- return EOF ;
2631- }
2632- free_user_stub = true;
2633- user_stub = ZSTR_VAL (suser_stub );
2634- len = ZSTR_LEN (suser_stub );
2635- }
2603+ char * pos = php_stristr (ZSTR_VAL (user_stub ), halt_stub , ZSTR_LEN (user_stub ), strlen (halt_stub ));
26362604
2637- if (( pos = php_stristr ( user_stub , halt_stub , len , sizeof ( halt_stub ) - 1 )) == NULL ) {
2605+ if (pos == NULL ) {
26382606 if (closeoldfile ) {
26392607 php_stream_close (oldfile );
26402608 }
26412609 php_stream_close (newfile );
26422610 if (error ) {
26432611 spprintf (error , 0 , "illegal stub for phar \"%s\" (__HALT_COMPILER(); is missing)" , phar -> fname );
26442612 }
2645- if (free_user_stub ) {
2646- zend_string_free (suser_stub );
2647- }
26482613 return EOF ;
26492614 }
2650- len = pos - user_stub + 18 ;
2651- if ((size_t )len != php_stream_write (newfile , user_stub , len )
2652- || 5 != php_stream_write (newfile , " ?>\r\n" , 5 )) {
2615+
2616+ size_t len = pos - ZSTR_VAL (user_stub ) + strlen (halt_stub );
2617+ const char end_sequence [] = " ?>\r\n" ;
2618+ size_t end_sequence_len = strlen (end_sequence );
2619+
2620+ if (
2621+ len != php_stream_write (newfile , ZSTR_VAL (user_stub ), len )
2622+ || end_sequence_len != php_stream_write (newfile , end_sequence , end_sequence_len )
2623+ ) {
26532624 if (closeoldfile ) {
26542625 php_stream_close (oldfile );
26552626 }
26562627 php_stream_close (newfile );
26572628 if (error ) {
26582629 spprintf (error , 0 , "unable to create stub from string in new phar \"%s\"" , phar -> fname );
26592630 }
2660- if (free_user_stub ) {
2661- zend_string_free (suser_stub );
2662- }
26632631 return EOF ;
26642632 }
2665- phar -> halt_offset = len + 5 ;
2666- if (free_user_stub ) {
2667- zend_string_free (suser_stub );
2668- }
2633+ phar -> halt_offset = len + end_sequence_len ;
26692634 } else {
26702635 size_t written ;
26712636
0 commit comments