Skip to content

Commit 22aaa20

Browse files
committed
Fix GH-20732: Phar::LoadPhar undefined behavior when loading directory
The size of `got` was incorrect: it being unsigned means that the error return codes are converted from -1 to SIZE_MAX. We should use ssize_t instead. Closes GH-20735.
1 parent ee01438 commit 22aaa20

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ PHP NEWS
2929
. Fixed bug GH-20674 (Fix GH-20674 mb_decode_mimeheader does not handle
3030
separator). (Yuya Hamada)
3131

32+
- Phar:
33+
. Fixed bug GH-20732 (Phar::LoadPhar undefined behavior when reading fails).
34+
(ndossche)
35+
3236
- SPL:
3337
. Fixed bug GH-20678 (resource created by GlobIterator crashes with fclose()).
3438
(David Carlier)

ext/phar/phar.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16091609
const zend_long readsize = sizeof(buffer) - sizeof(token);
16101610
const zend_long tokenlen = sizeof(token) - 1;
16111611
zend_long halt_offset;
1612-
size_t got;
1612+
ssize_t got;
16131613
uint32_t compression = PHAR_FILE_COMPRESSED_NONE;
16141614

16151615
if (error) {
@@ -1627,7 +1627,7 @@ static int phar_open_from_fp(php_stream* fp, char *fname, size_t fname_len, char
16271627
/* Maybe it's better to compile the file instead of just searching, */
16281628
/* but we only want the offset. So we want a .re scanner to find it. */
16291629
while(!php_stream_eof(fp)) {
1630-
if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
1630+
if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < tokenlen) {
16311631
MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
16321632
}
16331633

ext/phar/tests/gh20732.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-20732 (Phar::LoadPhar undefined behavior when loading directory)
3+
--EXTENSIONS--
4+
phar
5+
--FILE--
6+
<?php
7+
try {
8+
@Phar::LoadPhar('.');
9+
} catch (PharException $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
?>
13+
--EXPECTF--
14+
%r(internal corruption of phar "%s" \(truncated entry\)|unable to open phar for reading ".")%r

0 commit comments

Comments
 (0)