]> granicus.if.org Git - php/commitdiff
Fixed bug #77081 ftruncate() changes seek pointer in c mode
authorAnatol Belski <ab@php.net>
Tue, 30 Oct 2018 19:42:00 +0000 (20:42 +0100)
committerAnatol Belski <ab@php.net>
Tue, 30 Oct 2018 19:42:00 +0000 (20:42 +0100)
NEWS
ext/standard/tests/file/ftruncate_bug77081.phpt [new file with mode: 0644]
main/streams/plain_wrapper.c

diff --git a/NEWS b/NEWS
index 17dbea657dbf0ac3c5e616ef4584c1e9164b5338..34533b8872503f22854b8207c8916f7e449ca55c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,9 @@ PHP                                                                        NEWS
   . Fixed bug #50675 (SoapClient can't handle object references correctly).
     (Cameron Porter)
 
+- Standard:
+  . Fixed bug #77081 (ftruncate() changes seek pointer in c mode). (cmb, Anatol)
+
 - XML:
   . Fixed bug 71592 (External entity processing never fails). (cmb)
 
diff --git a/ext/standard/tests/file/ftruncate_bug77081.phpt b/ext/standard/tests/file/ftruncate_bug77081.phpt
new file mode 100644 (file)
index 0000000..7a9aa69
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+Bug #77081 ftruncate() changes seek pointer in c mode
+--FILE--
+<?php
+
+$filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test77081";
+
+file_put_contents($filename, 'foo');
+$stream = fopen($filename, 'c');
+ftruncate($stream, 0);
+var_dump(ftell($stream));
+fwrite($stream, 'bar');
+fclose($stream);
+var_dump(file_get_contents($filename));
+
+?>
+--CLEAN--
+<?php
+$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test77081";
+unlink($fn);
+?>
+--EXPECT--
+int(0)
+string(3) "bar"
index fdfc7b4f634e68984eb4231c7fb6619d4698a558..d8e1f517b4775a298b93d9d1f78900078aacab34 100644 (file)
@@ -858,12 +858,13 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
                                                return PHP_STREAM_OPTION_RETURN_ERR;
                                        }
 
-                                       LARGE_INTEGER old_sz;
-                                       if (!GetFileSizeEx(h, &old_sz)) {
+                                       LARGE_INTEGER sz, old_sz;
+                                       sz.QuadPart = 0;
+
+                                       if (!SetFilePointerEx(h, sz, &old_sz, FILE_CURRENT)) {
                                                return PHP_STREAM_OPTION_RETURN_ERR;
                                        }
 
-                                       LARGE_INTEGER sz;
 #if defined(_WIN64)
                                        sz.HighPart = (new_size >> 32);
                                        sz.LowPart = (new_size & 0xffffffff);