Skip to content

Commit ae44843

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.
1 parent 983be08 commit ae44843

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
Notice: Phar::loadPhar(): Read of 8192 bytes failed with errno=21 Is a directory in %s on line %d
15+
internal corruption of phar "/run/media/niels/MoreData/php-8.3" (truncated entry)

0 commit comments

Comments
 (0)