Fix safety errors on DMD 2.072.0-b2.

This commit is contained in:
Sönke Ludwig 2016-10-12 10:00:31 +02:00
parent 9f5c71d29a
commit 2a44817911
2 changed files with 30 additions and 28 deletions

View file

@ -140,10 +140,14 @@ abstract class PosixEventDriver : EventDriver,
void invalidateSocket() @nogc @trusted nothrow { closeSocket(sock); sock = StreamSocketFD.invalid; } void invalidateSocket() @nogc @trusted nothrow { closeSocket(sock); sock = StreamSocketFD.invalid; }
int bret;
() @trusted { // scope
scope bind_addr = new UnknownAddress; scope bind_addr = new UnknownAddress;
bind_addr.name.sa_family = cast(ushort)address.addressFamily; bind_addr.name.sa_family = cast(ushort)address.addressFamily;
bind_addr.name.sa_data[] = 0; bind_addr.name.sa_data[] = 0;
if (() @trusted { return bind(sock, bind_addr.name, bind_addr.nameLen); } () != 0) { bret = bind(sock, bind_addr.name, bind_addr.nameLen);
} ();
if (bret != 0) {
invalidateSocket(); invalidateSocket();
on_connect(sock, ConnectStatus.bindFailure); on_connect(sock, ConnectStatus.bindFailure);
return sock; return sock;
@ -230,12 +234,12 @@ abstract class PosixEventDriver : EventDriver,
private void onAccept(FD listenfd) private void onAccept(FD listenfd)
{ {
scope addr = new UnknownAddress;
foreach (i; 0 .. 20) { foreach (i; 0 .. 20) {
int sockfd; int sockfd;
version (Windows) int addr_len = addr.nameLen; sockaddr_storage addr;
else uint addr_len = addr.nameLen; version (Windows) int addr_len = addr.sizeof;
() @trusted { sockfd = accept(listenfd, addr.name, &addr_len); } (); else uint addr_len = addr.sizeof;
() @trusted { sockfd = accept(listenfd, () @trusted { return cast(sockaddr*)&addr; } (), &addr_len); } ();
if (sockfd == -1) break; if (sockfd == -1) break;
setSocketNonBlocking(cast(SocketFD)sockfd); setSocketNonBlocking(cast(SocketFD)sockfd);
@ -316,7 +320,7 @@ abstract class PosixEventDriver : EventDriver,
private void onSocketRead(FD fd) private void onSocketRead(FD fd)
{ {
auto slot = &m_fds[fd]; auto slot = () @trusted { return &m_fds[fd]; } ();
auto socket = cast(StreamSocketFD)fd; auto socket = cast(StreamSocketFD)fd;
void finalize()(IOStatus status) void finalize()(IOStatus status)
@ -409,7 +413,7 @@ abstract class PosixEventDriver : EventDriver,
private void onSocketWrite(FD fd) private void onSocketWrite(FD fd)
{ {
auto slot = &m_fds[fd]; auto slot = () @trusted { return &m_fds[fd]; } ();
auto socket = cast(StreamSocketFD)fd; auto socket = cast(StreamSocketFD)fd;
sizediff_t ret; sizediff_t ret;
@ -479,7 +483,7 @@ abstract class PosixEventDriver : EventDriver,
private void onSocketDataAvailable(FD fd) private void onSocketDataAvailable(FD fd)
{ {
auto slot = &m_fds[fd]; auto slot = () @trusted { return &m_fds[fd]; } ();
auto socket = cast(StreamSocketFD)fd; auto socket = cast(StreamSocketFD)fd;
void finalize()(IOStatus status) void finalize()(IOStatus status)
@ -526,11 +530,13 @@ abstract class PosixEventDriver : EventDriver,
} }
void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish) void receive(DatagramSocketFD socket, ubyte[] buffer, IOMode mode, DatagramIOCallback on_receive_finish)
{ @trusted { // DMD 2.072.0-b2: scope considered unsafe
import std.typecons : scoped;
assert(mode != IOMode.all, "Only IOMode.immediate and IOMode.once allowed for datagram sockets."); assert(mode != IOMode.all, "Only IOMode.immediate and IOMode.once allowed for datagram sockets.");
sizediff_t ret; sizediff_t ret;
scope src_addr = new UnknownAddress; scope src_addr = new UnknownAddress();
socklen_t src_addr_len = src_addr.nameLen; socklen_t src_addr_len = src_addr.nameLen;
() @trusted { ret = .recvfrom(socket, buffer.ptr, buffer.length, 0, src_addr.name, &src_addr_len); } (); () @trusted { ret = .recvfrom(socket, buffer.ptr, buffer.length, 0, src_addr.name, &src_addr_len); } ();
@ -568,8 +574,8 @@ abstract class PosixEventDriver : EventDriver,
} }
private void onDgramRead(FD fd) private void onDgramRead(FD fd)
{ @trusted { // DMD 2.072.0-b2: scope considered unsafe
auto slot = &m_fds[fd]; auto slot = () @trusted { return &m_fds[fd]; } ();
auto socket = cast(DatagramSocketFD)fd; auto socket = cast(DatagramSocketFD)fd;
sizediff_t ret; sizediff_t ret;
@ -637,7 +643,7 @@ abstract class PosixEventDriver : EventDriver,
private void onDgramWrite(FD fd) private void onDgramWrite(FD fd)
{ {
auto slot = &m_fds[fd]; auto slot = () @trusted { return &m_fds[fd]; } ();
auto socket = cast(DatagramSocketFD)fd; auto socket = cast(DatagramSocketFD)fd;
sizediff_t ret; sizediff_t ret;
@ -662,14 +668,14 @@ abstract class PosixEventDriver : EventDriver,
final override void addRef(SocketFD fd) final override void addRef(SocketFD fd)
{ {
auto pfd = &m_fds[fd]; auto pfd = () @trusted { return &m_fds[fd]; } ();
assert(pfd.refCount > 0, "Adding reference to unreferenced socket FD."); assert(pfd.refCount > 0, "Adding reference to unreferenced socket FD.");
m_fds[fd].refCount++; m_fds[fd].refCount++;
} }
final override void releaseRef(SocketFD fd) final override void releaseRef(SocketFD fd)
{ {
auto pfd = &m_fds[fd]; auto pfd = () @trusted { return &m_fds[fd]; } ();
assert(pfd.refCount > 0, "Releasing reference to unreferenced socket FD."); assert(pfd.refCount > 0, "Releasing reference to unreferenced socket FD.");
if (--m_fds[fd].refCount == 0) { if (--m_fds[fd].refCount == 0) {
unregisterFD(fd); unregisterFD(fd);
@ -725,8 +731,7 @@ abstract class PosixEventDriver : EventDriver,
import std.algorithm.searching : countUntil; import std.algorithm.searching : countUntil;
import std.algorithm.mutation : remove; import std.algorithm.mutation : remove;
auto slot = &m_fds[event]; m_fds[event].waiters.removePending(on_event);
slot.waiters.removePending(on_event);
} }
private void onEvent(FD event) private void onEvent(FD event)
@ -740,15 +745,13 @@ abstract class PosixEventDriver : EventDriver,
final override void addRef(EventID descriptor) final override void addRef(EventID descriptor)
{ {
auto pfd = &m_fds[descriptor]; assert(m_fds[descriptor].refCount > 0, "Adding reference to unreferenced event FD.");
assert(pfd.refCount > 0, "Adding reference to unreferenced event FD.");
m_fds[descriptor].refCount++; m_fds[descriptor].refCount++;
} }
final override void releaseRef(EventID descriptor) final override void releaseRef(EventID descriptor)
{ {
auto pfd = &m_fds[descriptor]; assert(m_fds[descriptor].refCount > 0, "Releasing reference to unreferenced event FD.");
assert(pfd.refCount > 0, "Releasing reference to unreferenced event FD.");
if (--m_fds[descriptor].refCount == 0) { if (--m_fds[descriptor].refCount == 0) {
unregisterFD(descriptor); unregisterFD(descriptor);
clearFD(descriptor); clearFD(descriptor);

View file

@ -223,16 +223,15 @@ log("start task");
final override void addRef(FileFD descriptor) final override void addRef(FileFD descriptor)
{ {
auto f = &m_files[descriptor]; m_files[descriptor].refCount++;
f.refCount++;
} }
final override void releaseRef(FileFD descriptor) final override void releaseRef(FileFD descriptor)
{ {
auto f = &m_files[descriptor]; auto f = () @trusted { return &m_files[descriptor]; } ();
if (!--f.refCount) { if (!--f.refCount) {
.close(descriptor); .close(descriptor);
m_files[descriptor] = FileInfo.init; *f = FileInfo.init;
assert(!m_activeReads.contains(descriptor)); assert(!m_activeReads.contains(descriptor));
assert(!m_activeWrites.contains(descriptor)); assert(!m_activeWrites.contains(descriptor));
} }