From 87fb595aee96055c6e1effa877ca3225213a2a41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 4 Sep 2018 10:43:27 +0200 Subject: [PATCH 1/2] Clear WSA buffers to make sure to detect erroneous accesses. --- source/eventcore/drivers/winapi/sockets.d | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/source/eventcore/drivers/winapi/sockets.d b/source/eventcore/drivers/winapi/sockets.d index 28512d3..7ef71fb 100644 --- a/source/eventcore/drivers/winapi/sockets.d +++ b/source/eventcore/drivers/winapi/sockets.d @@ -243,6 +243,8 @@ final class WinAPIEventDriverSockets : EventDriverSockets { slot.read.wsabuf[0].len = buffer.length; slot.read.wsabuf[0].buf = () @trusted { return buffer.ptr; } (); + void resetBuffers() { slot.read.buffer = null; slot.read.wsabuf[0] = WSABUF.init; } + auto ovl = mode == IOMode.immediate ? null : &slot.read.overlapped.overlapped; DWORD flags = 0; auto handler = &overlappedIOHandler!(onIOReadCompleted, DWORD); @@ -251,10 +253,12 @@ final class WinAPIEventDriverSockets : EventDriverSockets { auto err = WSAGetLastError(); if (err == WSA_IO_PENDING) { if (mode == IOMode.immediate) { + resetBuffers(); on_read_finish(socket, IOStatus.wouldBlock, 0); return; } } else { + resetBuffers(); on_read_finish(socket, IOStatus.error, 0); return; } @@ -277,6 +281,8 @@ final class WinAPIEventDriverSockets : EventDriverSockets { slot.common.core.removeWaiter(); auto cb = slot.streamSocket.read.callback; slot.streamSocket.read.callback = null; + slot.streamSocket.read.buffer = null; + slot.streamSocket.read.wsabuf[0] = WSABUF.init; if (slot.common.driver.releaseRef(cast(StreamSocketFD)slot.common.fd)) cb(cast(StreamSocketFD)slot.common.fd, status, nsent); } @@ -562,6 +568,8 @@ final class WinAPIEventDriverSockets : EventDriverSockets { slot.common.core.removeWaiter(); auto cb = slot.datagramSocket.read.callback; slot.datagramSocket.read.callback = null; + slot.datagramSocket.read.buffer = null; + slot.datagramSocket.read.wsabuf = WSABUF.init; scope addr = new RefAddress(cast(sockaddr*)&slot.datagramSocket.sourceAddr, slot.datagramSocket.sourceAddrLen); if (slot.common.driver.releaseRef(cast(DatagramSocketFD)slot.common.fd)) cb(cast(DatagramSocketFD)slot.common.fd, status, nsent, status == IOStatus.ok ? addr : null); @@ -919,6 +927,8 @@ static struct StreamDirection(bool RO) { size_t bytesTransferred; IOMode mode; IOCallback callback; + + void reset() { buffer = null; wsabuf[0] = WSABUF.init; callback = null; } } private struct StreamListenSocketSlot { @@ -943,4 +953,6 @@ static struct DgramDirection(bool RO) { size_t bytesTransferred; IOMode mode; DatagramIOCallback callback; + + void reset() { buffer = null; wsabuf[0] = WSABUF.init; callback = null; } } From 8495d7d91c77a8c4d155183b2184e7f68a9123fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Tue, 4 Sep 2018 10:42:08 +0200 Subject: [PATCH 2/2] Fix UCS-2 to UTF-8 conversion in the Windows watchers implementation. --- source/eventcore/drivers/winapi/watchers.d | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/eventcore/drivers/winapi/watchers.d b/source/eventcore/drivers/winapi/watchers.d index 2ec45eb..ea6bdb6 100644 --- a/source/eventcore/drivers/winapi/watchers.d +++ b/source/eventcore/drivers/winapi/watchers.d @@ -109,6 +109,7 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { private static nothrow void onIOCompleted(DWORD dwError, DWORD cbTransferred, OVERLAPPED_CORE* overlapped) { + import std.algorithm.iteration : map; import std.conv : to; import std.file : isDir; import std.path : dirName, baseName, buildPath; @@ -155,7 +156,7 @@ final class WinAPIEventDriverWatchers : EventDriverWatchers { } ch.baseDirectory = slot.directory; - auto path = () @trusted { scope (failure) assert(false); return to!string(fni.FileName[0 .. fni.FileNameLength/2]); } (); + auto path = () @trusted { try return fni.FileName[0 .. fni.FileNameLength/2].map!(ch => dchar(ch)).to!string; catch (Exception e) assert(false, e.msg); } (); auto fullpath = buildPath(slot.directory, path); ch.directory = dirName(path); if (ch.directory == ".") ch.directory = "";