ext/phar: Use bool instead of int

This commit is contained in:
Gina Peter Bnayard 2024-09-13 13:29:09 +02:00 committed by Gina Peter Banyard
parent 03e2cfdad1
commit b75c79ee1d
3 changed files with 52 additions and 50 deletions

View file

@ -2535,8 +2535,9 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
zend_string *newstub; zend_string *newstub;
phar_entry_info *entry, *newentry; phar_entry_info *entry, *newentry;
size_t halt_offset; size_t halt_offset;
int restore_alias_len, global_flags = 0, closeoldfile; int restore_alias_len, global_flags = 0;
bool has_dirs = 0; bool must_close_old_file = false;
bool has_dirs = false;
char manifest[18], entry_buffer[24]; char manifest[18], entry_buffer[24];
zend_off_t manifest_ftell; zend_off_t manifest_ftell;
zend_long offset; zend_long offset;
@ -2547,8 +2548,9 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
php_stream_filter *filter; php_stream_filter *filter;
php_serialize_data_t metadata_hash; php_serialize_data_t metadata_hash;
smart_str main_metadata_str = {0}; smart_str main_metadata_str = {0};
int free_fp = 1, free_ufp = 1; bool free_fp = true;
int manifest_hack = 0; bool free_ufp = true;
bool manifest_hack = false;
php_stream *shared_cfp = NULL; php_stream *shared_cfp = NULL;
if (phar->is_persistent) { if (phar->is_persistent) {
@ -2582,18 +2584,18 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (phar->fp && !phar->is_brandnew) { if (phar->fp && !phar->is_brandnew) {
oldfile = phar->fp; oldfile = phar->fp;
closeoldfile = 0; must_close_old_file = false;
php_stream_rewind(oldfile); php_stream_rewind(oldfile);
} else { } else {
oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
closeoldfile = oldfile != NULL; must_close_old_file = oldfile != NULL;
} }
newfile = php_stream_fopen_tmpfile(); newfile = php_stream_fopen_tmpfile();
if (!newfile) { if (!newfile) {
if (error) { if (error) {
spprintf(error, 0, "unable to create temporary file"); spprintf(error, 0, "unable to create temporary file");
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -2603,7 +2605,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub)); char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub));
if (pos == NULL) { if (pos == NULL) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2621,7 +2623,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
len != php_stream_write(newfile, ZSTR_VAL(user_stub), len) len != php_stream_write(newfile, ZSTR_VAL(user_stub), len)
|| end_sequence_len != php_stream_write(newfile, end_sequence, end_sequence_len) || end_sequence_len != php_stream_write(newfile, end_sequence, end_sequence_len)
) { ) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2644,7 +2646,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset); written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset);
} }
if (phar->halt_offset != written) { if (phar->halt_offset != written) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2697,10 +2699,10 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
/* open file pointers refer to this fp, do not free the stream */ /* open file pointers refer to this fp, do not free the stream */
switch (entry->fp_type) { switch (entry->fp_type) {
case PHAR_FP: case PHAR_FP:
free_fp = 0; free_fp = false;
break; break;
case PHAR_UFP: case PHAR_UFP:
free_ufp = 0; free_ufp = false;
default: default:
break; break;
} }
@ -2711,7 +2713,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (entry->is_dir) { if (entry->is_dir) {
/* we use this to calculate API version, 1.1.1 is used for phars with directories */ /* 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) { if (!Z_ISUNDEF(entry->metadata_tracker.val) && !entry->metadata_tracker.str) {
ZEND_ASSERT(!entry->is_persistent); ZEND_ASSERT(!entry->is_persistent);
@ -2747,7 +2749,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} }
file = phar_get_efp(entry, 0); file = phar_get_efp(entry, 0);
if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) { 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(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2767,7 +2769,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} }
filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0); filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0);
if (!filter) { if (!filter) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2794,7 +2796,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (error) { if (error) {
spprintf(error, 0, "unable to create temporary file"); spprintf(error, 0, "unable to create temporary file");
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2805,7 +2807,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
entry->header_offset = php_stream_tell(entry->cfp); entry->header_offset = php_stream_tell(entry->cfp);
php_stream_flush(file); php_stream_flush(file);
if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 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(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2816,7 +2818,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} }
php_stream_filter_append((&entry->cfp->writefilters), filter); php_stream_filter_append((&entry->cfp->writefilters), filter);
if (SUCCESS != php_stream_copy_to_stream_ex(file, entry->cfp, entry->uncompressed_filesize, NULL)) { 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(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2858,7 +2860,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if(manifest[0] == '\r' || manifest[0] == '\n') { if(manifest[0] == '\r' || manifest[0] == '\n') {
manifest_len++; manifest_len++;
phar_set_32(manifest, manifest_len); phar_set_32(manifest, manifest_len);
manifest_hack = 1; manifest_hack = true;
} }
phar_set_32(manifest+4, new_manifest_count); phar_set_32(manifest+4, new_manifest_count);
if (has_dirs) { if (has_dirs) {
@ -2875,7 +2877,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest)) if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
|| (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) { || (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); php_stream_close(oldfile);
} }
@ -2896,7 +2898,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
&& ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) { && 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); smart_str_free(&main_metadata_str);
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
@ -2932,7 +2934,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (4 != php_stream_write(newfile, entry_buffer, 4) if (4 != php_stream_write(newfile, entry_buffer, 4)
|| entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len) || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len)
|| (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) { || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -2967,7 +2969,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer)) if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer))
|| (metadata_str && || (metadata_str &&
ZSTR_LEN(metadata_str) != php_stream_write(newfile, ZSTR_VAL(metadata_str), ZSTR_LEN(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); php_stream_close(oldfile);
} }
@ -2981,9 +2983,9 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} }
} ZEND_HASH_FOREACH_END(); } ZEND_HASH_FOREACH_END();
/* Hack - see bug #65028, add padding byte to the end of the manifest */ /* 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(1 != php_stream_write(newfile, manifest, 1)) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
@ -3010,7 +3012,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} else { } else {
file = phar_get_efp(entry, 0); file = phar_get_efp(entry, 0);
if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 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(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -3022,7 +3024,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
} }
if (!file) { if (!file) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -3036,7 +3038,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
entry->offset = entry->offset_abs = offset; entry->offset = entry->offset_abs = offset;
offset += entry->compressed_filesize; offset += entry->compressed_filesize;
if (php_stream_copy_to_stream_ex(file, newfile, entry->compressed_filesize, &wrote) == FAILURE) { 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); php_stream_close(oldfile);
} }
@ -3099,7 +3101,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
if (digest) { if (digest) {
efree(digest); efree(digest);
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -3136,7 +3138,7 @@ int phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defau
phar->ufp = NULL; phar->ufp = NULL;
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }

View file

@ -960,7 +960,7 @@ int phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
phar_entry_info entry = {0}; phar_entry_info entry = {0};
static const char newstub[] = "<?php // tar-based phar archive stub file\n__HALT_COMPILER();"; static const char newstub[] = "<?php // tar-based phar archive stub file\n__HALT_COMPILER();";
php_stream *oldfile, *newfile; php_stream *oldfile, *newfile;
int closeoldfile; bool must_close_old_file = false;
size_t signature_length; size_t signature_length;
struct _phar_pass_tar_info pass; struct _phar_pass_tar_info pass;
char *buf, *signature, sigbuf[8]; char *buf, *signature, sigbuf[8];
@ -1092,11 +1092,11 @@ int phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
nostub: nostub:
if (phar->fp && !phar->is_brandnew) { if (phar->fp && !phar->is_brandnew) {
oldfile = phar->fp; oldfile = phar->fp;
closeoldfile = 0; must_close_old_file = false;
php_stream_rewind(oldfile); php_stream_rewind(oldfile);
} else { } else {
oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
closeoldfile = oldfile != NULL; must_close_old_file = oldfile != NULL;
} }
newfile = php_stream_fopen_tmpfile(); newfile = php_stream_fopen_tmpfile();
@ -1104,7 +1104,7 @@ nostub:
if (error) { if (error) {
spprintf(error, 0, "unable to create temporary file"); spprintf(error, 0, "unable to create temporary file");
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -1120,7 +1120,7 @@ nostub:
phar_entry_info *mentry; phar_entry_info *mentry;
if (NULL != (mentry = zend_hash_str_find_ptr(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1))) { 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 (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -1136,7 +1136,7 @@ nostub:
if (NULL == (mentry = zend_hash_str_add_mem(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1, (void *)&newentry, sizeof(phar_entry_info)))) { 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); 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); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -1144,7 +1144,7 @@ nostub:
if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata_tracker, mentry, error)) { 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); 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); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -1155,7 +1155,7 @@ nostub:
zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass); zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass);
if (error && *error) { if (error && *error) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
@ -1175,7 +1175,7 @@ nostub:
efree(save); efree(save);
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
@ -1210,7 +1210,7 @@ nostub:
spprintf(error, 0, "phar error: unable to write signature to tar-based phar %s", phar->fname); 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(oldfile);
} }
php_stream_close(newfile); php_stream_close(newfile);
@ -1223,7 +1223,7 @@ nostub:
entry.filename_len = phar_tar_writeheaders_int(&entry, (void *)&pass); entry.filename_len = phar_tar_writeheaders_int(&entry, (void *)&pass);
if (error && *error) { if (error && *error) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
/* error is set by writeheaders */ /* error is set by writeheaders */
@ -1237,7 +1237,7 @@ nostub:
php_stream_write(newfile, buf, 1024); php_stream_write(newfile, buf, 1024);
efree(buf); efree(buf);
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }

View file

@ -1257,7 +1257,7 @@ int phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
char halt_stub[] = "__HALT_COMPILER();"; char halt_stub[] = "__HALT_COMPILER();";
php_stream *oldfile; php_stream *oldfile;
int closeoldfile = 0; bool must_close_old_file = false;
phar_entry_info entry = {0}; phar_entry_info entry = {0};
char *temperr = NULL; char *temperr = NULL;
struct _phar_zip_pass pass; struct _phar_zip_pass pass;
@ -1268,7 +1268,7 @@ int phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
entry.flags = PHAR_ENT_PERM_DEF_FILE; entry.flags = PHAR_ENT_PERM_DEF_FILE;
entry.timestamp = time(NULL); entry.timestamp = time(NULL);
entry.is_modified = 1; entry.is_modified = 1;
entry.is_zip = 1; entry.is_zip = true;
entry.phar = phar; entry.phar = phar;
entry.fp_type = PHAR_MOD; entry.fp_type = PHAR_MOD;
@ -1390,11 +1390,11 @@ int phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_defa
nostub: nostub:
if (phar->fp && !phar->is_brandnew) { if (phar->fp && !phar->is_brandnew) {
oldfile = phar->fp; oldfile = phar->fp;
closeoldfile = 0; must_close_old_file = false;
php_stream_rewind(oldfile); php_stream_rewind(oldfile);
} else { } else {
oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL); 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 */ /* save modified files to the zip */
@ -1403,7 +1403,7 @@ nostub:
if (!pass.filefp) { if (!pass.filefp) {
fperror: fperror:
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
if (error) { if (error) {
@ -1444,7 +1444,7 @@ temperror:
php_stream_close(pass.centralfp); php_stream_close(pass.centralfp);
nocentralerror: nocentralerror:
php_stream_close(pass.filefp); php_stream_close(pass.filefp);
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
return EOF; return EOF;
@ -1521,7 +1521,7 @@ nocentralerror:
} else { } else {
phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL); phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
if (!phar->fp) { if (!phar->fp) {
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
phar->fp = pass.filefp; phar->fp = pass.filefp;
@ -1536,7 +1536,7 @@ nocentralerror:
php_stream_close(pass.filefp); php_stream_close(pass.filefp);
} }
if (closeoldfile) { if (must_close_old_file) {
php_stream_close(oldfile); php_stream_close(oldfile);
} }
return EOF; return EOF;