From: Anatol Belski Date: Tue, 28 Aug 2018 13:42:39 +0000 (+0200) Subject: Fixed bug #76803 ftruncate changes file pointer X-Git-Tag: php-7.3.0RC1~48 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7728160784785771d1933f5f0a8c4d7a735470b0;p=php Fixed bug #76803 ftruncate changes file pointer --- diff --git a/ext/standard/tests/file/ftruncate_bug76803.phpt b/ext/standard/tests/file/ftruncate_bug76803.phpt new file mode 100644 index 0000000000..7e99cf2fb2 --- /dev/null +++ b/ext/standard/tests/file/ftruncate_bug76803.phpt @@ -0,0 +1,38 @@ +--TEST-- +Bug #76803 ftruncate changes file pointer +--FILE-- + +--CLEAN-- + +--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 08c9380ddd..b2fc00b226 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;