diff --git a/ext/phar/phar.c b/ext/phar/phar.c index 76a580988fb62..b2f0594bd5107 100644 --- a/ext/phar/phar.c +++ b/ext/phar/phar.c @@ -414,7 +414,8 @@ void phar_entry_delref(phar_entry_data *idata) /* {{{ */ /** * Removes an entry, either by actually removing it or by marking it. */ -void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */ +// TODO: Move function into ext/phar/stream.c as it is only ever used there? +ZEND_ATTRIBUTE_NONNULL void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */ { phar_archive_data *phar; @@ -510,7 +511,6 @@ static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *al && ((alias && fname_len == phar->fname_len && !strncmp(fname, phar->fname, fname_len)) || !alias) ) { - phar_entry_info *stub; #ifdef PHP_WIN32 if (fname != save_fname) { free_alloca(fname, fname_use_heap); @@ -526,7 +526,7 @@ static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *al if (!is_data) { /* prevent any ".phar" without a stub getting through */ if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) { - if (PHAR_G(readonly) && NULL == (stub = zend_hash_str_find_ptr(&(phar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) { + if (PHAR_G(readonly) && !zend_hash_str_exists(&(phar->manifest), ZEND_STRL(".phar/stub.php"))) { if (error) { spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname); } @@ -1315,7 +1315,7 @@ static zend_result phar_parse_pharfile(php_stream *fp, char *fname, size_t fname /** * Create or open a phar for writing */ -zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */ { const char *ext_str, *z; char *my_error; @@ -1324,9 +1324,7 @@ zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *al test = &unused; - if (error) { - *error = NULL; - } + *error = NULL; /* first try to open an existing file */ if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1) == SUCCESS) { @@ -1335,31 +1333,24 @@ zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *al /* next try to create a new file */ if (FAILURE == phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 1, 1)) { - if (error) { - if (ext_len == -2) { - spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname); - } else { - spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname); - } + if (ext_len == -2) { + spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname); + } else { + spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname); } return FAILURE; } check_file: if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error) == SUCCESS) { - if (pphar) { - *pphar = *test; - } + *pphar = *test; if ((*test)->is_data && !(*test)->is_tar && !(*test)->is_zip) { - if (error) { - spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname); - } + spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname); return FAILURE; } if (PHAR_G(readonly) && !(*test)->is_data && ((*test)->is_tar || (*test)->is_zip)) { - phar_entry_info *stub; - if (NULL == (stub = zend_hash_str_find_ptr(&((*test)->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) { + if (!zend_hash_str_exists(&((*test)->manifest), ZEND_STRL(".phar/stub.php"))) { spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname); return FAILURE; } @@ -1370,11 +1361,7 @@ zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *al } return SUCCESS; } else if (my_error) { - if (error) { - *error = my_error; - } else { - efree(my_error); - } + *error = my_error; return FAILURE; } @@ -2324,29 +2311,23 @@ zend_result phar_split_fname(const char *filename, size_t filename_len, char **a * Invoked when a user calls Phar::mapPhar() from within an executing .phar * to set up its manifest directly */ -zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(3) zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error) /* {{{ */ { - if (error) { - *error = NULL; - } + *error = NULL; zend_string *fname = zend_get_executed_filename_ex(); if (!fname) { - if (error) { - spprintf(error, 0, "cannot initialize a phar outside of PHP execution"); - } + *error = estrdup("cannot initialize a phar outside of PHP execution"); return FAILURE; } - if (phar_open_parsed_phar(ZSTR_VAL(fname), ZSTR_LEN(fname), alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) { + if (phar_open_parsed_phar(ZSTR_VAL(fname), ZSTR_LEN(fname), alias, alias_len, 0, REPORT_ERRORS, NULL, NULL) == SUCCESS) { return SUCCESS; } - if (0 == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) { - if (error) { - spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar"); - } + if (NULL == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) { + *error = estrdup("__HALT_COMPILER(); must be declared in a phar"); return FAILURE; } @@ -2359,9 +2340,7 @@ zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **er fp = php_stream_open_wrapper(ZSTR_VAL(fname), "rb", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, &actual); if (!fp) { - if (error) { - spprintf(error, 0, "unable to open phar for reading \"%s\"", ZSTR_VAL(fname)); - } + spprintf(error, 0, "unable to open phar for reading \"%s\"", ZSTR_VAL(fname)); if (actual) { zend_string_release_ex(actual, 0); } @@ -2385,14 +2364,12 @@ zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **er /** * Validate the CRC32 of a file opened from within the phar */ -zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */ { php_stream *fp = idata->fp; phar_entry_info *entry = idata->internal_file; - if (error) { - *error = NULL; - } + *error = NULL; if (entry->is_zip && process_zip > 0) { /* verify local file header */ @@ -2400,14 +2377,15 @@ zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char * phar_zip_data_desc desc; if (SUCCESS != phar_open_archive_fp(idata->phar)) { - spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"", idata->phar->fname, ZSTR_VAL(entry->filename)); + spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"", + idata->phar->fname, ZSTR_VAL(entry->filename)); return FAILURE; } php_stream_seek(phar_get_entrypfp(idata->internal_file), entry->header_offset, SEEK_SET); if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &local, sizeof(local))) { - - spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")", idata->phar->fname, ZSTR_VAL(entry->filename)); + spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")", + idata->phar->fname, ZSTR_VAL(entry->filename)); return FAILURE; } @@ -2420,7 +2398,8 @@ zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char * entry->compressed_filesize, SEEK_SET); if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &desc, sizeof(desc))) { - spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")", idata->phar->fname, ZSTR_VAL(entry->filename)); + spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")", + idata->phar->fname, ZSTR_VAL(entry->filename)); return FAILURE; } if (desc.signature[0] == 'P' && desc.signature[1] == 'K') { @@ -2432,7 +2411,8 @@ zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char * } /* verify local header */ if (ZSTR_LEN(entry->filename) != PHAR_ZIP_16(local.filename_len) || entry->crc32 != PHAR_ZIP_32(local.crc32) || entry->uncompressed_filesize != PHAR_ZIP_32(local.uncompsize) || entry->compressed_filesize != PHAR_ZIP_32(local.compsize)) { - spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (local header of file \"%s\" does not match central directory)", idata->phar->fname, ZSTR_VAL(entry->filename)); + spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (local header of file \"%s\" does not match central directory)", + idata->phar->fname, ZSTR_VAL(entry->filename)); return FAILURE; } @@ -2460,7 +2440,8 @@ zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char * entry->is_crc_checked = 1; return SUCCESS; } else { - spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", idata->phar->fname, ZSTR_VAL(entry->filename)); + spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", + idata->phar->fname, ZSTR_VAL(entry->filename)); return FAILURE; } } @@ -2531,7 +2512,7 @@ zend_string *phar_create_default_stub(const char *index_php, const char *web_ind } /* }}} */ -void phar_flush(phar_archive_data *phar, char **error) { +ZEND_ATTRIBUTE_NONNULL void phar_flush(phar_archive_data *phar, char **error) { phar_flush_ex(phar, NULL, false, error); } @@ -2540,7 +2521,7 @@ void phar_flush(phar_archive_data *phar, char **error) { * * if user_stub is NULL the default or existing stub should be used */ -void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { static const char halt_stub[] = "__HALT_COMPILER();"; @@ -2565,15 +2546,11 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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); - } + spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname); return; } - if (error) { - *error = NULL; - } + *error = NULL; if (!zend_hash_num_elements(&phar->manifest) && !user_stub) { return; @@ -2605,9 +2582,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa } newfile = php_stream_fopen_tmpfile(); if (!newfile) { - if (error) { - spprintf(error, 0, "unable to create temporary file"); - } + *error = estrdup("unable to create temporary file"); if (must_close_old_file) { php_stream_close(oldfile); } @@ -2622,9 +2597,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(oldfile); } php_stream_close(newfile); - if (error) { - spprintf(error, 0, "illegal stub for phar \"%s\" (__HALT_COMPILER(); is missing)", phar->fname); - } + spprintf(error, 0, "illegal stub for phar \"%s\" (__HALT_COMPILER(); is missing)", phar->fname); return; } @@ -2640,9 +2613,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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); - } + spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname); return; } phar->halt_offset = len + end_sequence_len; @@ -2663,12 +2634,10 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(oldfile); } php_stream_close(newfile); - if (error) { - 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 (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 (new_stub) { zend_string_free(new_stub); @@ -2766,9 +2735,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); return; } newcrc32 = php_crc32_bulk_init(); @@ -2786,15 +2753,10 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(oldfile); } php_stream_close(newfile); - if (entry->flags & PHAR_ENT_COMPRESSED_GZ) { - if (error) { - spprintf(error, 0, "unable to gzip compress file \"%s\" to new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } - } else { - if (error) { - spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } - } + spprintf(error, 0, "unable to %s compress file \"%s\" to new phar \"%s\"", + entry->flags & PHAR_ENT_COMPRESSED_GZ ? "gzip" : "bzip2", + ZSTR_VAL(entry->filename), + phar->fname); return; } @@ -2807,9 +2769,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa entry->cfp = shared_cfp; if (!entry->cfp) { php_stream_filter_free(filter); - if (error) { - spprintf(error, 0, "unable to create temporary file"); - } + *error = estrdup("unable to create temporary file"); if (must_close_old_file) { php_stream_close(oldfile); } @@ -2826,9 +2786,8 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", + ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } php_stream_filter_append((&entry->cfp->writefilters), filter); @@ -2838,9 +2797,8 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(oldfile); } php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", + ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } php_stream_filter_flush(filter, 1); @@ -2900,9 +2858,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(newfile); phar->alias_len = restore_alias_len; - if (error) { - spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname); goto cleanup; } @@ -2921,9 +2877,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(newfile); phar->alias_len = restore_alias_len; - if (error) { - spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname); goto cleanup; } @@ -2954,12 +2908,10 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(oldfile); } php_stream_close(newfile); - if (error) { - if (entry->is_dir) { - spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } else { - spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } + if (entry->is_dir) { + spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); + } else { + spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); } goto cleanup; } @@ -2991,9 +2943,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } @@ -3007,9 +2957,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to write manifest padding byte"); - } + *error = estrdup("unable to write manifest padding byte"); goto cleanup; } @@ -3032,9 +2980,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } } @@ -3044,9 +2990,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa 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\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } @@ -3060,9 +3004,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa php_stream_close(newfile); - if (error) { - spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); - } + spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", ZSTR_VAL(entry->filename), phar->fname); goto cleanup; } @@ -3108,12 +3050,11 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa char *digest = NULL; size_t digest_len; - if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, error)) { - if (error) { - char *save = *error; - spprintf(error, 0, "phar error: unable to write signature: %s", save); - efree(save); - } + char *signature_error = NULL; + if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, &signature_error)) { + spprintf(error, 0, "phar error: unable to write signature: %s", signature_error); + efree(signature_error); + if (digest) { efree(digest); } @@ -3170,9 +3111,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL); if (!phar->fp) { phar->fp = newfile; - if (error) { - spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); - } + spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); return; } @@ -3186,9 +3125,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa zend_array_destroy(Z_ARR(filterparams)); if (!filter) { - 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); - } + 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; } @@ -3216,9 +3153,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa } if (-1 == php_stream_seek(phar->fp, phar->halt_offset, SEEK_SET)) { - if (error) { - spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname); } return; diff --git a/ext/phar/phar_internal.h b/ext/phar/phar_internal.h index 736226783bef1..843768d159d14 100644 --- a/ext/phar/phar_internal.h +++ b/ext/phar/phar_internal.h @@ -404,11 +404,11 @@ void phar_request_initialize(void); void phar_object_init(void); void phar_destroy_phar_data(phar_archive_data *phar); -zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip); +ZEND_ATTRIBUTE_NONNULL zend_result phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip); zend_result phar_open_from_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, uint32_t options, phar_archive_data** pphar, char **error); -zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error); zend_result phar_create_or_parse_filename(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error); -zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error); +ZEND_ATTRIBUTE_NONNULL_ARGS(3) zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **error); zend_result phar_free_alias(phar_archive_data *phar, char *alias, size_t alias_len); zend_result phar_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, char *alias, size_t alias_len, char **error); zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t sig_type, char *sig, size_t sig_len, char *fname, char **signature, size_t *signature_len, char **error); @@ -424,7 +424,7 @@ void phar_add_virtual_dirs(phar_archive_data *phar, char *filename, size_t filen zend_result phar_mount_entry(phar_archive_data *phar, char *filename, size_t filename_len, char *path, size_t path_len); zend_string *phar_find_in_include_path(zend_string *file, phar_archive_data **pphar); char *phar_fix_filepath(char *path, size_t *new_len, int use_cwd); -phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error); +ZEND_ATTRIBUTE_NONNULL phar_entry_info * phar_open_jit(const phar_archive_data *phar, phar_entry_info *entry, char **error); void phar_parse_metadata_lazy(const char *buffer, phar_metadata_tracker *tracker, uint32_t zip_metadata_len, bool persistent); bool phar_metadata_tracker_has_data(const phar_metadata_tracker* tracker, bool persistent); /* If this has data, free it and set all values to undefined. */ @@ -436,8 +436,8 @@ zend_result phar_metadata_tracker_unserialize_or_copy(phar_metadata_tracker* tra void destroy_phar_manifest_entry(zval *zv); int phar_seek_efp(phar_entry_info *entry, zend_off_t offset, int whence, zend_off_t position, int follow_links); php_stream *phar_get_efp(phar_entry_info *entry, int follow_links); -zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error); -zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links); +ZEND_ATTRIBUTE_NONNULL zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error); +ZEND_ATTRIBUTE_NONNULL zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links); phar_entry_info *phar_get_link_source(phar_entry_info *entry); zend_result phar_open_archive_fp(phar_archive_data *phar); zend_result phar_copy_on_write(phar_archive_data **pphar); @@ -445,13 +445,13 @@ zend_result phar_copy_on_write(phar_archive_data **pphar); /* tar functions in tar.c */ 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); -void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) 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); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) 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); -void phar_zip_flush(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) zend_result 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); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) 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; @@ -465,10 +465,10 @@ void phar_entry_delref(phar_entry_data *idata); phar_entry_info *phar_get_entry_info(phar_archive_data *phar, char *path, size_t path_len, char **error, int security); 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); -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_ATTRIBUTE_NONNULL 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_ATTRIBUTE_NONNULL 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); +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) void phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error); +ZEND_ATTRIBUTE_NONNULL 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 9982074f73a81..4a04e21c96178 100644 --- a/ext/phar/phar_object.c +++ b/ext/phar/phar_object.c @@ -2184,7 +2184,7 @@ static zend_object *phar_rename_archive(phar_archive_data **sphar, char *ext) /* goto err_oldpath; } - phar_flush_ex(phar, NULL, 1, &error); + phar_flush_ex(phar, NULL, true, &error); if (error) { zend_hash_str_del(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len); @@ -4133,7 +4133,7 @@ PHP_METHOD(Phar, delMetadata) } /* }}} */ -static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, char *dest, size_t dest_len, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, const zend_string *dest, char **error) /* {{{ */ { php_stream_statbuf ssb; size_t len; @@ -4161,7 +4161,7 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha if (virtual_file_ex(&new_state, ZSTR_VAL(entry->filename), NULL, CWD_EXPAND) != 0 || new_state.cwd_length <= 1) { if (EINVAL == errno && ZSTR_LEN(entry->filename) > 50) { - spprintf(error, 4096, "Cannot extract \"%.50s...\" to \"%s...\", extracted filename is too long for filesystem", ZSTR_VAL(entry->filename), dest); + spprintf(error, 4096, "Cannot extract \"%.50s...\" to \"%s...\", extracted filename is too long for filesystem", ZSTR_VAL(entry->filename), ZSTR_VAL(dest)); } else { spprintf(error, 4096, "Cannot extract \"%s\", internal error", ZSTR_VAL(entry->filename)); } @@ -4183,7 +4183,7 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha } #endif - len = spprintf(&fullpath, 0, "%s/%s", dest, filename); + len = spprintf(&fullpath, 0, "%s/%s", ZSTR_VAL(dest), filename); if (len >= MAXPATHLEN) { /* truncate for error message */ @@ -4224,9 +4224,9 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha slash = zend_memrchr(filename, '/', filename_len); if (slash) { - fullpath[dest_len + (slash - filename) + 1] = '\0'; + fullpath[ZSTR_LEN(dest) + (slash - filename) + 1] = '\0'; } else { - fullpath[dest_len] = '\0'; + fullpath[ZSTR_LEN(dest)] = '\0'; } if (FAILURE == php_stream_stat_path(fullpath, &ssb)) { @@ -4248,9 +4248,9 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha } if (slash) { - fullpath[dest_len + (slash - filename) + 1] = '/'; + fullpath[ZSTR_LEN(dest) + (slash - filename) + 1] = '/'; } else { - fullpath[dest_len] = '/'; + fullpath[ZSTR_LEN(dest)] = '/'; } filename = NULL; @@ -4270,9 +4270,10 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha } if ((phar_get_fp_type(entry) == PHAR_FP && (entry->flags & PHAR_ENT_COMPRESSION_MASK)) || !phar_get_efp(entry, 0)) { - if (FAILURE == phar_open_entry_fp(entry, error, 1)) { - if (error) { - spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", ZSTR_VAL(entry->filename), fullpath, *error); + char *open_entry_error = NULL; + if (FAILURE == phar_open_entry_fp(entry, &open_entry_error, 1)) { + if (open_entry_error) { + spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", ZSTR_VAL(entry->filename), fullpath, open_entry_error); } else { spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", ZSTR_VAL(entry->filename), fullpath); } @@ -4310,28 +4311,28 @@ static zend_result phar_extract_file(bool overwrite, phar_entry_info *entry, cha } /* }}} */ -static int extract_helper(phar_archive_data *archive, zend_string *search, char *pathto, size_t pathto_len, bool overwrite, char **error) { /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 3, 5) static int extract_helper(const phar_archive_data *archive, zend_string *search, const zend_string *pathto, bool overwrite, char **error) { /* {{{ */ int extracted = 0; phar_entry_info *entry; if (!search) { /* nothing to match -- extract all files */ ZEND_HASH_MAP_FOREACH_PTR(&archive->manifest, entry) { - if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1; + if (FAILURE == phar_extract_file(overwrite, entry, pathto, error)) return -1; extracted++; } ZEND_HASH_FOREACH_END(); } else if (ZSTR_LEN(search) > 0 && '/' == ZSTR_VAL(search)[ZSTR_LEN(search) - 1]) { /* ends in "/" -- extract all entries having that prefix */ ZEND_HASH_MAP_FOREACH_PTR(&archive->manifest, entry) { if (!zend_string_starts_with(entry->filename, search)) continue; - if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1; + if (FAILURE == phar_extract_file(overwrite, entry, pathto, error)) return -1; extracted++; } ZEND_HASH_FOREACH_END(); } else { /* otherwise, looking for an exact match */ entry = zend_hash_find_ptr(&archive->manifest, search); if (NULL == entry) return 0; - if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, error)) return -1; + if (FAILURE == phar_extract_file(overwrite, entry, pathto, error)) return -1; return 1; } @@ -4344,9 +4345,8 @@ PHP_METHOD(Phar, extractTo) { php_stream *fp; php_stream_statbuf ssb; - char *pathto; + zend_string *path_to; zend_string *filename = NULL; - size_t pathto_len; int ret; zval *zval_file; HashTable *files_ht = NULL; @@ -4354,7 +4354,7 @@ PHP_METHOD(Phar, extractTo) char *error = NULL; ZEND_PARSE_PARAMETERS_START(1, 3) - Z_PARAM_PATH(pathto, pathto_len) + Z_PARAM_PATH_STR(path_to) Z_PARAM_OPTIONAL Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(files_ht, filename) Z_PARAM_BOOL(overwrite) @@ -4372,30 +4372,30 @@ PHP_METHOD(Phar, extractTo) php_stream_close(fp); - if (pathto_len < 1) { + if (ZSTR_LEN(path_to) < 1) { zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Invalid argument, extraction path must be non-zero length"); RETURN_THROWS(); } - if (pathto_len >= MAXPATHLEN) { - char *tmp = estrndup(pathto, 50); + if (ZSTR_LEN(path_to) >= MAXPATHLEN) { + char *tmp = estrndup(ZSTR_VAL(path_to), 50); /* truncate for error message */ zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp); efree(tmp); RETURN_THROWS(); } - if (php_stream_stat_path(pathto, &ssb) < 0) { - ret = php_stream_mkdir(pathto, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL); + if (php_stream_stat_path(ZSTR_VAL(path_to), &ssb) < 0) { + ret = php_stream_mkdir(ZSTR_VAL(path_to), 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL); if (!ret) { zend_throw_exception_ex(spl_ce_RuntimeException, 0, - "Unable to create path \"%s\" for extraction", pathto); + "Unable to create path \"%s\" for extraction", ZSTR_VAL(path_to)); RETURN_THROWS(); } } else if (!(ssb.sb.st_mode & S_IFDIR)) { zend_throw_exception_ex(spl_ce_RuntimeException, 0, - "Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto); + "Unable to use path \"%s\" for extraction, it is a file, must be a directory", ZSTR_VAL(path_to)); RETURN_THROWS(); } @@ -4411,7 +4411,7 @@ PHP_METHOD(Phar, extractTo) "Invalid argument, array of filenames to extract contains non-string value"); RETURN_THROWS(); } - switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), pathto, pathto_len, overwrite, &error)) { + switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), path_to, overwrite, &error)) { case -1: zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error); @@ -4427,7 +4427,7 @@ PHP_METHOD(Phar, extractTo) RETURN_TRUE; } - ret = extract_helper(phar_obj->archive, filename, pathto, pathto_len, overwrite, &error); + ret = extract_helper(phar_obj->archive, filename, path_to, overwrite, &error); if (-1 == ret) { zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error); diff --git a/ext/phar/stream.h b/ext/phar/stream.h index ce75b70dcba00..83b395b4cfca3 100644 --- a/ext/phar/stream.h +++ b/ext/phar/stream.h @@ -21,7 +21,7 @@ BEGIN_EXTERN_C() #include "ext/standard/url.h" php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options); -void phar_entry_remove(phar_entry_data *idata, char **error); +ZEND_ATTRIBUTE_NONNULL void phar_entry_remove(phar_entry_data *idata, char **error); static php_stream* phar_wrapper_open_url(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC); static int phar_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context); diff --git a/ext/phar/tar.c b/ext/phar/tar.c index 7316e67ebb35f..3eaab22d0dbd5 100644 --- a/ext/phar/tar.c +++ b/ext/phar/tar.c @@ -127,7 +127,7 @@ bool phar_is_tar(char *buf, char *fname) /* {{{ */ } /* }}} */ -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) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) 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) /* {{{ */ { phar_archive_data *phar; zend_result ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error); @@ -136,9 +136,7 @@ zend_result phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, return FAILURE; } - if (pphar) { - *pphar = phar; - } + *pphar = phar; phar->is_data = is_data; @@ -153,9 +151,7 @@ zend_result phar_open_or_create_tar(char *fname, size_t fname_len, char *alias, } /* we've reached here - the phar exists and is a regular phar */ - if (error) { - spprintf(error, 4096, "phar tar error: \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a tar-based phar", fname); - } + spprintf(error, 4096, "phar tar error: \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a tar-based phar", fname); return FAILURE; } /* }}} */ @@ -877,7 +873,7 @@ static int phar_tar_writeheaders(zval *zv, void *argument) /* {{{ */ } /* }}} */ -static int phar_tar_setmetadata(const phar_metadata_tracker *tracker, phar_entry_info *entry, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL static int phar_tar_setmetadata(const phar_metadata_tracker *tracker, phar_entry_info *entry, char **error) /* {{{ */ { /* Copy the metadata from tracker to the new entry being written out to temporary files */ const zend_string *serialized_str; @@ -897,7 +893,7 @@ static int phar_tar_setmetadata(const phar_metadata_tracker *tracker, phar_entry entry->fp = php_stream_fopen_tmpfile(); entry->offset = entry->offset_abs = 0; if (entry->fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return -1; } if (serialized_str && ZSTR_LEN(serialized_str) != php_stream_write(entry->fp, ZSTR_VAL(serialized_str), ZSTR_LEN(serialized_str))) { @@ -910,7 +906,7 @@ static int phar_tar_setmetadata(const phar_metadata_tracker *tracker, phar_entry } /* }}} */ -static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */ { struct _phar_pass_tar_info *i = (struct _phar_pass_tar_info *)argument; char **error = i->error; @@ -964,7 +960,7 @@ static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */ } /* }}} */ -void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { static const char newstub[] = "is_persistent) { - if (error) { - spprintf(error, 0, "internal error: attempt to flush cached tar-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "internal error: attempt to flush cached tar-based phar \"%s\"", phar->fname); return; } @@ -1002,13 +996,11 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def if (!phar->is_temporary_alias && phar->alias_len) { entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); 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 tar-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to set alias in tar-based phar \"%s\"", phar->fname); php_stream_close(entry.fp); return; } @@ -1028,9 +1020,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), sizeof(halt_stub) - 1); if (pos == NULL) { - if (error) { - spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname); return; } @@ -1040,7 +1030,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return; } entry.uncompressed_filesize = len + end_sequence_len; @@ -1049,9 +1039,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def 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); - } + spprintf(error, 0, "unable to create stub from string in new tar-based phar \"%s\"", phar->fname); php_stream_close(entry.fp); return; } @@ -1062,14 +1050,13 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def /* 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"); + *error = estrdup("phar error: unable to create temporary file"); 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); - } + spprintf(error, 0, "unable to %s stub in%star-based phar \"%s\", failed", + user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); return; } @@ -1081,9 +1068,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def if (NULL == zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info))) { php_stream_close(entry.fp); zend_string_efree(entry.filename); - if (error) { - spprintf(error, 0, "unable to create stub in tar-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to create stub in tar-based phar \"%s\"", phar->fname); return; } } else { @@ -1106,9 +1091,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def newfile = php_stream_fopen_tmpfile(); if (!newfile) { - if (error) { - spprintf(error, 0, "unable to create temporary file"); - } + *error = estrdup("unable to create temporary file"); if (must_close_old_file) { php_stream_close(oldfile); } @@ -1159,7 +1142,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass); - if (error && *error) { + if (*error) { if (must_close_old_file) { php_stream_close(oldfile); } @@ -1173,12 +1156,10 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def /* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */ if (!phar->is_data || phar->sig_flags) { - if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, error)) { - if (error) { - char *save = *error; - spprintf(error, 0, "phar error: unable to write signature to tar-based phar: %s", save); - efree(save); - } + char *signature_error = NULL; + if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, &signature_error)) { + spprintf(error, 0, "phar error: unable to write signature to tar-based phar: %s", signature_error); + efree(signature_error); if (must_close_old_file) { php_stream_close(oldfile); @@ -1190,7 +1171,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return; } #ifdef WORDS_BIGENDIAN @@ -1209,9 +1190,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def if (8 != php_stream_write(entry.fp, sigbuf, 8) || signature_length != php_stream_write(entry.fp, signature, signature_length)) { efree(signature); - if (error) { - 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 (must_close_old_file) { php_stream_close(oldfile); @@ -1228,7 +1207,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def phar_tar_writeheaders_int(&entry, &pass); ZSTR_ALLOCA_FREE(entry.filename, use_heap); - if (error && *error) { + if (*error) { if (must_close_old_file) { php_stream_close(oldfile); } @@ -1248,7 +1227,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def } /* on error in the hash iterator above, error is set */ - if (error && *error) { + if (*error) { php_stream_close(newfile); return; } @@ -1274,9 +1253,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL); if (!phar->fp) { phar->fp = newfile; - if (error) { - spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname); - } + spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname); return; } @@ -1298,9 +1275,7 @@ void phar_tar_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def /* copy contents uncompressed rather than lose them */ php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL); php_stream_close(newfile); - 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); - } + 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; } diff --git a/ext/phar/tests/phar_setsignaturealgo2.phpt b/ext/phar/tests/phar_setsignaturealgo2.phpt index 4f31836fbbbcc..82615a13da478 100644 --- a/ext/phar/tests/phar_setsignaturealgo2.phpt +++ b/ext/phar/tests/phar_setsignaturealgo2.phpt @@ -2,6 +2,7 @@ Phar::setSupportedSignatures() with hash --EXTENSIONS-- phar +openssl --SKIPIF-- getSignature()); + +echo "Set MD5:\n"; $p->setSignatureAlgorithm(Phar::MD5); var_dump($p->getSignature()); + +echo "Set SHA1:\n"; $p->setSignatureAlgorithm(Phar::SHA1); var_dump($p->getSignature()); + +echo "Set SHA256:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA256); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA256); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set SHA512:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA512); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA512); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set OPENSSL:\n"; try { -$config = __DIR__ . '/files/openssl.cnf'; -$config_arg = array('config' => $config); -$private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem')); -$pkey = ''; -openssl_pkey_export($private, $pkey, NULL, $config_arg); -$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $config = __DIR__ . '/files/openssl.cnf'; + $config_arg = array('config' => $config); + $private = openssl_get_privatekey(file_get_contents(__DIR__ . '/files/private.pem')); + $pkey = ''; + openssl_pkey_export($private, $pkey, NULL, $config_arg); + $p->setSignatureAlgorithm(Phar::OPENSSL, $pkey); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + ?> --CLEAN-- --EXPECTF-- +Default: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set MD5: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(3) "MD5" } +Set SHA1: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(5) "SHA-1" } +Set SHA256: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set SHA512: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-512" } +Set OPENSSL: array(2) { ["hash"]=> string(%d) "%s" diff --git a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt index 1a3a84303ea1b..6d60d3a680a1d 100644 --- a/ext/phar/tests/tar/phar_setsignaturealgo2.phpt +++ b/ext/phar/tests/tar/phar_setsignaturealgo2.phpt @@ -2,6 +2,7 @@ Phar::setSupportedSignatures() with hash, tar-based --EXTENSIONS-- phar +openssl --SKIPIF-- getSignature()); + +echo "Set MD5:\n"; $p->setSignatureAlgorithm(Phar::MD5); var_dump($p->getSignature()); + +echo "Set SHA1:\n"; $p->setSignatureAlgorithm(Phar::SHA1); var_dump($p->getSignature()); + +echo "Set SHA256:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA256); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA256); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set SHA512:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA512); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA512); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set OPENSSL:\n"; try { -$config = __DIR__ . '/../files/openssl.cnf'; -$config_arg = array('config' => $config); -$private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem')); -$pkey = ''; -openssl_pkey_export($private, $pkey, NULL, $config_arg); -$p->setSignatureAlgorithm(Phar::OPENSSL, $pkey); -var_dump($p->getSignature()); -$p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey); -var_dump($p->getSignature()); -$p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $config = __DIR__ . '/../files/openssl.cnf'; + $config_arg = array('config' => $config); + $private = openssl_get_privatekey(file_get_contents(dirname(__DIR__) . '/files/private.pem')); + $pkey = ''; + openssl_pkey_export($private, $pkey, NULL, $config_arg); + $p->setSignatureAlgorithm(Phar::OPENSSL, $pkey); + var_dump($p->getSignature()); + echo "Set OPENSSL_SHA512:\n"; + $p->setSignatureAlgorithm(Phar::OPENSSL_SHA512, $pkey); + var_dump($p->getSignature()); + echo "Set OPENSSL_SHA256:\n"; + $p->setSignatureAlgorithm(Phar::OPENSSL_SHA256, $pkey); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + ?> --CLEAN-- --EXPECTF-- +Default: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set MD5: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(3) "MD5" } +Set SHA1: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(5) "SHA-1" } +Set SHA256: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set SHA512: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-512" } +Set OPENSSL: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "OpenSSL" } +Set OPENSSL_SHA512: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(14) "OpenSSL_SHA512" } +Set OPENSSL_SHA256: array(2) { ["hash"]=> string(%d) "%s" diff --git a/ext/phar/tests/zip/phar_setsignaturealgo2.phpt b/ext/phar/tests/zip/phar_setsignaturealgo2.phpt index 4fb9a155c5f57..8e3eed61cf781 100644 --- a/ext/phar/tests/zip/phar_setsignaturealgo2.phpt +++ b/ext/phar/tests/zip/phar_setsignaturealgo2.phpt @@ -20,49 +20,59 @@ $fname5 = __DIR__ . '/' . basename(__FILE__, '.php') . '.5.phar.zip'; $fname6 = __DIR__ . '/' . basename(__FILE__, '.php') . '.6.phar.zip'; $p = new Phar($fname); $p['file1.txt'] = 'hi'; + +echo "Default:\n"; var_dump($p->getSignature()); + +echo "Set MD5:\n"; $p->setSignatureAlgorithm(Phar::MD5); copy($fname, $fname2); $p = new Phar($fname2); var_dump($p->getSignature()); +echo "Set SHA1:\n"; $p->setSignatureAlgorithm(Phar::SHA1); copy($fname2, $fname3); $p = new Phar($fname3); var_dump($p->getSignature()); +echo "Set SHA256:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA256); -copy($fname3, $fname4); -$p = new Phar($fname4); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA256); + copy($fname3, $fname4); + $p = new Phar($fname4); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set SHA512:\n"; try { -$p->setSignatureAlgorithm(Phar::SHA512); -copy($fname4, $fname5); -$p = new Phar($fname5); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + $p->setSignatureAlgorithm(Phar::SHA512); + copy($fname4, $fname5); + $p = new Phar($fname5); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } + +echo "Set OPENSSL:\n"; try { -$config = __DIR__ . '/../files/openssl.cnf'; -$config_arg = array('config' => $config); -$keys=openssl_pkey_new($config_arg); -openssl_pkey_export($keys, $privkey, NULL, $config_arg); -$pubkey=openssl_pkey_get_details($keys); -$p->setSignatureAlgorithm(Phar::OPENSSL, $privkey); + $config = __DIR__ . '/../files/openssl.cnf'; + $config_arg = array('config' => $config); + $keys=openssl_pkey_new($config_arg); + openssl_pkey_export($keys, $privkey, NULL, $config_arg); + $pubkey=openssl_pkey_get_details($keys); + $p->setSignatureAlgorithm(Phar::OPENSSL, $privkey); -copy($fname5, $fname6); -file_put_contents($fname6 . '.pubkey', $pubkey['key']); -$p = new Phar($fname6); -var_dump($p->getSignature()); -} catch (Exception $e) { -echo $e->getMessage(); + copy($fname5, $fname6); + file_put_contents($fname6 . '.pubkey', $pubkey['key']); + $p = new Phar($fname6); + var_dump($p->getSignature()); +} catch (Throwable $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; } ?> --CLEAN-- @@ -76,36 +86,42 @@ unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip'); unlink(__DIR__ . '/' . basename(__FILE__, '.clean.php') . '.6.phar.zip.pubkey'); ?> --EXPECTF-- +Default: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set MD5: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(3) "MD5" } +Set SHA1: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(5) "SHA-1" } +Set SHA256: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-256" } +Set SHA512: array(2) { ["hash"]=> string(%d) "%s" ["hash_type"]=> string(7) "SHA-512" } +Set OPENSSL: array(2) { ["hash"]=> string(%d) "%s" diff --git a/ext/phar/util.c b/ext/phar/util.c index 06394813574b7..007cc12703ad7 100644 --- a/ext/phar/util.c +++ b/ext/phar/util.c @@ -399,7 +399,7 @@ static zend_result phar_create_writeable_entry(phar_archive_data *phar, phar_ent if (!entry->fp) { if (error) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); } return FAILURE; } @@ -418,7 +418,7 @@ static zend_result phar_create_writeable_entry(phar_archive_data *phar, phar_ent } /* }}} */ -static zend_result phar_separate_entry_fp(phar_entry_info *entry, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL static zend_result phar_separate_entry_fp(phar_entry_info *entry, char **error) /* {{{ */ { php_stream *fp; phar_entry_info *link; @@ -433,7 +433,7 @@ static zend_result phar_separate_entry_fp(phar_entry_info *entry, char **error) fp = php_stream_fopen_tmpfile(); if (fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return FAILURE; } phar_seek_efp(entry, 0, SEEK_SET, 0, 1); @@ -444,9 +444,8 @@ static zend_result phar_separate_entry_fp(phar_entry_info *entry, char **error) } if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), fp, link->uncompressed_filesize, NULL)) { - if (error) { - spprintf(error, 4096, "phar error: cannot separate entry file \"%s\" contents in phar archive \"%s\" for write access", ZSTR_VAL(entry->filename), entry->phar->fname); - } + spprintf(error, 4096, "phar error: cannot separate entry file \"%s\" contents in phar archive \"%s\" for write access", + ZSTR_VAL(entry->filename), entry->phar->fname); return FAILURE; } @@ -472,7 +471,7 @@ static zend_result phar_separate_entry_fp(phar_entry_info *entry, char **error) * appended, truncated, or read. For read, if the entry is marked unmodified, it is * assumed that the file pointer, if present, is opened for reading */ -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) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL 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) /* {{{ */ { phar_archive_data *phar; phar_entry_info *entry; @@ -481,31 +480,20 @@ zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname bool for_create = mode[0] != 'r'; bool for_trunc = mode[0] == 'w'; - if (!ret) { - return FAILURE; - } - *ret = NULL; - - if (error) { - *error = NULL; - } + *error = NULL; if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, error)) { return FAILURE; } if (for_write && PHAR_G(readonly) && !phar->is_data) { - if (error) { - spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, disabled by ini setting", path, fname); - } + spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, disabled by ini setting", path, fname); return FAILURE; } if (!path_len) { - if (error) { - spprintf(error, 4096, "phar error: file \"\" in phar \"%s\" must not be empty", fname); - } + spprintf(error, 4096, "phar error: file \"\" in phar \"%s\" must not be empty", fname); return FAILURE; } really_get_entry: @@ -527,9 +515,7 @@ zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname if (for_write && phar->is_persistent) { if (FAILURE == phar_copy_on_write(&phar)) { - if (error) { - spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, could not make cached phar writeable", path, fname); - } + spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, could not make cached phar writeable", path, fname); return FAILURE; } else { goto really_get_entry; @@ -537,16 +523,12 @@ zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname } if (entry->is_modified && !for_write) { - if (error) { - spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for reading, writable file pointers are open", path, fname); - } + spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for reading, writable file pointers are open", path, fname); return FAILURE; } if (entry->fp_refcount && for_write) { - if (error) { - spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, readable file pointers are open", path, fname); - } + spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be opened for writing, readable file pointers are open", path, fname); return FAILURE; } @@ -632,7 +614,7 @@ zend_result phar_get_entry_data(phar_entry_data **ret, char *fname, size_t fname /** * Create a new dummy file slot within a writeable phar for a newly created file */ -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_ATTRIBUTE_NONNULL 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) /* {{{ */ { phar_archive_data *phar; phar_entry_info *entry, etemp; @@ -657,16 +639,12 @@ phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, ch } if (phar_path_check(&path, &path_len, &pcr_error) > pcr_is_ok) { - if (error) { - spprintf(error, 0, "phar error: invalid path \"%s\" contains %s", path, pcr_error); - } + spprintf(error, 0, "phar error: invalid path \"%s\" contains %s", path, pcr_error); return NULL; } if (phar->is_persistent && FAILURE == phar_copy_on_write(&phar)) { - if (error) { - spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be created, could not make cached phar writeable", path, fname); - } + spprintf(error, 4096, "phar error: file \"%s\" in phar \"%s\" cannot be created, could not make cached phar writeable", path, fname); return NULL; } @@ -679,9 +657,7 @@ phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, ch etemp.fp = php_stream_fopen_tmpfile(); if (!etemp.fp) { - if (error) { - spprintf(error, 0, "phar error: unable to create temporary file"); - } + *error = estrdup("phar error: unable to create temporary file"); efree(ret); return NULL; } @@ -713,9 +689,8 @@ phar_entry_data *phar_get_or_create_entry_data(char *fname, size_t fname_len, ch if (NULL == (entry = zend_hash_add_mem(&phar->manifest, etemp.filename, &etemp, sizeof(phar_entry_info)))) { php_stream_close(etemp.fp); - if (error) { - spprintf(error, 0, "phar error: unable to add new entry \"%s\" to phar \"%s\"", ZSTR_VAL(etemp.filename), phar->fname); - } + spprintf(error, 0, "phar error: unable to add new entry \"%s\" to phar \"%s\"", + ZSTR_VAL(etemp.filename), phar->fname); efree(ret); zend_string_efree(etemp.filename); return NULL; @@ -763,7 +738,7 @@ zend_result phar_open_archive_fp(phar_archive_data *phar) /* {{{ */ /* }}} */ /* copy file data from an existing to a new phar_entry_info that is not in the manifest */ -zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, char **error) /* {{{ */ { phar_entry_info *link; @@ -782,7 +757,7 @@ zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, c dest->is_modified = 1; dest->fp = php_stream_fopen_tmpfile(); if (dest->fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return EOF; } phar_seek_efp(source, 0, SEEK_SET, 0, 1); @@ -795,9 +770,8 @@ zend_result phar_copy_entry_fp(phar_entry_info *source, phar_entry_info *dest, c if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), dest->fp, link->uncompressed_filesize, NULL)) { php_stream_close(dest->fp); dest->fp_type = PHAR_FP; - if (error) { - spprintf(error, 4096, "phar error: unable to copy contents of file \"%s\" to \"%s\" in phar archive \"%s\"", ZSTR_VAL(source->filename), ZSTR_VAL(dest->filename), source->phar->fname); - } + spprintf(error, 4096, "phar error: unable to copy contents of file \"%s\" to \"%s\" in phar archive \"%s\"", + ZSTR_VAL(source->filename), ZSTR_VAL(dest->filename), source->phar->fname); return FAILURE; } @@ -831,7 +805,7 @@ static void phar_set_fp_type(phar_entry_info *entry, enum phar_fp_type type, zen /* open and decompress a compressed phar entry */ -zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_links) /* {{{ */ { php_stream_filter *filter; phar_archive_data *phar = entry->phar; @@ -950,11 +924,9 @@ zend_result phar_open_entry_fp(phar_entry_info *entry, char **error, int follow_ /** * helper function to open an internal file's fp just-in-time */ -phar_entry_info * phar_open_jit(phar_archive_data *phar, phar_entry_info *entry, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL phar_entry_info * phar_open_jit(const phar_archive_data *phar, phar_entry_info *entry, char **error) /* {{{ */ { - if (error) { - *error = NULL; - } + *error = NULL; /* seek to start of internal file and read it */ if (FAILURE == phar_open_entry_fp(entry, error, 1)) { return NULL; @@ -1543,7 +1515,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s #ifndef PHAR_HAVE_OPENSSL if (!zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) { if (error) { - spprintf(error, 0, "openssl not loaded"); + *error = estrdup("openssl not loaded"); } return FAILURE; } @@ -1558,7 +1530,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s php_stream_close(pfp); } if (error) { - spprintf(error, 0, "openssl public key could not be read"); + *error = estrdup("openssl public key could not be read"); } return FAILURE; } @@ -1571,7 +1543,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s zend_string_release_ex(pubkey, 0); if (error) { - spprintf(error, 0, "openssl signature could not be verified"); + *error = estrdup("openssl signature could not be verified"); } return FAILURE; @@ -1586,7 +1558,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (NULL == in) { zend_string_release_ex(pubkey, 0); if (error) { - spprintf(error, 0, "openssl signature could not be processed"); + *error = estrdup("openssl signature could not be processed"); } return FAILURE; } @@ -1597,7 +1569,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (NULL == key) { if (error) { - spprintf(error, 0, "openssl signature could not be processed"); + *error = estrdup("openssl signature could not be processed"); } return FAILURE; } @@ -1608,7 +1580,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s EVP_MD_CTX_destroy(md_ctx); } if (error) { - spprintf(error, 0, "openssl signature could not be verified"); + *error = estrdup("openssl signature could not be verified"); } return FAILURE; } @@ -1640,7 +1612,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s EVP_MD_CTX_destroy(md_ctx); if (error) { - spprintf(error, 0, "broken openssl signature"); + *error = estrdup("broken openssl signature"); } return FAILURE; @@ -1659,7 +1631,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (sig_len < sizeof(digest)) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken openssl signature"); } return FAILURE; } @@ -1685,7 +1657,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (memcmp(digest, sig, sizeof(digest))) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken openssl signature"); } return FAILURE; } @@ -1699,7 +1671,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (sig_len < sizeof(digest)) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken openssl signature"); } return FAILURE; } @@ -1725,7 +1697,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (memcmp(digest, sig, sizeof(digest))) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken signature"); } return FAILURE; } @@ -1739,7 +1711,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (sig_len < sizeof(digest)) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken signature"); } return FAILURE; } @@ -1765,7 +1737,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (memcmp(digest, sig, sizeof(digest))) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken signature"); } return FAILURE; } @@ -1779,7 +1751,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (sig_len < sizeof(digest)) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken signature"); } return FAILURE; } @@ -1805,7 +1777,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s if (memcmp(digest, sig, sizeof(digest))) { if (error) { - spprintf(error, 0, "broken signature"); + *error = estrdup("broken signature"); } return FAILURE; } @@ -1815,7 +1787,7 @@ zend_result phar_verify_signature(php_stream *fp, size_t end_of_phar, uint32_t s } default: if (error) { - spprintf(error, 0, "broken or unsupported signature"); + *error = estrdup("broken or unsupported signature"); } return FAILURE; } @@ -1902,7 +1874,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char if (!key) { if (error) { - spprintf(error, 0, "unable to process private key"); + *error = estrdup("unable to process private key"); } return FAILURE; } @@ -1921,6 +1893,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char if (!EVP_SignInit(md_ctx, mdtype)) { EVP_PKEY_free(key); + EVP_MD_CTX_free(md_ctx); efree(sigbuf); if (error) { spprintf(error, 0, "unable to initialize openssl signature for phar \"%s\"", phar->fname); @@ -1931,6 +1904,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char while ((sig_len = php_stream_read(fp, (char*)buf, sizeof(buf))) > 0) { if (!EVP_SignUpdate(md_ctx, buf, sig_len)) { EVP_PKEY_free(key); + EVP_MD_CTX_free(md_ctx); efree(sigbuf); if (error) { spprintf(error, 0, "unable to update the openssl signature for phar \"%s\"", phar->fname); @@ -1941,6 +1915,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char if (!EVP_SignFinal (md_ctx, sigbuf, &siglen, key)) { EVP_PKEY_free(key); + EVP_MD_CTX_free(md_ctx); efree(sigbuf); if (error) { spprintf(error, 0, "unable to write phar \"%s\" with requested openssl signature", phar->fname); @@ -1950,7 +1925,7 @@ zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char sigbuf[siglen] = '\0'; EVP_PKEY_free(key); - EVP_MD_CTX_destroy(md_ctx); + EVP_MD_CTX_free(md_ctx); #else size_t siglen; sigbuf = NULL; diff --git a/ext/phar/zip.c b/ext/phar/zip.c index 0ee958bda24fb..2b4b831f0cbb8 100644 --- a/ext/phar/zip.c +++ b/ext/phar/zip.c @@ -827,18 +827,16 @@ int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alia /** * Create or open a zip-based phar for writing */ -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) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) zend_result 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) /* {{{ */ { phar_archive_data *phar; - int ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error); + zend_result ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error); if (FAILURE == ret) { return FAILURE; } - if (pphar) { - *pphar = phar; - } + *pphar = phar; phar->is_data = is_data; @@ -853,9 +851,7 @@ int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t a } /* we've reached here - the phar exists and is a regular phar */ - if (error) { - spprintf(error, 4096, "phar zip error: phar \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a zip-based phar", fname); - } + spprintf(error, 4096, "phar zip error: phar \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a zip-based phar", fname); return FAILURE; } @@ -1207,11 +1203,11 @@ static int phar_zip_applysignature(phar_archive_data *phar, struct _phar_zip_pas php_stream_write(newfile, ZSTR_VAL(phar->metadata_tracker.str), ZSTR_LEN(phar->metadata_tracker.str)); } - if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, pass->error)) { - if (pass->error) { - char *save = *(pass->error); - spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar: %s", save); - efree(save); + char *signature_error = NULL; + if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, &signature_error)) { + if (signature_error) { + spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar: %s", signature_error); + efree(signature_error); } php_stream_close(newfile); @@ -1258,7 +1254,7 @@ static int phar_zip_applysignature(phar_archive_data *phar, struct _phar_zip_pas } /* }}} */ -void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ +ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_default_stub, char **error) /* {{{ */ { static const char newstub[] = "is_persistent) { - if (error) { - spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname); return; } @@ -1294,13 +1288,11 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def if (!phar->is_temporary_alias && phar->alias_len) { entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); 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); - } + spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname); return; } @@ -1324,9 +1316,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def char *pos = php_stristr(ZSTR_VAL(user_stub), halt_stub, ZSTR_LEN(user_stub), strlen(halt_stub)); if (pos == NULL) { - if (error) { - spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname); return; } @@ -1336,7 +1326,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def entry.fp = php_stream_fopen_tmpfile(); if (entry.fp == NULL) { - spprintf(error, 0, "phar error: unable to create temporary file"); + *error = estrdup("phar error: unable to create temporary file"); return; } entry.uncompressed_filesize = len + end_sequence_len; @@ -1345,9 +1335,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def 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); - } + spprintf(error, 0, "unable to create stub from string in new zip-based phar \"%s\"", phar->fname); php_stream_close(entry.fp); return; } @@ -1359,14 +1347,12 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def /* 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"); + *error = estrdup("phar error: unable to create temporary file"); 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); - } + spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname); return; } @@ -1378,9 +1364,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def if (NULL == zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info))) { php_stream_close(entry.fp); zend_string_efree(entry.filename); - if (error) { - spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname); - } + spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname); return; } } else { @@ -1410,9 +1394,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def 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); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to open temporary file", phar->fname); return; } @@ -1440,9 +1422,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def phar_metadata_tracker_try_ensure_has_serialized_data(&phar->metadata_tracker, phar->is_persistent); if (temperr) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: %s", phar->fname, temperr); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: %s", phar->fname, temperr); efree(temperr); temperror: php_stream_close(pass.centralfp); @@ -1469,9 +1449,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def size_t clen; int ret = php_stream_copy_to_stream_ex(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL, &clen); if (SUCCESS != ret || clen != cdir_size) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname); goto temperror; } } @@ -1484,23 +1462,17 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def PHAR_SET_16(eocd.comment_len, ZSTR_LEN(phar->metadata_tracker.str)); if (sizeof(eocd) != php_stream_write(pass.filefp, (char *)&eocd, sizeof(eocd))) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname); goto nocentralerror; } if (ZSTR_LEN(phar->metadata_tracker.str) != php_stream_write(pass.filefp, ZSTR_VAL(phar->metadata_tracker.str), ZSTR_LEN(phar->metadata_tracker.str))) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write metadata to zip comment", phar->fname); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write metadata to zip comment", phar->fname); goto nocentralerror; } } else { if (sizeof(eocd) != php_stream_write(pass.filefp, (char *)&eocd, sizeof(eocd))) { - if (error) { - spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname); - } + spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname); goto nocentralerror; } } @@ -1529,9 +1501,7 @@ void phar_zip_flush(phar_archive_data *phar, zend_string *user_stub, bool is_def php_stream_close(oldfile); } phar->fp = pass.filefp; - if (error) { - spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); - } + spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname); return; } php_stream_rewind(pass.filefp);