com php-src: Fixed bug #76803 ftruncate changes file pointer: ext/standard/tests/file/ftruncate_bug76803.phpt main/streams/plain_wrapper.c
| From: | Anatol Belski | Date: | Tue, 28 Aug 2018 13:42:39 +0000 |
| Subject: | com php-src: Fixed bug #76803 ftruncate changes file pointer: ext/standard/tests/file/ftruncate_bug76803.phpt main/streams/plain_wrapper.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-102911@lists.php.net to get a copy of this message | ||
Commit: 7728160784785771d1933f5f0a8c4d7a735470b0
Author: Anatol Belski <ab@php.net> Tue, 28 Aug 2018 15:42:39 +0200
Parents: 441b6a6f5e7e19276d10485434c610278a781261
Branches: PHP-7.3 master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=7728160784785771d1933f5f0a8c4d7a735470b0
Log:
Fixed bug #76803 ftruncate changes file pointer
Bugs:
https://bugs.php.net/76803
Changed paths:
A ext/standard/tests/file/ftruncate_bug76803.phpt
M main/streams/plain_wrapper.c
Diff:
diff --git a/ext/standard/tests/file/ftruncate_bug76803.phpt
b/ext/standard/tests/file/ftruncate_bug76803.phpt
new file mode 100644
index 0000000..7e99cf2
--- /dev/null
+++ b/ext/standard/tests/file/ftruncate_bug76803.phpt
@@ -0,0 +1,38 @@
+--TEST--
+Bug #76803 ftruncate changes file pointer
+--FILE--
+<?php
+
+$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test76803";
+
+$f = fopen($fn, "w");
+fwrite($f, "Hello");
+ftruncate($f, 2);
+fwrite($f, "World");
+fclose($f);
+var_dump(addslashes(file_get_contents($fn)));
+
+$f = fopen($fn, "w");
+fwrite($f, "Hello");
+ftruncate($f, 2);
+fclose($f);
+var_dump(addslashes(file_get_contents($fn)));
+
+$f = fopen('php://memory', 'w+');
+fwrite($f, 'Hello');
+ftruncate($f, 2); // in 7.3 changes file pointer to 2
+fwrite($f, 'World');
+rewind($f);
+var_dump(addslashes(stream_get_contents($f)));
+fclose($f);
+
+?>
+--CLEAN--
+<?php
+$fn = dirname(__FILE__) . DIRECTORY_SEPARATOR . "test76803";
+unlink($fn);
+?>
+--EXPECT--
+string(13) "He\0\0\0World"
+string(2) "He"
+string(7) "HeWorld"
diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c
index 08c9380..b2fc00b 100644
--- a/main/streams/plain_wrapper.c
+++ b/main/streams/plain_wrapper.c
@@ -857,6 +857,12 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value,
void
if (INVALID_HANDLE_VALUE == h) {
return PHP_STREAM_OPTION_RETURN_ERR;
}
+
+ LARGE_INTEGER old_sz;
+ if (!GetFileSizeEx(h, &old_sz)) {
+ return PHP_STREAM_OPTION_RETURN_ERR;
+ }
+
LARGE_INTEGER sz;
#if defined(_WIN64)
sz.HighPart = (new_size >> 32);
@@ -871,6 +877,9 @@ static int php_stdiop_set_option(php_stream *stream, int option, int value, void
if (0 == SetEndOfFile(h)) {
return PHP_STREAM_OPTION_RETURN_ERR;
}
+ if (INVALID_SET_FILE_POINTER == SetFilePointerEx(h, old_sz, NULL, FILE_BEGIN) &&
NO_ERROR != GetLastError()) {
+ return PHP_STREAM_OPTION_RETURN_ERR;
+ }
return PHP_STREAM_OPTION_RETURN_OK;
#else
return ftruncate(fd, new_size) == 0 ? PHP_STREAM_OPTION_RETURN_OK :
PHP_STREAM_OPTION_RETURN_ERR;