Skip to content

Commit 4ee2539

Browse files
committed
Fix GH-20442: Phar does not respect case-insensitiveness of __halt_compiler() when reading stub
Functions are case insensitive. The flush code already takes this into account by checking for the __halt_compiler() symbol in a case insensitive manner; however the parsing code did not do that yet. Closes GH-20445.
1 parent 80b7316 commit 4ee2539

File tree

4 files changed

+26
-32
lines changed

4 files changed

+26
-32
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ PHP NEWS
1313
. Fixed bug GH-20329 (opcache.file_cache broken with full interned string
1414
buffer). (Arnaud)
1515

16+
- Phar:
17+
. Fixed bug GH-20442 (Phar does not respect case-insensitiveness of
18+
__halt_compiler() when reading stub). (ndossche, TimWolla)
19+
1620
- Standard:
1721
. Fix memory leak in array_diff() with custom type checks. (ndossche)
1822

ext/phar/phar.c

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,35 +1590,6 @@ int phar_open_from_filename(char *fname, size_t fname_len, char *alias, size_t a
15901590
}
15911591
/* }}}*/
15921592

1593-
static inline char *phar_strnstr(const char *buf, size_t buf_len, const char *search, size_t search_len) /* {{{ */
1594-
{
1595-
const char *c;
1596-
ptrdiff_t so_far = 0;
1597-
1598-
if (buf_len < search_len) {
1599-
return NULL;
1600-
}
1601-
1602-
c = buf - 1;
1603-
1604-
do {
1605-
if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
1606-
return (char *) NULL;
1607-
}
1608-
1609-
so_far = c - buf;
1610-
1611-
if (so_far >= (buf_len - search_len)) {
1612-
return (char *) NULL;
1613-
}
1614-
1615-
if (!memcmp(c, search, search_len)) {
1616-
return (char *) c;
1617-
}
1618-
} while (1);
1619-
}
1620-
/* }}} */
1621-
16221593
/**
16231594
* Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
16241595
* that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
@@ -1630,7 +1601,8 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16301601
static const char zip_magic[] = "PK\x03\x04";
16311602
static const char gz_magic[] = "\x1f\x8b\x08";
16321603
static const char bz_magic[] = "BZh";
1633-
char *pos, test = '\0';
1604+
const char *pos;
1605+
char test = '\0';
16341606
int recursion_count = 3; // arbitrary limit to avoid too deep or even infinite recursion
16351607
const int window_size = 1024;
16361608
char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
@@ -1779,14 +1751,14 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
17791751
}
17801752

17811753
if (got >= 512) {
1782-
if (phar_is_tar(pos, fname)) {
1754+
if (phar_is_tar((char *) pos, fname)) { /* TODO: fix const correctness */
17831755
php_stream_rewind(fp);
17841756
return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error);
17851757
}
17861758
}
17871759
}
17881760

1789-
if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
1761+
if (got > 0 && (pos = php_memnistr(buffer, token, tokenlen, buffer + got + sizeof(token))) != NULL) {
17901762
halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
17911763
return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, compression, error);
17921764
}

ext/phar/tests/files/gh20442.phar

144 Bytes
Binary file not shown.

ext/phar/tests/gh20442.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-20442 (Phar does not respect case-insensitiveness of __halt_compiler() when reading stub)
3+
--EXTENSIONS--
4+
phar
5+
--FILE--
6+
<?php
7+
8+
$phar = new Phar(__DIR__.'/files/gh20442.phar');
9+
var_dump($phar->count());
10+
var_dump($phar->getStub());
11+
12+
?>
13+
--EXPECT--
14+
int(1)
15+
string(50) "<?php
16+
echo "Hello World!";
17+
__halt_compiler(); ?>
18+
"

0 commit comments

Comments
 (0)