Skip to content

Commit 18f3144

Browse files
committed
More return value handling fixes
1 parent 09aa58c commit 18f3144

File tree

4 files changed

+94
-12
lines changed

4 files changed

+94
-12
lines changed

ext/phar/phar_object.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,10 +1351,13 @@ static phar_entry_data *phar_build_entry_data(char *fname, size_t fname_len, cha
13511351
zval rv;
13521352
zend_call_method_with_0_params(Z_OBJ_P(file_info), Z_OBJCE_P(file_info), NULL, "getMTime", &rv);
13531353

1354+
if (Z_ISREF(rv)) {
1355+
zend_unwrap_reference(&rv);
1356+
}
1357+
13541358
if (UNEXPECTED(Z_TYPE(rv) != IS_LONG)) {
1355-
/* Either an exception happened, or the function returned false to indicate failure. */
1356-
ZEND_ASSERT(Z_TYPE(rv) == IS_UNDEF || Z_TYPE(rv) == IS_FALSE);
1357-
*error = estrdup("getMTime() failed");
1359+
/* Either it's a tentative type failure, an exception happened, or the function returned false to indicate failure. */
1360+
*error = estrdup("getMTime() must return an int");
13581361
return NULL;
13591362
}
13601363

ext/phar/tests/buildFromIterator_user_overrides/getMTime.phpt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ N. Dossche
1212
<?php
1313

1414
class MySplFileInfo extends SplFileInfo {
15-
public function getPathname(): string {
16-
echo "[Pathname]\n";
17-
return parent::getPathname();
18-
}
19-
2015
public function getMTime(): int|false {
2116
echo "[MTime]\n";
2217
return 123;
@@ -74,7 +69,6 @@ $workdir = __DIR__.'/001';
7469
?>
7570
--EXPECTF--
7671
[ Found: %shello.txt ]
77-
[Pathname]
7872
[MTime]
7973
object(PharFileInfo)#%d (2) {
8074
["pathName":"SplFileInfo":private]=>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--TEST--
2+
buildFromIterator with user overrides - getMTime() by ref
3+
--EXTENSIONS--
4+
phar
5+
--INI--
6+
phar.readonly=0
7+
phar.require_hash=0
8+
--CREDITS--
9+
Arne Blankerts
10+
N. Dossche
11+
--FILE--
12+
<?php
13+
14+
class MySplFileInfo extends SplFileInfo {
15+
public function &getMTime(): int|false {
16+
static $data = 123;
17+
echo "[MTime]\n";
18+
return $data;
19+
}
20+
}
21+
22+
class MyIterator extends RecursiveDirectoryIterator {
23+
public function current(): SplFileInfo {
24+
echo "[ Found: " . parent::current()->getPathname() . " ]\n";
25+
return new MySplFileInfo(parent::current()->getPathname());
26+
}
27+
}
28+
29+
$workdir = __DIR__.'/007';
30+
mkdir($workdir . '/content', recursive: true);
31+
file_put_contents($workdir . '/content/hello.txt', "Hello world.");
32+
33+
$phar = new \Phar($workdir . '/test.phar');
34+
$phar->startBuffering();
35+
$phar->buildFromIterator(
36+
new RecursiveIteratorIterator(
37+
new MyIterator($workdir . '/content', FilesystemIterator::SKIP_DOTS)
38+
),
39+
$workdir
40+
);
41+
$phar->stopBuffering();
42+
43+
44+
$result = new \Phar($workdir . '/test.phar', 0, 'test.phar');
45+
var_dump($result['content/hello.txt']);
46+
var_dump($result['content/hello.txt']->getATime());
47+
var_dump($result['content/hello.txt']->getMTime());
48+
var_dump($result['content/hello.txt']->getCTime());
49+
50+
?>
51+
--CLEAN--
52+
<?php
53+
$workdir = __DIR__.'/007';
54+
@unlink($workdir . '/test.phar');
55+
@unlink($workdir . '/content/hello.txt');
56+
@rmdir($workdir . '/content');
57+
@rmdir($workdir);
58+
?>
59+
--EXPECTF--
60+
[ Found: %shello.txt ]
61+
[MTime]
62+
object(PharFileInfo)#%d (2) {
63+
["pathName":"SplFileInfo":private]=>
64+
string(%d) "phar://%shello.txt"
65+
["fileName":"SplFileInfo":private]=>
66+
string(%d) "hello.txt"
67+
}
68+
int(123)
69+
int(123)
70+
int(123)

ext/phar/tests/buildFromIterator_user_overrides/getMTime_errors.phpt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ class MySplFileInfo3 extends SplFileInfo {
3636
}
3737
}
3838

39+
class MySplFileInfo4 extends SplFileInfo {
40+
#[\ReturnTypeWillChange]
41+
public function getMTime(): string {
42+
echo "[MTime]\n";
43+
return "wrong type";
44+
}
45+
}
46+
3947
class MyIterator extends RecursiveDirectoryIterator {
4048
public function current(): SplFileInfo {
4149
static $counter = 0;
@@ -47,6 +55,8 @@ class MyIterator extends RecursiveDirectoryIterator {
4755
return new MySplFileInfo2(parent::current()->getPathname());
4856
} else if ($counter === 3) {
4957
return new MySplFileInfo3(parent::current()->getPathname());
58+
} else if ($counter === 4) {
59+
return new MySplFileInfo4(parent::current()->getPathname());
5060
}
5161
}
5262
}
@@ -55,7 +65,7 @@ $workdir = __DIR__.'/002';
5565
mkdir($workdir . '/content', recursive: true);
5666
file_put_contents($workdir . '/content/hello.txt', "Hello world.");
5767

58-
for ($i = 0; $i < 3; $i++) {
68+
for ($i = 0; $i < 4; $i++) {
5969
echo "--- Iteration $i ---\n";
6070
try {
6171
$phar = new \Phar($workdir . "/test$i.phar");
@@ -80,6 +90,7 @@ for ($i = 0; $i < 3; $i++) {
8090
<?php
8191
$workdir = __DIR__.'/002';
8292
@unlink($workdir . '/content/hello.txt');
93+
@unlink($workdir . '/test3.phar');
8394
@unlink($workdir . '/test2.phar');
8495
@unlink($workdir . '/test1.phar');
8596
@unlink($workdir . '/test0.phar');
@@ -94,9 +105,13 @@ Entry content%chello.txt cannot be created: timestamp is limited to 32-bit
94105
--- Iteration 1 ---
95106
[ Found: %shello.txt ]
96107
[MTime]
97-
Entry content%chello.txt cannot be created: getMTime() failed
108+
Entry content/hello.txt cannot be created: getMTime() must return an int
98109
--- Iteration 2 ---
99110
[ Found: %shello.txt ]
100111
[MTime]
101-
Entry content/hello.txt cannot be created: getMTime() failed
112+
Entry content/hello.txt cannot be created: getMTime() must return an int
102113
Previous: Throwing an exception inside getMTime()
114+
--- Iteration 3 ---
115+
[ Found: /run/media/niels/MoreData/php-src-multitasking/ext/phar/tests/buildFromIterator_user_overrides/002/content/hello.txt ]
116+
[MTime]
117+
Entry content/hello.txt cannot be created: getMTime() must return an int

0 commit comments

Comments
 (0)