Skip to content

Commit 4592d1c

Browse files
authored
ext/phar: refactor _phar_archive_data.fname field to be a zend_string* (#21823)
Convert relevant cache global to zend_string as well. This is in preparation for future refactorings to convert functions taking a char* + size_t pair to zend_string*
1 parent a935460 commit 4592d1c

10 files changed

Lines changed: 323 additions & 323 deletions

File tree

ext/phar/dirstream.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,15 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
447447

448448
void *had_been_added = zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info));
449449
if (!had_been_added) {
450-
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", adding to manifest failed", ZSTR_VAL(entry.filename), phar->fname);
450+
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", adding to manifest failed", ZSTR_VAL(entry.filename), ZSTR_VAL(phar->fname));
451451
zend_string_efree(entry.filename);
452452
return 0;
453453
}
454454

455455
phar_flush(phar, &error);
456456

457457
if (error) {
458-
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(entry.filename), phar->fname, error);
458+
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(entry.filename), ZSTR_VAL(phar->fname), error);
459459
zend_hash_del(&phar->manifest, entry.filename);
460460
efree(error);
461461
return 0;
@@ -576,7 +576,7 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
576576
phar_flush(phar, &error);
577577

578578
if (error) {
579-
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(entry->filename), phar->fname, error);
579+
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(entry->filename), ZSTR_VAL(phar->fname), error);
580580
php_url_free(resource);
581581
efree(error);
582582
return 0;

ext/phar/func_interceptors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
483483
goto skip_phar;
484484
}
485485

486-
if (PHAR_G(last_phar) && ZSTR_LEN(fname) - 7 >= PHAR_G(last_phar_name_len) && !memcmp(ZSTR_VAL(fname) + 7, PHAR_G(last_phar_name), PHAR_G(last_phar_name_len))) {
486+
if (PHAR_G(last_phar) && ZSTR_LEN(fname) - 7 >= ZSTR_LEN(PHAR_G(last_phar_name)) && !memcmp(ZSTR_VAL(fname) + 7, ZSTR_VAL(PHAR_G(last_phar_name)), ZSTR_LEN(PHAR_G(last_phar_name)))) {
487487
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
488488
phar = PHAR_G(last_phar);
489489
goto splitted;

ext/phar/phar.c

Lines changed: 63 additions & 60 deletions
Large diffs are not rendered by default.

ext/phar/phar_internal.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phar)
146146
char *openssl_privatekey;
147147
uint32_t openssl_privatekey_len;
148148
/* phar_get_archive cache */
149-
const char *last_phar_name;
150-
uint32_t last_phar_name_len;
149+
const zend_string *last_phar_name;
151150
uint32_t last_alias_len;
152151
const char* last_alias;
153152
phar_archive_data* last_phar;
@@ -243,8 +242,7 @@ typedef struct _phar_entry_info {
243242

244243
/* information about a phar file (the archive itself) */
245244
struct _phar_archive_data {
246-
char *fname;
247-
uint32_t fname_len;
245+
zend_string *fname;
248246
/* The ext field stores the location of the file extension from the fname field, and thus should never be freed. */
249247
uint32_t ext_len;
250248
const char *ext;
@@ -380,21 +378,20 @@ static inline bool phar_validate_alias(const char *alias, size_t alias_len) /* {
380378

381379
static inline void phar_set_inode(phar_entry_info *entry) /* {{{ */
382380
{
383-
char tmp[MAXPATHLEN];
384-
size_t tmp_len;
385-
size_t len1, len2;
386-
387-
tmp_len = MIN(MAXPATHLEN, ZSTR_LEN(entry->filename) + entry->phar->fname_len);
388-
389-
len1 = MIN(entry->phar->fname_len, tmp_len);
390381
if (entry->phar->fname) {
391-
memcpy(tmp, entry->phar->fname, len1);
392-
}
382+
char tmp[MAXPATHLEN];
383+
size_t tmp_len = MIN(MAXPATHLEN, ZSTR_LEN(entry->filename) + ZSTR_LEN(entry->phar->fname));
393384

394-
len2 = MIN(tmp_len - len1, ZSTR_LEN(entry->filename));
395-
memcpy(tmp + len1, entry->filename, len2);
385+
size_t len1 = MIN(ZSTR_LEN(entry->phar->fname), tmp_len);
386+
memcpy(tmp, ZSTR_VAL(entry->phar->fname), len1);
396387

397-
entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
388+
size_t len2 = MIN(tmp_len - len1, ZSTR_LEN(entry->filename));
389+
memcpy(tmp + len1, entry->filename, len2);
390+
391+
entry->inode = (unsigned short) zend_hash_func(tmp, tmp_len);
392+
} else {
393+
entry->inode = (unsigned short) zend_string_hash_func(entry->filename);
394+
}
398395
}
399396
/* }}} */
400397

0 commit comments

Comments
 (0)