Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Mark all functions with a @safe interface as @trusted #3839

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/core/sys/windows/winsock2.d
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ enum NI_MAXSERV = 32;
@nogc
{
int WSAStartup(ushort wVersionRequested, LPWSADATA lpWSAData);
int WSACleanup();
SOCKET socket(int af, int type, int protocol);
@trusted int WSACleanup();
@trusted SOCKET socket(int af, int type, int protocol);
int ioctlsocket(SOCKET s, int cmd, uint* argp);
int bind(SOCKET s, const(sockaddr)* name, socklen_t namelen);
int connect(SOCKET s, const(sockaddr)* name, socklen_t namelen);
int listen(SOCKET s, int backlog);
@trusted int listen(SOCKET s, int backlog);
SOCKET accept(SOCKET s, sockaddr* addr, socklen_t* addrlen);
int closesocket(SOCKET s);
int shutdown(SOCKET s, int how);
@trusted int closesocket(SOCKET s);
@trusted int shutdown(SOCKET s, int how);
int getpeername(SOCKET s, sockaddr* name, socklen_t* namelen);
int getsockname(SOCKET s, sockaddr* name, socklen_t* namelen);
int send(SOCKET s, const(void)* buf, int len, int flags);
Expand All @@ -64,11 +64,11 @@ int getsockopt(SOCKET s, int level, int optname, void* optval, socklen_t* optlen
int setsockopt(SOCKET s, int level, int optname, const(void)* optval, socklen_t optlen);
uint inet_addr(const char* cp);
int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds, const(timeval)* timeout);
char* inet_ntoa(in_addr ina);
@trusted char* inet_ntoa(in_addr ina);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inet_ntoa cannot be @trusted.

https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-inet_ntoa#remarks says:

The string returned by inet_ntoa resides in memory that is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The string returned is guaranteed to be valid only until the next Windows Sockets function call is made within the same thread.

I.e., if you dereference the returned pointer after having called another Windows Sockets function, you've got undefined behavior. That's not safe.

hostent* gethostbyname(const char* name);
hostent* gethostbyaddr(const(void)* addr, int len, int type);
protoent* getprotobyname(const char* name);
protoent* getprotobynumber(int number);
@trusted protoent* getprotobynumber(int number);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getprotobynumber cannot be @trusted.

https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getprotobynumber#remarks says:

The pointer that is returned points to the structure allocated by Windows Sockets. The application must never attempt to modify this structure [...].

But the return value is mutable. So @safe code might do just that.

servent* getservbyname(const char* name, const char* proto);
servent* getservbyport(int port, const char* proto);
}
Expand Down