Skip to content

Commit 4195fc4

Browse files
committed
ext/phar: no longer assign to variables in if conditions
To increase legibility
1 parent 9d53371 commit 4195fc4

8 files changed

Lines changed: 158 additions & 120 deletions

File tree

ext/phar/dirstream.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const Has
247247
*/
248248
php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC) /* {{{ */
249249
{
250-
php_url *resource = NULL;
251250
char *error;
252251
phar_archive_data *phar;
253252

254-
if ((resource = phar_parse_url(wrapper, path, mode, options)) == NULL) {
253+
php_url *resource = phar_parse_url(wrapper, path, mode, options);
254+
if (!resource) {
255255
php_stream_wrapper_log_error(wrapper, options, "phar url \"%s\" is unknown", path);
256256
return NULL;
257257
}
@@ -343,7 +343,7 @@ php_stream *phar_wrapper_open_dir(php_stream_wrapper *wrapper, const char *path,
343343
*/
344344
int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mode, int options, php_stream_context *context) /* {{{ */
345345
{
346-
phar_entry_info entry, *e;
346+
phar_entry_info entry;
347347
phar_archive_data *phar = NULL;
348348
char *error;
349349
php_url *resource = NULL;
@@ -390,7 +390,8 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
390390
return 0;
391391
}
392392

393-
if ((e = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1, 2, &error, true))) {
393+
phar_entry_info *e = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, ZSTR_LEN(resource->path) - 1, 2, &error, true);
394+
if (e) {
394395
/* directory exists, or is a subdirectory of an existing file */
395396
if (e->is_temp_dir) {
396397
zend_string_efree(e->filename);
@@ -444,7 +445,8 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
444445
entry.flags = PHAR_ENT_PERM_DEF_DIR;
445446
entry.old_flags = PHAR_ENT_PERM_DEF_DIR;
446447

447-
if (NULL == zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info))) {
448+
void *had_been_added = zend_hash_add_mem(&phar->manifest, entry.filename, &entry, sizeof(phar_entry_info));
449+
if (!had_been_added) {
448450
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);
449451
zend_string_efree(entry.filename);
450452
return 0;
@@ -469,10 +471,8 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
469471
*/
470472
int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context) /* {{{ */
471473
{
472-
phar_entry_info *entry;
473474
phar_archive_data *phar = NULL;
474475
char *error;
475-
php_url *resource = NULL;
476476

477477
/* pre-readonly check, we need to know if this is a data phar */
478478
zend_string *arch = phar_split_fname(url, strlen(url), NULL, 2, 2);
@@ -492,7 +492,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
492492
return 0;
493493
}
494494

495-
if ((resource = phar_parse_url(wrapper, url, "w", options)) == NULL) {
495+
php_url *resource = phar_parse_url(wrapper, url, "w", options);
496+
if (!resource) {
496497
return 0;
497498
}
498499

@@ -518,7 +519,8 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
518519

519520
size_t path_len = ZSTR_LEN(resource->path) - 1;
520521

521-
if (!(entry = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, path_len, 2, &error, true))) {
522+
phar_entry_info *entry = phar_get_entry_info_dir(phar, ZSTR_VAL(resource->path) + 1, path_len, 2, &error, true);
523+
if (!entry) {
522524
if (error) {
523525
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\" in phar \"%s\", %s", ZSTR_VAL(resource->path)+1, ZSTR_VAL(resource->host), error);
524526
efree(error);

ext/phar/func_interceptors.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
106106

107107
zend_string *name = NULL;
108108
if (using_include_path) {
109-
if (!(name = phar_find_in_include_path(filename, NULL))) {
109+
name = phar_find_in_include_path(filename, NULL);
110+
if (!name) {
110111
/* this file is not in the phar, use the original path */
111112
zend_string_release_ex(arch, false);
112113
return NULL;
@@ -841,7 +842,8 @@ void phar_release_functions(void)
841842
/* {{{ void phar_intercept_functions_init(void) */
842843
#define PHAR_INTERCEPT(func) \
843844
PHAR_G(orig_##func) = NULL; \
844-
if (NULL != (orig = zend_hash_str_find_ptr(CG(function_table), #func, sizeof(#func)-1))) { \
845+
orig = zend_hash_str_find_ptr(CG(function_table), #func, sizeof(#func)-1); \
846+
if (orig) { \
845847
PHAR_G(orig_##func) = orig->internal_function.handler; \
846848
orig->internal_function.handler = PHP_FN(phar_##func); \
847849
}

ext/phar/phar.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,8 @@ static zend_result phar_parse_pharfile(php_stream *fp, char *fname, size_t fname
12631263
MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
12641264
}
12651265

1266-
if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
1266+
fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len);
1267+
if (fd_ptr) {
12671268
if (SUCCESS != phar_free_alias(fd_ptr)) {
12681269
signature = NULL;
12691270
fp = NULL;
@@ -1460,10 +1461,9 @@ ZEND_ATTRIBUTE_NONNULL_ARGS(1, 7, 8) zend_result phar_create_or_parse_filename(c
14601461
/* assume tar format, PharData can specify other */
14611462
mydata->is_tar = 1;
14621463
} else {
1463-
phar_archive_data *fd_ptr;
1464-
1465-
if (alias && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
1466-
if (SUCCESS != phar_free_alias(fd_ptr)) {
1464+
if (alias) {
1465+
const phar_archive_data *fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len);
1466+
if (fd_ptr && SUCCESS != phar_free_alias(fd_ptr)) {
14671467
spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
14681468

14691469
zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
@@ -1669,14 +1669,14 @@ static zend_result phar_open_from_fp(php_stream* fp, char *fname, size_t fname_l
16691669
continue;
16701670
} else if (!memcmp(pos, bz_magic, 3)) {
16711671
php_stream_filter *filter;
1672-
php_stream *temp;
16731672

16741673
if (!PHAR_G(has_bz2)) {
16751674
MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
16761675
}
16771676

16781677
/* entire file is bzip-compressed, uncompress to temporary file */
1679-
if (!(temp = php_stream_fopen_tmpfile())) {
1678+
php_stream *temp = php_stream_fopen_tmpfile();
1679+
if (!temp) {
16801680
MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
16811681
}
16821682

@@ -1750,10 +1750,10 @@ static zend_result phar_open_from_fp(php_stream* fp, char *fname, size_t fname_l
17501750
static zend_result phar_analyze_path(const char *fname, const char *ext, size_t ext_len, int for_create) /* {{{ */
17511751
{
17521752
php_stream_statbuf ssb;
1753-
char *realpath;
17541753
char *filename = estrndup(fname, (ext - fname) + ext_len);
17551754

1756-
if ((realpath = expand_filepath(filename, NULL))) {
1755+
char *realpath = expand_filepath(filename, NULL);
1756+
if (realpath) {
17571757
#ifdef PHP_WIN32
17581758
phar_unixify_path_separators(realpath, strlen(realpath));
17591759
#endif
@@ -1800,7 +1800,8 @@ static zend_result phar_analyze_path(const char *fname, const char *ext, size_t
18001800

18011801
if (SUCCESS != php_stream_stat_path((char *) filename, &ssb)) {
18021802
if (!slash) {
1803-
if (!(realpath = expand_filepath(filename, NULL))) {
1803+
realpath = expand_filepath(filename, NULL);
1804+
if (!realpath) {
18041805
efree(filename);
18051806
return FAILURE;
18061807
}
@@ -1943,7 +1944,8 @@ zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len
19431944
phar_archive_data *pphar;
19441945

19451946
if (is_complete) {
1946-
if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), (char *) filename, filename_len))) {
1947+
pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), filename, filename_len);
1948+
if (pphar) {
19471949
*ext_str = filename + (filename_len - pphar->ext_len);
19481950
woohoo:
19491951
*ext_len = pphar->ext_len;
@@ -1963,7 +1965,8 @@ zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len
19631965
return FAILURE;
19641966
}
19651967

1966-
if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, (char *) filename, filename_len))) {
1968+
pphar = PHAR_G(manifest_cached) ? zend_hash_str_find_ptr(&cached_phars, filename, filename_len) : NULL;
1969+
if (pphar) {
19671970
*ext_str = filename + (filename_len - pphar->ext_len);
19681971
goto woohoo;
19691972
}

0 commit comments

Comments
 (0)