From 8827cc34cf8c77828330182ee1e6d0b9438d489c Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 30 Oct 2018 20:42:00 +0100 Subject: [PATCH] Fixed bug #77081 ftruncate() changes seek pointer in c mode --- NEWS | 3 +++ .../tests/file/ftruncate_bug77081.phpt | 24 +++++++++++++++++++ main/streams/plain_wrapper.c | 7 +++--- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 ext/standard/tests/file/ftruncate_bug77081.phpt diff --git a/NEWS b/NEWS index 17dbea657db..34533b88725 100644 --- 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 index 00000000000..7a9aa691fb3 --- /dev/null +++ b/ext/standard/tests/file/ftruncate_bug77081.phpt @@ -0,0 +1,24 @@ +--TEST-- +Bug #77081 ftruncate() changes seek pointer in c mode +--FILE-- + +--CLEAN-- + +--EXPECT-- +int(0) +string(3) "bar" diff --git a/main/streams/plain_wrapper.c b/main/streams/plain_wrapper.c index fdfc7b4f634..d8e1f517b47 100644 --- a/main/streams/plain_wrapper.c +++ b/main/streams/plain_wrapper.c @@ -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);