diff --git a/ext/phar/dirstream.c b/ext/phar/dirstream.c index 41f45bd17f185..4fe61db412a4c 100644 --- a/ext/phar/dirstream.c +++ b/ext/phar/dirstream.c @@ -507,7 +507,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo return 0; } - phar_flush(phar, 0, 0, 0, &error); + phar_flush(phar, &error); if (error) { php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", entry.filename, phar->fname, error); @@ -634,7 +634,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options } else { entry->is_deleted = 1; entry->is_modified = 1; - phar_flush(phar, 0, 0, 0, &error); + phar_flush(phar, &error); if (error) { php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", entry->filename, phar->fname, error); diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 131bc6755301b..5ae2d2f9a4562 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -433,7 +433,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */ } if (!phar->donotflush) { - phar_flush(phar, 0, 0, 0, error); + phar_flush(phar, error); } } /* }}} */ @@ -2520,39 +2520,44 @@ zend_string *phar_create_default_stub(const char *index_php, const char *web_ind } /* }}} */ +void phar_flush(phar_archive_data *phar, char **error) { + phar_flush_ex(phar, NULL, false, error); +} + /** * Save phar contents to disk * - * user_stub contains either a string, or a resource pointer, if len is a negative length. - * user_stub and len should be both 0 if the default or existing stub should be used + * if user_stub is NULL the default or existing stub should be used */ -int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */ +void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { - char halt_stub[] = "__HALT_COMPILER();"; - zend_string *newstub; + static const char halt_stub[] = "__HALT_COMPILER();"; + phar_entry_info *entry, *newentry; size_t halt_offset; - int restore_alias_len, global_flags = 0, closeoldfile; - char *pos, has_dirs = 0; + int restore_alias_len, global_flags = 0; + bool must_close_old_file = false; + bool has_dirs = false; char manifest[18], entry_buffer[24]; zend_off_t manifest_ftell; zend_long offset; size_t wrote; uint32_t manifest_len, mytime, new_manifest_count; uint32_t newcrc32; - php_stream *file, *oldfile, *newfile, *stubfile; + php_stream *file, *oldfile, *newfile; php_stream_filter *filter; php_serialize_data_t metadata_hash; smart_str main_metadata_str = {0}; - int free_fp = 1, free_ufp = 1; - int manifest_hack = 0; + bool free_fp = true; + bool free_ufp = true; + bool manifest_hack = false; php_stream *shared_cfp = NULL; if (phar->is_persistent) { if (error) { spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname); } - return EOF; + return; } if (error) { @@ -2560,143 +2565,107 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } if (!zend_hash_num_elements(&phar->manifest) && !user_stub) { - return EOF; + return; } zend_hash_clean(&phar->virtual_dirs); if (phar->is_zip) { - return phar_zip_flush(phar, user_stub, len, convert, error); + phar_zip_flush(phar, user_stub, is_default_stub, error); + return; } if (phar->is_tar) { - return phar_tar_flush(phar, user_stub, len, convert, error); + phar_tar_flush(phar, user_stub, is_default_stub, error); + return; } if (PHAR_G(readonly)) { - return EOF; + return; } if (phar->fp && !phar->is_brandnew) { oldfile = phar->fp; - closeoldfile = 0; + must_close_old_file = false; php_stream_rewind(oldfile); } else { oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); - closeoldfile = oldfile != NULL; + must_close_old_file = oldfile != NULL; } newfile = php_stream_fopen_tmpfile(); if (!newfile) { if (error) { spprintf(error, 0, "unable to create temporary file"); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } if (user_stub) { - zend_string *suser_stub; - bool free_user_stub = false; - - if (len < 0) { - /* resource passed in */ - if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) { - if (closeoldfile) { - php_stream_close(oldfile); - } - php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname); - } - return EOF; - } - if (len == -1) { - len = PHP_STREAM_COPY_ALL; - } else { - len = -len; - } - user_stub = 0; - - if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) { - if (closeoldfile) { - php_stream_close(oldfile); - } - php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname); - } - return EOF; - } - free_user_stub = true; - user_stub = ZSTR_VAL(suser_stub); - len = ZSTR_LEN(suser_stub); - } + char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub)); - if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) { - if (closeoldfile) { + if (pos == NULL) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); if (error) { spprintf(error, 0, "illegal stub for phar \"%s\" (__HALT_COMPILER(); is missing)", phar->fname); } - if (free_user_stub) { - zend_string_free(suser_stub); - } - return EOF; + return; } - len = pos - user_stub + 18; - if ((size_t)len != php_stream_write(newfile, user_stub, len) - || 5 != php_stream_write(newfile, " ?>\r\n", 5)) { - if (closeoldfile) { + + size_t len = pos - ZSTR_VAL(user_stub) + strlen(halt_stub); + const char end_sequence[] = " ?>\r\n"; + size_t end_sequence_len = strlen(end_sequence); + + if ( + len != php_stream_write(newfile, ZSTR_VAL(user_stub), len) + || end_sequence_len != php_stream_write(newfile, end_sequence, end_sequence_len) + ) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); if (error) { spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname); } - if (free_user_stub) { - zend_string_free(suser_stub); - } - return EOF; - } - phar->halt_offset = len + 5; - if (free_user_stub) { - zend_string_free(suser_stub); + return; } + phar->halt_offset = len + end_sequence_len; } else { size_t written; + zend_string *new_stub = NULL; if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) { php_stream_copy_to_stream_ex(oldfile, newfile, phar->halt_offset, &written); - newstub = NULL; } else { /* this is either a brand new phar or a default stub overwrite */ - newstub = phar_create_default_stub(NULL, NULL, NULL); - phar->halt_offset = ZSTR_LEN(newstub); - written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset); + new_stub = phar_create_default_stub(NULL, NULL, NULL); + phar->halt_offset = ZSTR_LEN(new_stub); + written = php_stream_write(newfile, ZSTR_VAL(new_stub), phar->halt_offset); } if (phar->halt_offset != written) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); if (error) { - if (newstub) { + if (new_stub) { spprintf(error, 0, "unable to create stub in new phar \"%s\"", phar->fname); } else { spprintf(error, 0, "unable to copy stub of old phar to new phar \"%s\"", phar->fname); } } - if (newstub) { - zend_string_free(newstub); + if (new_stub) { + zend_string_free(new_stub); } - return EOF; + return; } - if (newstub) { - zend_string_free(newstub); + if (new_stub) { + zend_string_free(new_stub); } } manifest_ftell = php_stream_tell(newfile); @@ -2732,10 +2701,10 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv /* open file pointers refer to this fp, do not free the stream */ switch (entry->fp_type) { case PHAR_FP: - free_fp = 0; + free_fp = false; break; case PHAR_UFP: - free_ufp = 0; + free_ufp = false; default: break; } @@ -2746,7 +2715,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (entry->is_dir) { /* we use this to calculate API version, 1.1.1 is used for phars with directories */ - has_dirs = 1; + has_dirs = true; } if (!Z_ISUNDEF(entry->metadata_tracker.val) && !entry->metadata_tracker.str) { ZEND_ASSERT(!entry->is_persistent); @@ -2782,14 +2751,14 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } file = phar_get_efp(entry, 0); if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); if (error) { spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname); } - return EOF; + return; } newcrc32 = php_crc32_bulk_init(); php_crc32_stream_bulk_update(&newcrc32, file, entry->uncompressed_filesize); @@ -2802,7 +2771,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0); if (!filter) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -2815,7 +2784,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname); } } - return EOF; + return; } /* create new file that holds the compressed versions */ @@ -2829,7 +2798,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (error) { spprintf(error, 0, "unable to create temporary file"); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -2840,7 +2809,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv entry->header_offset = php_stream_tell(entry->cfp); php_stream_flush(file); if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -2851,7 +2820,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } php_stream_filter_append((&entry->cfp->writefilters), filter); if (SUCCESS != php_stream_copy_to_stream_ex(file, entry->cfp, entry->uncompressed_filesize, NULL)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -2893,7 +2862,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if(manifest[0] == '\r' || manifest[0] == '\n') { manifest_len++; phar_set_32(manifest, manifest_len); - manifest_hack = 1; + manifest_hack = true; } phar_set_32(manifest+4, new_manifest_count); if (has_dirs) { @@ -2910,7 +2879,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest)) || (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -2931,7 +2900,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv && ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) { smart_str_free(&main_metadata_str); - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -2967,7 +2936,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (4 != php_stream_write(newfile, entry_buffer, 4) || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len) || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -3002,7 +2971,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer)) || (metadata_str && ZSTR_LEN(metadata_str) != php_stream_write(newfile, ZSTR_VAL(metadata_str), ZSTR_LEN(metadata_str)))) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -3016,9 +2985,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } } ZEND_HASH_FOREACH_END(); /* Hack - see bug #65028, add padding byte to the end of the manifest */ - if(manifest_hack) { + if (manifest_hack) { if(1 != php_stream_write(newfile, manifest, 1)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -3045,7 +3014,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } else { file = phar_get_efp(entry, 0); if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -3057,7 +3026,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv } if (!file) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); @@ -3071,7 +3040,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv entry->offset = entry->offset_abs = offset; offset += entry->compressed_filesize; if (php_stream_copy_to_stream_ex(file, newfile, entry->compressed_filesize, &wrote) == FAILURE) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -3134,11 +3103,11 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (digest) { efree(digest); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); - return EOF; + return; } php_stream_write(newfile, digest, digest_len); @@ -3171,7 +3140,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv phar->ufp = NULL; } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } @@ -3190,7 +3159,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (error) { spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); } - return EOF; + return; } if (phar->flags & PHAR_FILE_COMPRESSED_GZ) { @@ -3206,7 +3175,7 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (error) { spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname); } - return EOF; + return; } php_stream_filter_append(&phar->fp->writefilters, filter); @@ -3236,10 +3205,9 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv if (error) { spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname); } - return EOF; } - return EOF; + return; cleanup: if (shared_cfp != NULL) { @@ -3251,8 +3219,6 @@ int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int conv entry->header_offset = 0; } } ZEND_HASH_FOREACH_END(); - - return EOF; } /* }}} */ diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index eb836ac691adc..2f9ae7c1c84ea 100644 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -447,12 +447,12 @@ zend_result phar_copy_on_write(phar_archive_data **pphar); bool phar_is_tar(char *buf, char *fname); zend_result phar_parse_tarfile(php_stream* fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, uint32_t compression, char **error); zend_result phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error); -int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error); +void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error); /* zip functions in zip.c */ int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, char **error); int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error); -int phar_zip_flush(phar_archive_data *archive, char *user_stub, zend_long len, int defaultstub, char **error); +void phar_zip_flush(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error); #ifdef PHAR_MAIN extern const php_stream_wrapper php_stream_phar_wrapper; @@ -468,7 +468,8 @@ phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t phar_entry_info *phar_get_entry_info_dir(phar_archive_data *phar, char *path, size_t path_len, char dir, char **error, int security); phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security); zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname_len, char *path, size_t path_len, const char *mode, char allow_dir, char **error, int security); -int phar_flush(phar_archive_data *archive, char *user_stub, zend_long len, int convert, char **error); +void phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error); +void phar_flush(phar_archive_data *archive, char **error); zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, int is_complete); zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, char **entry, size_t *entry_len, int executable, int for_create); diff --git a/ext/phar/phar_object.c b/ext/phar/phar_object.c index 0d992a6dd7f6a..4a2f8726191dd 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -1812,7 +1812,7 @@ PHP_METHOD(Phar, buildFromDirectory) } phar_obj->archive->ufp = pass.fp; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -1877,7 +1877,7 @@ PHP_METHOD(Phar, buildFromIterator) if (SUCCESS == spl_iterator_apply(obj, (spl_iterator_apply_func_t) phar_build, (void *) &pass)) { phar_obj->archive->ufp = pass.fp; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); efree(error); @@ -2194,7 +2194,7 @@ static zend_object *phar_rename_archive(phar_archive_data **sphar, char *ext) /* return NULL; } - phar_flush(phar, 0, 0, 1, &error); + phar_flush_ex(phar, NULL, 1, &error); if (error) { zend_hash_str_del(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len); @@ -2642,7 +2642,7 @@ PHP_METHOD(Phar, delete) RETURN_THROWS(); } - phar_flush(phar_obj->archive, NULL, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); efree(error); @@ -2754,7 +2754,7 @@ PHP_METHOD(Phar, setAlias) phar_obj->archive->alias = estrndup(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias)); phar_obj->archive->alias_len = ZSTR_LEN(new_alias); phar_obj->archive->is_temporary_alias = 0; - phar_flush(phar_obj->archive, NULL, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { phar_obj->archive->alias = oldalias; @@ -2835,7 +2835,7 @@ PHP_METHOD(Phar, stopBuffering) } phar_obj->archive->donotflush = 0; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -2849,9 +2849,9 @@ PHP_METHOD(Phar, stopBuffering) */ PHP_METHOD(Phar, setStub) { + char *error; + zend_string *stub; zval *zstub; - char *stub, *error; - size_t stub_len; zend_long len = -1; php_stream *stream; @@ -2883,16 +2883,26 @@ PHP_METHOD(Phar, setStub) } if ((php_stream_from_zval_no_verify(stream, zstub)) != NULL) { + if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) { + zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); + RETURN_THROWS(); + } + + zend_string *stub_file_content = NULL; if (len > 0) { - len = -len; + stub_file_content = php_stream_copy_to_mem(stream, len, false); } else { - len = -1; + stub_file_content = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, false); } - if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) { - zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); + + if (stub_file_content == NULL) { + zend_throw_exception_ex(phar_ce_PharException, 0, "unable to read resource to copy stub to new phar \"%s\"", phar_obj->archive->fname); RETURN_THROWS(); } - phar_flush(phar_obj->archive, (char *) zstub, len, 0, &error); + + phar_flush_ex(phar_obj->archive, stub_file_content, /* is_default_stub */ false, &error); + zend_string_release_ex(stub_file_content, false); + if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); efree(error); @@ -2902,12 +2912,12 @@ PHP_METHOD(Phar, setStub) zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Cannot change stub, unable to read from input stream"); } - } else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &stub, &stub_len) == SUCCESS) { + } else if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &stub) == SUCCESS) { if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) { zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); RETURN_THROWS(); } - phar_flush(phar_obj->archive, stub, stub_len, 0, &error); + phar_flush_ex(phar_obj->archive, stub, /* is_default_stub */ false, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -2988,7 +2998,7 @@ PHP_METHOD(Phar, setDefaultStub) zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname); RETURN_THROWS(); } - phar_flush(phar_obj->archive, stub ? ZSTR_VAL(stub) : 0, stub ? ZSTR_LEN(stub) : 0, 1, &error); + phar_flush_ex(phar_obj->archive, stub, /* is_default_stub */ true, &error); if (created_stub) { zend_string_free(stub); @@ -3044,7 +3054,7 @@ PHP_METHOD(Phar, setSignatureAlgorithm) PHAR_G(openssl_privatekey) = key; PHAR_G(openssl_privatekey_len) = key_len; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); efree(error); @@ -3353,7 +3363,7 @@ PHP_METHOD(Phar, compressFiles) } pharobj_set_compression(&phar_obj->archive->manifest, flags); phar_obj->archive->is_modified = 1; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error); @@ -3397,7 +3407,7 @@ PHP_METHOD(Phar, decompressFiles) } phar_obj->archive->is_modified = 1; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error); @@ -3492,7 +3502,7 @@ PHP_METHOD(Phar, copy) zend_hash_str_add_mem(&oldentry->phar->manifest, ZSTR_VAL(new_file), tmp_len, &newentry, sizeof(phar_entry_info)); phar_obj->archive->is_modified = 1; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -3673,7 +3683,7 @@ static void phar_add_file(phar_archive_data **pphar, zend_string *file_name, con *pphar = data->phar; } phar_entry_delref(data); - phar_flush(*pphar, 0, 0, 0, &error); + phar_flush(*pphar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -3716,7 +3726,7 @@ static void phar_mkdir(phar_archive_data **pphar, zend_string *dir_name) *pphar = data->phar; } phar_entry_delref(data); - phar_flush(*pphar, 0, 0, 0, &error); + phar_flush(*pphar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -3800,7 +3810,7 @@ PHP_METHOD(Phar, offsetUnset) entry->is_modified = 0; entry->is_deleted = 1; /* we need to "flush" the stream to save the newly deleted file on disk */ - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4070,7 +4080,7 @@ PHP_METHOD(Phar, setMetadata) } phar_obj->archive->is_modified = 1; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4101,7 +4111,7 @@ PHP_METHOD(Phar, delMetadata) phar_metadata_tracker_free(&phar_obj->archive->metadata_tracker, phar_obj->archive->is_persistent); phar_obj->archive->is_modified = 1; - phar_flush(phar_obj->archive, 0, 0, 0, &error); + phar_flush(phar_obj->archive, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4669,7 +4679,7 @@ PHP_METHOD(PharFileInfo, chmod) BG(CurrentLStatFile) = NULL; BG(CurrentStatFile) = NULL; - phar_flush(entry_obj->entry->phar, 0, 0, 0, &error); + phar_flush(entry_obj->entry->phar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4752,7 +4762,7 @@ PHP_METHOD(PharFileInfo, setMetadata) entry_obj->entry->is_modified = 1; entry_obj->entry->phar->is_modified = 1; - phar_flush(entry_obj->entry->phar, 0, 0, 0, &error); + phar_flush(entry_obj->entry->phar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4799,7 +4809,7 @@ PHP_METHOD(PharFileInfo, delMetadata) entry_obj->entry->is_modified = 1; entry_obj->entry->phar->is_modified = 1; - phar_flush(entry_obj->entry->phar, 0, 0, 0, &error); + phar_flush(entry_obj->entry->phar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -4979,7 +4989,7 @@ PHP_METHOD(PharFileInfo, compress) entry_obj->entry->phar->is_modified = 1; entry_obj->entry->is_modified = 1; - phar_flush(entry_obj->entry->phar, 0, 0, 0, &error); + phar_flush(entry_obj->entry->phar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); @@ -5071,7 +5081,7 @@ PHP_METHOD(PharFileInfo, decompress) entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK; entry_obj->entry->phar->is_modified = 1; entry_obj->entry->is_modified = 1; - phar_flush(entry_obj->entry->phar, 0, 0, 0, &error); + phar_flush(entry_obj->entry->phar, &error); if (error) { zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error); diff --git a/ext/phar/stream.c b/ext/phar/stream.c index f5e1d9514c724..17563f7fad04f 100644 --- a/ext/phar/stream.c +++ b/ext/phar/stream.c @@ -469,17 +469,16 @@ static ssize_t phar_stream_write(php_stream *stream, const char *buf, size_t cou static int phar_stream_flush(php_stream *stream) /* {{{ */ { char *error; - int ret; phar_entry_data *data = (phar_entry_data *) stream->abstract; if (data->internal_file->is_modified) { data->internal_file->timestamp = time(0); - ret = phar_flush(data->phar, 0, 0, 0, &error); + phar_flush(data->phar, &error); if (error) { php_stream_wrapper_log_error(stream->wrapper, REPORT_ERRORS, "%s", error); efree(error); } - return ret; + return EOF; } else { return EOF; } @@ -955,7 +954,7 @@ static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from } if (is_modified) { - phar_flush(phar, 0, 0, 0, &error); + phar_flush(phar, &error); if (error) { php_url_free(resource_from); php_url_free(resource_to); diff --git a/ext/phar/tar.c b/ext/phar/tar.c index 44a8e127e233d..bfbcd4216af21 100644 --- a/ext/phar/tar.c +++ b/ext/phar/tar.c @@ -955,16 +955,17 @@ static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */ } /* }}} */ -int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error) /* {{{ */ +void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { - phar_entry_info entry = {0}; static const char newstub[] = "fname); } - return EOF; + return; } if (phar->is_data) { @@ -996,7 +997,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (entry.fp == NULL) { efree(entry.filename); spprintf(error, 0, "phar error: unable to create temporary file"); - return -1; + return; } if (phar->alias_len != php_stream_write(entry.fp, phar->alias, phar->alias_len)) { if (error) { @@ -1004,7 +1005,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int } php_stream_close(entry.fp); efree(entry.filename); - return EOF; + return; } entry.uncompressed_filesize = phar->alias_len; @@ -1017,105 +1018,61 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int } /* set stub */ - if (user_stub && !defaultstub) { - char *pos; - if (len < 0) { - /* resource passed in */ - if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) { - if (error) { - spprintf(error, 0, "unable to access resource to copy stub to new tar-based phar \"%s\"", phar->fname); - } - return EOF; - } - if (len == -1) { - len = PHP_STREAM_COPY_ALL; - } else { - len = -len; - } - user_stub = 0; - - // TODO: refactor to avoid reallocation ??? -//??? len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0) - { - zend_string *str = php_stream_copy_to_mem(stubfile, len, 0); - if (str) { - len = ZSTR_LEN(str); - user_stub = estrndup(ZSTR_VAL(str), ZSTR_LEN(str)); - zend_string_release_ex(str, 0); - } else { - user_stub = NULL; - len = 0; - } - } + if (user_stub && !is_default_stub) { + char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), sizeof(halt_stub) - 1); - if (!len || !user_stub) { - if (error) { - spprintf(error, 0, "unable to read resource to copy stub to new tar-based phar \"%s\"", phar->fname); - } - return EOF; - } - free_user_stub = 1; - } else { - free_user_stub = 0; - } - - if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) { + if (pos == NULL) { if (error) { spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname); } - if (free_user_stub) { - efree(user_stub); - } - return EOF; + return; } - len = pos - user_stub + 18; + size_t len = pos - ZSTR_VAL(user_stub) + strlen(halt_stub); + const char end_sequence[] = " ?>\r\n"; + size_t end_sequence_len = strlen(end_sequence); + entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } - entry.uncompressed_filesize = len + 5; + entry.uncompressed_filesize = len + end_sequence_len; - if ((size_t)len != php_stream_write(entry.fp, user_stub, len) - || 5 != php_stream_write(entry.fp, " ?>\r\n", 5)) { + if ( + len != php_stream_write(entry.fp, ZSTR_VAL(user_stub), len) + || end_sequence_len != php_stream_write(entry.fp, end_sequence, end_sequence_len) + ) { if (error) { spprintf(error, 0, "unable to create stub from string in new tar-based phar \"%s\"", phar->fname); } - if (free_user_stub) { - efree(user_stub); - } php_stream_close(entry.fp); - return EOF; + return; } entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); entry.filename_len = sizeof(".phar/stub.php")-1; zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info)); - - if (free_user_stub) { - efree(user_stub); - } } else { /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */ entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { php_stream_close(entry.fp); if (error) { spprintf(error, 0, "unable to %s stub in%star-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); } - return EOF; + return; } entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1; entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); entry.filename_len = sizeof(".phar/stub.php")-1; - if (!defaultstub) { + if (!is_default_stub) { if (!zend_hash_str_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) { php_stream_close(entry.fp); @@ -1123,7 +1080,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (error) { spprintf(error, 0, "unable to create stub in tar-based phar \"%s\"", phar->fname); } - return EOF; + return; } } else { php_stream_close(entry.fp); @@ -1136,11 +1093,11 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int nostub: if (phar->fp && !phar->is_brandnew) { oldfile = phar->fp; - closeoldfile = 0; + must_close_old_file = false; php_stream_rewind(oldfile); } else { oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); - closeoldfile = oldfile != NULL; + must_close_old_file = oldfile != NULL; } newfile = php_stream_fopen_tmpfile(); @@ -1148,10 +1105,10 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (error) { spprintf(error, 0, "unable to create temporary file"); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } pass.old = oldfile; @@ -1164,10 +1121,10 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int phar_entry_info *mentry; if (NULL != (mentry = zend_hash_str_find_ptr(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1))) { if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } } else { phar_entry_info newentry = {0}; @@ -1180,18 +1137,18 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (NULL == (mentry = zend_hash_str_add_mem(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1, (void *)&newentry, sizeof(phar_entry_info)))) { spprintf(error, 0, "phar tar error: unable to add magic metadata file to manifest for phar archive \"%s\"", phar->fname); - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) { zend_hash_str_del(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1); - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } } } @@ -1199,13 +1156,13 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass); if (error && *error) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } /* on error in the hash iterator above, error is set */ php_stream_close(newfile); - return EOF; + return; } zend_hash_apply_with_argument(&phar->manifest, phar_tar_writeheaders, (void *) &pass); @@ -1219,12 +1176,12 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int efree(save); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); - return EOF; + return; } entry.filename = ".phar/signature.bin"; @@ -1232,7 +1189,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } #ifdef WORDS_BIGENDIAN # define PHAR_SET_32(destination, source) do { \ @@ -1254,11 +1211,11 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int spprintf(error, 0, "phar error: unable to write signature to tar-based phar %s", phar->fname); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } php_stream_close(newfile); - return EOF; + return; } efree(signature); @@ -1267,12 +1224,12 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int entry.filename_len = phar_tar_writeheaders_int(&entry, (void *)&pass); if (error && *error) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } /* error is set by writeheaders */ php_stream_close(newfile); - return EOF; + return; } } /* signature */ @@ -1281,14 +1238,14 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int php_stream_write(newfile, buf, 1024); efree(buf); - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } /* on error in the hash iterator above, error is set */ if (error && *error) { php_stream_close(newfile); - return EOF; + return; } if (phar->fp && pass.free_fp) { @@ -1315,7 +1272,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (error) { spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname); } - return EOF; + return; } if (phar->flags & PHAR_FILE_COMPRESSED_GZ) { @@ -1339,7 +1296,7 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (error) { spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname); } - return EOF; + return; } php_stream_filter_append(&phar->fp->writefilters, filter); @@ -1366,6 +1323,5 @@ int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int php_stream_close(newfile); } } - return EOF; } /* }}} */ diff --git a/ext/phar/zip.c b/ext/phar/zip.c index 70cdfc9cc1b50..ad0e53a80c4b2 100644 --- a/ext/phar/zip.c +++ b/ext/phar/zip.c @@ -1251,14 +1251,13 @@ static int phar_zip_applysignature(phar_archive_data *phar, struct _phar_zip_pas } /* }}} */ -int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error) /* {{{ */ +void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { - char *pos; static const char newstub[] = "fname); } - return EOF; + return; } if (phar->is_data) { @@ -1289,13 +1288,13 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } if (phar->alias_len != php_stream_write(entry.fp, phar->alias, phar->alias_len)) { if (error) { spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname); } - return EOF; + return; } entry.uncompressed_filesize = entry.compressed_filesize = phar->alias_len; @@ -1310,112 +1309,67 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int /* register alias */ if (phar->alias_len) { if (FAILURE == phar_get_archive(&phar, phar->fname, phar->fname_len, phar->alias, phar->alias_len, error)) { - return EOF; + return; } } /* set stub */ - if (user_stub && !defaultstub) { - if (len < 0) { - /* resource passed in */ - if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) { - if (error) { - spprintf(error, 0, "unable to access resource to copy stub to new zip-based phar \"%s\"", phar->fname); - } - return EOF; - } - - if (len == -1) { - len = PHP_STREAM_COPY_ALL; - } else { - len = -len; - } - - user_stub = 0; - - // TODO: refactor to avoid reallocation ??? -//??? len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0) - { - zend_string *str = php_stream_copy_to_mem(stubfile, len, 0); - if (str) { - len = ZSTR_LEN(str); - user_stub = estrndup(ZSTR_VAL(str), ZSTR_LEN(str)); - zend_string_release_ex(str, 0); - } else { - user_stub = NULL; - len = 0; - } - } + if (user_stub && !is_default_stub) { + char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub)); - if (!len || !user_stub) { - if (error) { - spprintf(error, 0, "unable to read resource to copy stub to new zip-based phar \"%s\"", phar->fname); - } - return EOF; - } - free_user_stub = 1; - } else { - free_user_stub = 0; - } - - if ((pos = php_stristr(user_stub, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) { + if (pos == NULL) { if (error) { spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname); } - if (free_user_stub) { - efree(user_stub); - } - return EOF; + return; } - len = pos - user_stub + 18; + size_t len = pos - ZSTR_VAL(user_stub) + strlen(halt_stub); + const char end_sequence[] = " ?>\r\n"; + size_t end_sequence_len = strlen(end_sequence); + entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } - entry.uncompressed_filesize = len + 5; + entry.uncompressed_filesize = len + end_sequence_len; - if ((size_t)len != php_stream_write(entry.fp, user_stub, len) - || 5 != php_stream_write(entry.fp, " ?>\r\n", 5)) { + if ( + len != php_stream_write(entry.fp, ZSTR_VAL(user_stub), len) + || end_sequence_len != php_stream_write(entry.fp, end_sequence, end_sequence_len) + ) { if (error) { spprintf(error, 0, "unable to create stub from string in new zip-based phar \"%s\"", phar->fname); } - if (free_user_stub) { - efree(user_stub); - } php_stream_close(entry.fp); - return EOF; + return; } entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); entry.filename_len = sizeof(".phar/stub.php")-1; zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info)); - - if (free_user_stub) { - efree(user_stub); - } } else { /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */ entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { spprintf(error, 0, "phar error: unable to create temporary file"); - return EOF; + return; } if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) { php_stream_close(entry.fp); if (error) { spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); } - return EOF; + return; } entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1; entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1); entry.filename_len = sizeof(".phar/stub.php")-1; - if (!defaultstub) { + if (!is_default_stub) { if (!zend_hash_str_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) { if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) { php_stream_close(entry.fp); @@ -1423,7 +1377,7 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (error) { spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname); } - return EOF; + return; } } else { php_stream_close(entry.fp); @@ -1436,11 +1390,11 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int nostub: if (phar->fp && !phar->is_brandnew) { oldfile = phar->fp; - closeoldfile = 0; + must_close_old_file = false; php_stream_rewind(oldfile); } else { oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); - closeoldfile = oldfile != NULL; + must_close_old_file = oldfile != NULL; } /* save modified files to the zip */ @@ -1449,13 +1403,13 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int if (!pass.filefp) { fperror: - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } if (error) { spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to open temporary file", phar->fname); } - return EOF; + return; } pass.centralfp = php_stream_fopen_tmpfile(); @@ -1490,10 +1444,10 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int php_stream_close(pass.centralfp); nocentralerror: php_stream_close(pass.filefp); - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; + return; } if (FAILURE == phar_zip_applysignature(phar, &pass)) { @@ -1567,14 +1521,14 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int } else { phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL); if (!phar->fp) { - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } phar->fp = pass.filefp; if (error) { spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); } - return EOF; + return; } php_stream_rewind(pass.filefp); php_stream_copy_to_stream_ex(pass.filefp, phar->fp, PHP_STREAM_COPY_ALL, NULL); @@ -1582,9 +1536,8 @@ int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int php_stream_close(pass.filefp); } - if (closeoldfile) { + if (must_close_old_file) { php_stream_close(oldfile); } - return EOF; } /* }}} */ diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index afb156ad7d8b2..51c537de6e628 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -42,7 +42,7 @@ PHPAPI zend_string *php_addcslashes(zend_string *str, const char *what, size_t w PHPAPI void php_stripcslashes(zend_string *str); PHPAPI zend_string *php_basename(const char *s, size_t len, const char *suffix, size_t sufflen); PHPAPI size_t php_dirname(char *str, size_t len); -PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len); +PHPAPI char *php_stristr(const char *s, const char *t, size_t s_len, size_t t_len); PHPAPI zend_string *php_str_to_str(const char *haystack, size_t length, const char *needle, size_t needle_len, const char *str, size_t str_len); PHPAPI zend_string *php_trim(zend_string *str, const char *what, size_t what_len, int mode); diff --git a/ext/standard/string.c b/ext/standard/string.c index 6ebe7db08965f..d40b076efa55f 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1669,7 +1669,7 @@ PHP_FUNCTION(pathinfo) /* {{{ php_stristr case insensitive strstr */ -PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len) +PHPAPI char *php_stristr(const char *s, const char *t, size_t s_len, size_t t_len) { return (char*)php_memnistr(s, t, t_len, s + s_len); }