Skip to content

Commit 3a9d7b5

Browse files
committed
ext/phar: refactor logic after expand_filepath() call in phar_get_archive()
We don't need to be assigning the realpath to fname, we can just use it directly. This also makes the efree(my_realpath) call not seem completely random.
1 parent 530276a commit 3a9d7b5

1 file changed

Lines changed: 10 additions & 17 deletions

File tree

ext/phar/util.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,6 @@ zend_result phar_free_alias(const phar_archive_data *phar) /* {{{ */
963963
zend_result phar_get_archive(phar_archive_data **archive, const char *fname, size_t fname_len, const char *alias, size_t alias_len, char **error) /* {{{ */
964964
{
965965
phar_archive_data *fd, *fd_ptr;
966-
char *my_realpath;
967966

968967
phar_request_initialize();
969968

@@ -1036,7 +1035,6 @@ zend_result phar_get_archive(phar_archive_data **archive, const char *fname, siz
10361035
}
10371036
}
10381037

1039-
my_realpath = NULL;
10401038
const char *save = fname;
10411039
size_t save_len = fname_len;
10421040

@@ -1116,25 +1114,24 @@ zend_result phar_get_archive(phar_archive_data **archive, const char *fname, siz
11161114
}
11171115

11181116
/* not found, try converting \ to / */
1119-
my_realpath = expand_filepath(fname, my_realpath);
1117+
char *my_realpath = expand_filepath(fname, NULL);
11201118

1121-
if (my_realpath) {
1122-
size_t my_realpath_len = strlen(my_realpath);
1123-
#ifdef PHP_WIN32
1124-
phar_unixify_path_separators(my_realpath, my_realpath_len);
1125-
#endif
1126-
fname_len = my_realpath_len;
1127-
fname = my_realpath;
1128-
} else {
1119+
if (UNEXPECTED(!my_realpath)) {
11291120
return FAILURE;
11301121
}
11311122

1132-
fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len);
1123+
size_t my_realpath_len = strlen(my_realpath);
1124+
#ifdef PHP_WIN32
1125+
phar_unixify_path_separators(my_realpath, my_realpath_len);
1126+
#endif
1127+
1128+
fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), my_realpath, my_realpath_len);
11331129

11341130
/* If we didn't find the path in the fname map, check in the cached manifest to see if we can find it */
11351131
if (!fd_ptr && PHAR_G(manifest_cached)) {
1136-
fd_ptr = zend_hash_str_find_ptr(&cached_phars, fname, fname_len);
1132+
fd_ptr = zend_hash_str_find_ptr(&cached_phars, my_realpath, my_realpath_len);
11371133
}
1134+
efree(my_realpath);
11381135

11391136
if (fd_ptr) {
11401137
*archive = fd_ptr;
@@ -1144,17 +1141,13 @@ zend_result phar_get_archive(phar_archive_data **archive, const char *fname, siz
11441141
zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, fd);
11451142
}
11461143

1147-
efree(my_realpath);
1148-
11491144
PHAR_G(last_phar) = fd;
11501145
PHAR_G(last_phar_name) = fd->fname;
11511146
PHAR_G(last_alias) = fd->alias;
11521147
PHAR_G(last_alias_len) = fd->alias_len;
11531148

11541149
return SUCCESS;
11551150
}
1156-
1157-
efree(my_realpath);
11581151
}
11591152

11601153
return FAILURE;

0 commit comments

Comments
 (0)