Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r60281 - in trunk/libs/iostreams: src test
From: daniel_james_at_[hidden]
Date: 2010-03-07 04:34:47


Author: danieljames
Date: 2010-03-07 04:34:45 EST (Sun, 07 Mar 2010)
New Revision: 60281
URL: http://svn.boost.org/trac/boost/changeset/60281

Log:
Fix error checks after calling SetFilePointer. Refs #3953

Thanks to Eg. Especially for writing a test.
Text files modified:
   trunk/libs/iostreams/src/mapped_file.cpp | 14 ++++++++------
   trunk/libs/iostreams/test/mapped_file_test.cpp | 17 +++++++++++++++++
   2 files changed, 25 insertions(+), 6 deletions(-)

Modified: trunk/libs/iostreams/src/mapped_file.cpp
==============================================================================
--- trunk/libs/iostreams/src/mapped_file.cpp (original)
+++ trunk/libs/iostreams/src/mapped_file.cpp 2010-03-07 04:34:45 EST (Sun, 07 Mar 2010)
@@ -128,12 +128,13 @@
         cleanup_and_throw("failed unmapping file");
 #ifdef BOOST_IOSTREAMS_WINDOWS
     stream_offset offset = ::SetFilePointer(handle_, 0, NULL, FILE_CURRENT);
- if (::GetLastError() != NO_ERROR)
- cleanup_and_throw("failed querying file pointer");
+ if (offset == INVALID_SET_FILE_POINTER && ::GetLastError() != NO_ERROR)
+ cleanup_and_throw("failed querying file pointer");
     LONG sizehigh = (new_size >> (sizeof(LONG) * 8));
     LONG sizelow = (new_size & 0xffffffff);
- ::SetFilePointer(handle_, sizelow, &sizehigh, FILE_BEGIN);
- if (::GetLastError() != NO_ERROR || !::SetEndOfFile(handle_))
+ DWORD result = ::SetFilePointer(handle_, sizelow, &sizehigh, FILE_BEGIN);
+ if ((result == INVALID_SET_FILE_POINTER && ::GetLastError() != NO_ERROR)
+ || !::SetEndOfFile(handle_))
         cleanup_and_throw("failed resizing mapped file");
     sizehigh = (offset >> (sizeof(LONG) * 8));
     sizelow = (offset & 0xffffffff);
@@ -197,8 +198,9 @@
     if (p.new_file_size != 0 && !readonly) {
         LONG sizehigh = (p.new_file_size >> (sizeof(LONG) * 8));
         LONG sizelow = (p.new_file_size & 0xffffffff);
- ::SetFilePointer(handle_, sizelow, &sizehigh, FILE_BEGIN);
- if (::GetLastError() != NO_ERROR || !::SetEndOfFile(handle_))
+ DWORD result = ::SetFilePointer(handle_, sizelow, &sizehigh, FILE_BEGIN);
+ if ((result == INVALID_SET_FILE_POINTER && ::GetLastError() != NO_ERROR)
+ || !::SetEndOfFile(handle_))
             cleanup_and_throw("failed setting file size");
     }
 

Modified: trunk/libs/iostreams/test/mapped_file_test.cpp
==============================================================================
--- trunk/libs/iostreams/test/mapped_file_test.cpp (original)
+++ trunk/libs/iostreams/test/mapped_file_test.cpp 2010-03-07 04:34:45 EST (Sun, 07 Mar 2010)
@@ -175,6 +175,23 @@
         );
     }
 
+ //--------------Writing to a pre-existing file---------------------------//
+ {
+ // Test for Bug #3953 - writing to a pre-existing mapped file.
+ boost::iostreams::test::test_file first, test;
+
+ mapped_file_params p(first.name());
+ p.new_file_size = boost::iostreams::test::data_reps * boost::iostreams::test::data_length();
+ boost::iostreams::stream<mapped_file_sink> out;
+ out.open(mapped_file_sink(p));
+ boost::iostreams::test::write_data_in_chars(out);
+ out.close();
+ BOOST_CHECK_MESSAGE(
+ boost::iostreams::test::compare_files(first.name(), test.name()),
+ "failed writing to pre-existing mapped file in chars"
+ );
+ }
+
     //--------------Random access with a mapped_file--------------------------//
 
     {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk