Skip to content

Commit 8827cc3

Browse files
committed
Fixed bug #77081 ftruncate() changes seek pointer in c mode
1 parent 2816a3f commit 8827cc3

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ PHP NEWS
1313
. Fixed bug #50675 (SoapClient can't handle object references correctly).
1414
(Cameron Porter)
1515

16+
- Standard:
17+
. Fixed bug #77081 (ftruncate() changes seek pointer in c mode). (cmb, Anatol)
18+
1619
- XML:
1720
. Fixed bug 71592 (External entity processing never fails). (cmb)
1821

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Bug #77081 ftruncate() changes seek pointer in c mode
3+
--FILE--
4+
<?php
5+
6+
$filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test77081";
7+
8+
file_put_contents($filename, 'foo');
9+
$stream = fopen($filename, 'c');
10+
ftruncate($stream, 0);
11+
var_dump(ftell($stream));
12+
fwrite($stream, 'bar');
13+
fclose($stream);
14+
var_dump(file_get_contents($filename));
15+
16+
?>
17+
--CLEAN--
18+
<?php
19+
$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test77081";
20+
unlink($fn);
21+
?>
22+
--EXPECT--
23+
int(0)
24+
string(3) "bar"

main/streams/plain_wrapper.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -858,12 +858,13 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
858858
return PHP_STREAM_OPTION_RETURN_ERR;
859859
}
860860

861-
LARGE_INTEGER old_sz;
862-
if (!GetFileSizeEx(h, &old_sz)) {
861+
LARGE_INTEGER sz, old_sz;
862+
sz.QuadPart = 0;
863+
864+
if (!SetFilePointerEx(h, sz, &old_sz, FILE_CURRENT)) {
863865
return PHP_STREAM_OPTION_RETURN_ERR;
864866
}
865867

866-
LARGE_INTEGER sz;
867868
#if defined(_WIN64)
868869
sz.HighPart = (new_size >> 32);
869870
sz.LowPart = (new_size & 0xffffffff);

0 commit comments

Comments
 (0)