Opened 6 years ago
Last modified 6 years ago
#12383 new Bugs
boost::asio fails to read more than 65536 bytes from file asyncronously
Reported by: | Owned by: | chris_kohlhoff | |
---|---|---|---|
Milestone: | To Be Determined | Component: | asio |
Version: | Boost 1.61.0 | Severity: | Problem |
Keywords: | Cc: |
Description
When more than 65536
bytes are read into a buffer from a file using boost::asio::windows::stream_handle
asynchronously, then buffer contains the wrong data after reading is completed.
Starting from 65537
th byte the buffer contains the the data from the very beginning of the file, rather than the expected data.
Here is a code example, which reproduces the issue:
auto handle = ::CreateFile(L"BigFile.xml", GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr); boost::asio::io_service ios; boost::asio::windows::stream_handle streamHandle(ios, handle); const auto to_read_bytes = 100000; char buffer[to_read_bytes]; boost::asio::async_read(streamHandle, boost::asio::buffer(buffer, to_read_bytes), [](auto &ec, auto read) { std::cout << "Bytes read: " << read << std::endl; }); ios.run(); auto bufferBegin = std::string(buffer, 38); auto bufferCorrupted = std::string(buffer + 65536, 38); // <- it contains bytes from the beginning of the file std::cout << "offset 0: " << bufferBegin << std::endl; std::cout << "offset 65536: " << bufferCorrupted << std::endl; ::CloseHandle(handle);
That code produces an output:
Bytes read: 100000
offset 0: <?xml version="1.0" encoding="UTF-8"?>
offset 65536: <?xml version="1.0" encoding="UTF-8"?>
The source file is a valid XML file, which is bigger than 65536 bytes in size.
This is reproducible with boost 1.61 + VS2015. Also that issue was in boost 1.55 + VS2010.
Operating systems are: Windows 7 and Windows Server 2008R2.
The solution for the issue and much more other details, including the reason of it, could be found in answers to my question on stackoverflow: http://stackoverflow.com/questions/38833771/boostasio-fails-to-read-more-than-65536-bytes-from-file
I still think, it's a defect. At least such limitation in 65536 bytes should be mentioned in the documentation for
boost::asio::windows::stream_handle