Skip to content

Commit

Permalink
Use zend_string for phar alias
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Jan 18, 2025
1 parent f97d8f2 commit 0379bbd
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 210 deletions.
10 changes: 5 additions & 5 deletions ext/phar/dirstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,

phar_request_initialize();

if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, &error)) {
if (error) {
php_stream_wrapper_log_error(wrapper, options, "%s", error);
efree(error);
Expand Down Expand Up @@ -359,7 +359,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
return 0;
}

if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
phar = NULL;
}

Expand Down Expand Up @@ -388,7 +388,7 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
return 0;
}

if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, &error)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path) + 1, ZSTR_VAL(resource->host), error);
efree(error);
php_url_free(resource);
Expand Down Expand Up @@ -487,7 +487,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
return 0;
}

if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
phar = NULL;
}

Expand Down Expand Up @@ -516,7 +516,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
return 0;
}

if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, 0, &error)) {
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(resource->host), ZSTR_LEN(resource->host), NULL, &error)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", error retrieving phar information: %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
efree(error);
php_url_free(resource);
Expand Down
8 changes: 4 additions & 4 deletions ext/phar/func_interceptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
/* retrieving a file defaults to within the current directory, so use this if possible */
phar_archive_data *phar;
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
efree(arch);
return NULL;
}
Expand Down Expand Up @@ -515,7 +515,7 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
entry = estrndup(filename, filename_length);
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
entry_len = filename_length;
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
efree(arch);
efree(entry);
goto skip_phar;
Expand Down Expand Up @@ -759,7 +759,7 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
entry_len = filename_len;
/* retrieving a file within the current directory, so use this if possible */
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
phar_entry_info *etemp;

entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
Expand Down Expand Up @@ -825,7 +825,7 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
entry_len = filename_len;
/* retrieving a file within the current directory, so use this if possible */
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, NULL)) {
phar_entry_info *etemp;

entry = phar_fix_filepath(estrndup(entry, entry_len), &entry_len, 1);
Expand Down
71 changes: 36 additions & 35 deletions ext/phar/phar.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void phar_split_cache_list(void) /* {{{ */
len = strlen(key);
}

if (SUCCESS == phar_open_from_filename(key, len, NULL, 0, 0, &phar, NULL)) {
if (SUCCESS == phar_open_from_filename(key, len, NULL, 0, &phar, NULL)) {
phar->phar_pos = i++;
php_stream_close(phar->fp);
phar->fp = NULL;
Expand Down Expand Up @@ -194,8 +194,8 @@ PHP_INI_END()
*/
void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
{
if (phar->alias && phar->alias != phar->fname) {
pefree(phar->alias, phar->is_persistent);
if (phar->alias) {
zend_string_release_ex(phar->alias, phar->is_persistent);
phar->alias = NULL;
}

Expand Down Expand Up @@ -258,7 +258,8 @@ bool phar_archive_delref(phar_archive_data *phar) /* {{{ */
} else if (!phar->refcount) {
/* invalidate phar cache */
PHAR_G(last_phar) = NULL;
PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
PHAR_G(last_phar_name) = NULL;
PHAR_G(last_alias) = NULL;

if (phar->fp && (!(phar->flags & PHAR_FILE_COMPRESSION_MASK) || !phar->alias)) {
/* close open file handle - allows removal or rename of
Expand Down Expand Up @@ -486,7 +487,7 @@ void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
/**
* Open an already loaded phar
*/
static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *alias, size_t alias_len, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, zend_string *alias, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
{
phar_archive_data *phar;
#ifdef PHP_WIN32
Expand All @@ -506,7 +507,7 @@ static zend_result phar_open_parsed_phar(char *fname, size_t fname_len, char *al
phar_unixify_path_separators(fname, fname_len);
}
#endif
if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error)
if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, error)
&& ((alias && fname_len == phar->fname_len
&& !strncmp(fname, phar->fname, fname_len)) || !alias)
) {
Expand Down Expand Up @@ -1256,9 +1257,11 @@ static zend_result phar_parse_pharfile(php_stream *fp, char *fname, size_t fname
}

mydata->alias = alias ?
pestrndup(alias, alias_len, mydata->is_persistent) :
pestrndup(mydata->fname, fname_len, mydata->is_persistent);
mydata->alias_len = alias ? alias_len : fname_len;
zend_string_init(alias, alias_len, mydata->is_persistent) :
zend_string_init(mydata->fname, fname_len, mydata->is_persistent);
if (mydata->is_persistent) {
GC_MAKE_PERSISTENT_LOCAL(mydata->alias);
}
mydata->sig_flags = sig_flags;
mydata->fp = fp;
mydata->sig_len = sig_len;
Expand All @@ -1270,7 +1273,7 @@ static zend_result phar_parse_pharfile(php_stream *fp, char *fname, size_t fname

mydata->is_temporary_alias = temp_alias;

if (!phar_validate_alias(mydata->alias, mydata->alias_len)) {
if (!phar_validate_alias(ZSTR_VAL(mydata->alias), ZSTR_LEN(mydata->alias))) {
signature = NULL;
fp = NULL;
MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
Expand Down Expand Up @@ -1315,7 +1318,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_result phar_open_or_create_filename(char *fname, size_t fname_len, zend_string *alias, bool is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
{
const char *ext_str, *z;
char *my_error;
Expand Down Expand Up @@ -1345,7 +1348,7 @@ zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *al
return FAILURE;
}
check_file:
if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error) == SUCCESS) {
if (phar_open_parsed_phar(fname, fname_len, alias, is_data, options, test, &my_error) == SUCCESS) {
if (pphar) {
*pphar = *test;
}
Expand Down Expand Up @@ -1380,15 +1383,15 @@ zend_result phar_open_or_create_filename(char *fname, size_t fname_len, char *al

if (ext_len > 3 && (z = memchr(ext_str, 'z', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ip", 2)) {
/* assume zip-based phar */
return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
return phar_open_or_create_zip(fname, fname_len, alias ? ZSTR_VAL(alias) : NULL, alias ? ZSTR_LEN(alias) : 0, is_data, options, pphar, error);
}

if (ext_len > 3 && (z = memchr(ext_str, 't', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ar", 2)) {
/* assume tar-based phar */
return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
return phar_open_or_create_tar(fname, fname_len, alias ? ZSTR_VAL(alias) : NULL, alias ? ZSTR_LEN(alias) : 0, is_data, options, pphar, error);
}

return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
return phar_create_or_parse_filename(fname, fname_len, alias ? ZSTR_VAL(alias) : NULL, alias ? ZSTR_LEN(alias) : 0, is_data, options, pphar, error);
}
/* }}} */

Expand Down Expand Up @@ -1515,8 +1518,7 @@ zend_result phar_create_or_parse_filename(char *fname, size_t fname_len, char *a
}

ZEND_ASSERT(!mydata->is_persistent);
mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
mydata->alias_len = alias ? alias_len : fname_len;
mydata->alias = alias ? zend_string_init(alias, alias_len, false) : zend_string_init(mydata->fname, fname_len, false);
}

if (alias_len && alias) {
Expand Down Expand Up @@ -1548,7 +1550,7 @@ zend_result phar_create_or_parse_filename(char *fname, size_t fname_len, char *a
* that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
* or FAILURE is returned and pphar is set to a pointer to the phar's manifest
*/
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_from_filename(char *fname, size_t fname_len, zend_string *alias, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
{
php_stream *fp;
zend_string *actual;
Expand All @@ -1562,7 +1564,7 @@ zend_result phar_open_from_filename(char *fname, size_t fname_len, char *alias,
is_data = true;
}

if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error) == SUCCESS) {
if (phar_open_parsed_phar(fname, fname_len, alias, is_data, options, pphar, error) == SUCCESS) {
return SUCCESS;
} else if (error && *error) {
return FAILURE;
Expand Down Expand Up @@ -1590,7 +1592,7 @@ zend_result phar_open_from_filename(char *fname, size_t fname_len, char *alias,
fname_len = ZSTR_LEN(actual);
}

zend_result ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, error);
zend_result ret = phar_open_from_fp(fp, fname, fname_len, alias ? ZSTR_VAL(alias) : NULL, alias ? ZSTR_LEN(alias) : 0, options, pphar, error);

if (actual) {
zend_string_release_ex(actual, 0);
Expand Down Expand Up @@ -2322,7 +2324,7 @@ 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_result phar_open_executed_filename(zend_string *alias, char **error) /* {{{ */
{
if (error) {
*error = NULL;
Expand All @@ -2337,7 +2339,7 @@ zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **er
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, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
return SUCCESS;
}

Expand Down Expand Up @@ -2370,7 +2372,7 @@ zend_result phar_open_executed_filename(char *alias, size_t alias_len, char **er
fname = actual;
}

zend_result ret = phar_open_from_fp(fp, ZSTR_VAL(fname), ZSTR_LEN(fname), alias, alias_len, REPORT_ERRORS, NULL, error);
zend_result ret = phar_open_from_fp(fp, ZSTR_VAL(fname), ZSTR_LEN(fname), alias ? ZSTR_VAL(alias) : NULL, alias ? ZSTR_LEN(alias) : 0, REPORT_ERRORS, NULL, error);

if (actual) {
zend_string_release_ex(actual, 0);
Expand Down Expand Up @@ -2544,7 +2546,7 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa

phar_entry_info *entry, *newentry;
size_t halt_offset;
int restore_alias_len, global_flags = 0;
int global_flags = 0;
bool must_close_old_file = false;
bool has_dirs = false;
char manifest[18], entry_buffer[24];
Expand Down Expand Up @@ -2860,12 +2862,14 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa
* 4: phar metadata length
* ?: phar metadata
*/
restore_alias_len = phar->alias_len;
size_t written_alias_len;
if (phar->is_temporary_alias) {
phar->alias_len = 0;
written_alias_len = 0;
} else {
written_alias_len = ZSTR_LEN(phar->alias); // TODO: null alias?
}

manifest_len = offset + phar->alias_len + sizeof(manifest) + (main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
manifest_len = offset + written_alias_len + sizeof(manifest) + (main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
phar_set_32(manifest, manifest_len);
/* Hack - see bug #65028, add padding byte to the end of the manifest */
if(manifest[0] == '\r' || manifest[0] == '\n') {
Expand All @@ -2882,18 +2886,17 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa
*(manifest + 9) = (unsigned char) (((PHAR_API_VERSION_NODIR) & 0xF0));
}
phar_set_32(manifest+10, global_flags);
phar_set_32(manifest+14, phar->alias_len);
phar_set_32(manifest+14, written_alias_len);

/* write the manifest header */
if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
|| (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) {
|| written_alias_len != php_stream_write(newfile, ZSTR_VAL(phar->alias), written_alias_len)) {

if (must_close_old_file) {
php_stream_close(oldfile);
}

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);
Expand All @@ -2902,8 +2905,6 @@ void phar_flush_ex(phar_archive_data *phar, zend_string *user_stub, bool is_defa
goto cleanup;
}

phar->alias_len = restore_alias_len;

phar_set_32(manifest, main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
if (4 != php_stream_write(newfile, manifest, 4) || ((main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0)
&& ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) {
Expand All @@ -2914,7 +2915,6 @@ 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);
Expand Down Expand Up @@ -3271,7 +3271,7 @@ static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type)
return phar_orig_compile_file(file_handle, type);
}
if (strstr(ZSTR_VAL(file_handle->filename), ".phar") && !strstr(ZSTR_VAL(file_handle->filename), "://")) {
if (SUCCESS == phar_open_from_filename(ZSTR_VAL(file_handle->filename), ZSTR_LEN(file_handle->filename), NULL, 0, 0, &phar, NULL)) {
if (SUCCESS == phar_open_from_filename(ZSTR_VAL(file_handle->filename), ZSTR_LEN(file_handle->filename), NULL, 0, &phar, NULL)) {
if (phar->is_zip || phar->is_tar) {
zend_file_handle f;

Expand Down Expand Up @@ -3456,7 +3456,8 @@ void phar_request_initialize(void) /* {{{ */
if (!PHAR_G(request_init))
{
PHAR_G(last_phar) = NULL;
PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
PHAR_G(last_phar_name) = NULL;
PHAR_G(last_alias) = NULL;
PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
PHAR_G(request_init) = 1;
Expand Down
14 changes: 6 additions & 8 deletions ext/phar/phar_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phar)
/* phar_get_archive cache */
char* last_phar_name;
uint32_t last_phar_name_len;
char* last_alias;
uint32_t last_alias_len;
zend_string*last_alias;
phar_archive_data* last_phar;
HashTable mime_types;
ZEND_END_MODULE_GLOBALS(phar)
Expand Down Expand Up @@ -248,8 +247,7 @@ struct _phar_archive_data {
/* for phar_detect_fname_ext, this stores the location of the file extension within fname */
char *ext;
uint32_t ext_len;
char *alias;
uint32_t alias_len;
zend_string *alias;
char version[12];
size_t halt_offset;
HashTable manifest;
Expand Down Expand Up @@ -405,12 +403,12 @@ 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_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_result phar_open_from_filename(char *fname, size_t fname_len, zend_string *alias, uint32_t options, phar_archive_data** pphar, char **error);
zend_result phar_open_or_create_filename(char *fname, size_t fname_len, zend_string *alias, 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_result phar_open_executed_filename(zend_string *alias, char **error);
zend_result phar_free_alias(phar_archive_data *phar);
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_get_archive(phar_archive_data **archive, char *fname, size_t fname_len, zend_string *alias, 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);
zend_result phar_create_signature(phar_archive_data *phar, php_stream *fp, char **signature, size_t *signature_length, char **error);

Expand Down
Loading

0 comments on commit 0379bbd

Please sign in to comment.