@@ -162,7 +162,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
162162{
163163 phar_archive_data * phar ;
164164 phar_entry_data * idata ;
165- const char * internal_file ;
165+ char * internal_file ;
166166 char * error ;
167167 HashTable * pharcontext ;
168168 php_url * resource = NULL ;
@@ -189,7 +189,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
189189 phar_request_initialize ();
190190
191191 /* strip leading "/" */
192- internal_file = ZSTR_VAL (resource -> path ) + 1 ;
192+ internal_file = estrndup ( ZSTR_VAL (resource -> path ) + 1 , ZSTR_LEN ( resource -> path ) - 1 ) ;
193193 if (mode [0 ] == 'w' || (mode [0 ] == 'r' && mode [1 ] == '+' )) {
194194 if (NULL == (idata = phar_get_or_create_entry_data (ZSTR_VAL (resource -> host ), ZSTR_LEN (resource -> host ), internal_file , strlen (internal_file ), mode , 0 , & error , 1 ))) {
195195 if (error ) {
@@ -198,6 +198,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
198198 } else {
199199 php_stream_wrapper_log_error (wrapper , options , "phar error: file \"%s\" could not be created in phar \"%s\"" , internal_file , ZSTR_VAL (resource -> host ));
200200 }
201+ efree (internal_file );
201202 php_url_free (resource );
202203 return NULL ;
203204 }
@@ -206,6 +207,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
206207 }
207208 fpf = php_stream_alloc (& phar_ops , idata , NULL , mode );
208209 php_url_free (resource );
210+ efree (internal_file );
209211
210212 if (context && Z_TYPE (context -> options ) != IS_UNDEF && (pzoption = zend_hash_str_find (HASH_OF (& context -> options ), "phar" , sizeof ("phar" )- 1 )) != NULL ) {
211213 pharcontext = HASH_OF (pzoption );
@@ -235,13 +237,15 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
235237 /* retrieve the stub */
236238 if (FAILURE == phar_get_archive (& phar , ZSTR_VAL (resource -> host ), ZSTR_LEN (resource -> host ), NULL , 0 , NULL )) {
237239 php_stream_wrapper_log_error (wrapper , options , "file %s is not a valid phar archive" , ZSTR_VAL (resource -> host ));
240+ efree (internal_file );
238241 php_url_free (resource );
239242 return NULL ;
240243 }
241244 if (phar -> is_tar || phar -> is_zip ) {
242245 if ((FAILURE == phar_get_entry_data (& idata , ZSTR_VAL (resource -> host ), ZSTR_LEN (resource -> host ), ".phar/stub.php" , sizeof (".phar/stub.php" )- 1 , "r" , 0 , & error , 0 )) || !idata ) {
243246 goto idata_error ;
244247 }
248+ efree (internal_file );
245249 if (opened_path ) {
246250 * opened_path = strpprintf (MAXPATHLEN , "%s" , phar -> fname );
247251 }
@@ -252,6 +256,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
252256 if (stream == NULL ) {
253257 if (UNEXPECTED (FAILURE == phar_open_archive_fp (phar ))) {
254258 php_stream_wrapper_log_error (wrapper , options , "phar error: could not reopen phar \"%s\"" , ZSTR_VAL (resource -> host ));
259+ efree (internal_file );
255260 php_url_free (resource );
256261 return NULL ;
257262 }
@@ -280,6 +285,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
280285 if (opened_path ) {
281286 * opened_path = strpprintf (MAXPATHLEN , "%s" , phar -> fname );
282287 }
288+ efree (internal_file );
283289 goto phar_stub ;
284290 }
285291 }
@@ -292,6 +298,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
292298 } else {
293299 php_stream_wrapper_log_error (wrapper , options , "phar error: \"%s\" is not a file in phar \"%s\"" , internal_file , ZSTR_VAL (resource -> host ));
294300 }
301+ efree (internal_file );
295302 php_url_free (resource );
296303 return NULL ;
297304 }
@@ -310,6 +317,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
310317 php_stream_wrapper_log_error (wrapper , options , "%s" , error );
311318 efree (error );
312319 phar_entry_delref (idata );
320+ efree (internal_file );
313321 return NULL ;
314322 }
315323
@@ -332,6 +340,7 @@ static php_stream * phar_wrapper_open_url(php_stream_wrapper *wrapper, const cha
332340 if (opened_path ) {
333341 * opened_path = zend_strpprintf_unchecked (MAXPATHLEN , "phar://%s/%S" , idata -> phar -> fname , idata -> internal_file -> filename );
334342 }
343+ efree (internal_file );
335344phar_stub :
336345 fpf = php_stream_alloc (& phar_ops , idata , NULL , mode );
337346 return fpf ;
@@ -653,8 +662,7 @@ static int phar_wrapper_stat(php_stream_wrapper *wrapper, const char *url, int f
653662static int phar_wrapper_unlink (php_stream_wrapper * wrapper , const char * url , int options , php_stream_context * context ) /* {{{ */
654663{
655664 php_url * resource ;
656- const char * internal_file ;
657- char * error ;
665+ char * internal_file , * error ;
658666 int internal_file_len ;
659667 phar_entry_data * idata ;
660668 phar_archive_data * pphar ;
@@ -687,7 +695,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
687695 }
688696
689697 /* need to copy to strip leading "/", will get touched again */
690- internal_file = ZSTR_VAL (resource -> path ) + 1 ;
698+ internal_file = estrndup ( ZSTR_VAL (resource -> path ) + 1 , ZSTR_LEN ( resource -> path ) - 1 ) ;
691699 internal_file_len = ZSTR_LEN (resource -> path ) - 1 ;
692700 if (FAILURE == phar_get_entry_data (& idata , ZSTR_VAL (resource -> host ), ZSTR_LEN (resource -> host ), internal_file , internal_file_len , "r" , 0 , & error , 1 )) {
693701 /* constraints of fp refcount were not met */
@@ -697,6 +705,7 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
697705 } else {
698706 php_stream_wrapper_log_error (wrapper , options , "unlink of \"%s\" failed, file does not exist" , url );
699707 }
708+ efree (internal_file );
700709 php_url_free (resource );
701710 return 0 ;
702711 }
@@ -706,11 +715,13 @@ static int phar_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int
706715 if (idata -> internal_file -> fp_refcount > 1 ) {
707716 /* more than just our fp resource is open for this file */
708717 php_stream_wrapper_log_error (wrapper , options , "phar error: \"%s\" in phar \"%s\", has open file pointers, cannot unlink" , internal_file , ZSTR_VAL (resource -> host ));
718+ efree (internal_file );
709719 php_url_free (resource );
710720 phar_entry_delref (idata );
711721 return 0 ;
712722 }
713723 php_url_free (resource );
724+ efree (internal_file );
714725 phar_entry_remove (idata , & error );
715726 if (error ) {
716727 php_stream_wrapper_log_error (wrapper , options , "%s" , error );
0 commit comments