Skip to content

Commit 3a9d599

Browse files
authored
phar: Use a loop instead of goto when looking for extensions (php#20289)
This gets rid of a TODO and makes the code clearer.
1 parent 0cdf7be commit 3a9d599

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

ext/phar/phar.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,45 +2034,40 @@ zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len
20342034
}
20352035
}
20362036

2037-
// TODO Use some sort of loop here instead of a goto
20382037
pos = memchr(filename + 1, '.', filename_len);
2039-
next_extension:
2040-
if (!pos) {
2041-
return FAILURE;
2042-
}
2043-
2044-
while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
2045-
pos = memchr(pos + 1, '.', filename_len - (pos - filename) - 1);
2046-
if (!pos) {
2047-
return FAILURE;
2038+
while (pos) {
2039+
while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
2040+
pos = memchr(pos + 1, '.', filename_len - (pos - filename) - 1);
2041+
if (!pos) {
2042+
return FAILURE;
2043+
}
20482044
}
2049-
}
20502045

2051-
slash = memchr(pos, '/', filename_len - (pos - filename));
2046+
slash = memchr(pos, '/', filename_len - (pos - filename));
20522047

2053-
if (!slash) {
2054-
/* this is a url like "phar://blah.phar" with no directory */
2055-
*ext_str = pos;
2056-
*ext_len = strlen(pos);
2048+
if (!slash) {
2049+
/* this is a url like "phar://blah.phar" with no directory */
2050+
*ext_str = pos;
2051+
*ext_len = strlen(pos);
20572052

2058-
/* file extension must contain "phar" */
2059-
return phar_check_str(filename, *ext_str, *ext_len, executable, for_create);
2060-
}
2053+
/* file extension must contain "phar" */
2054+
return phar_check_str(filename, *ext_str, *ext_len, executable, for_create);
2055+
}
20612056

2062-
/* we've found an extension that ends at a directory separator */
2063-
*ext_str = pos;
2064-
*ext_len = slash - pos;
2057+
/* we've found an extension that ends at a directory separator */
2058+
*ext_str = pos;
2059+
*ext_len = slash - pos;
20652060

2066-
if (phar_check_str(filename, *ext_str, *ext_len, executable, for_create) == SUCCESS) {
2067-
return SUCCESS;
2068-
}
2061+
if (phar_check_str(filename, *ext_str, *ext_len, executable, for_create) == SUCCESS) {
2062+
return SUCCESS;
2063+
}
20692064

2070-
/* look for more extensions */
2071-
pos = strchr(pos + 1, '.');
2072-
if (pos) {
2073-
*ext_str = NULL;
2074-
*ext_len = 0;
2075-
goto next_extension;
2065+
/* look for more extensions */
2066+
pos = strchr(pos + 1, '.');
2067+
if (pos) {
2068+
*ext_str = NULL;
2069+
*ext_len = 0;
2070+
}
20762071
}
20772072

20782073
return FAILURE;

0 commit comments

Comments
 (0)