Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange logic in crypto/compat/posix_win.c #607

Closed
jimpark opened this issue Jul 23, 2020 · 2 comments
Closed

Strange logic in crypto/compat/posix_win.c #607

jimpark opened this issue Jul 23, 2020 · 2 comments
Labels

Comments

@jimpark
Copy link

jimpark commented Jul 23, 2020

In posix_close(), posix_read() and posix_close(), when the socket operation returns an error, we need to distinguish whether it was really a file operation or a true error. Why does WSANOTINITIALISED fall into calling the file function? If the fd was a socket and winsock was not initialized, that should just call wsa_errno(). I think WSAEBADF is only returned if the socket handle is bad. But if it's a file handle, it returns WSAENOTSOCK. So I think the only time we can safely call file operations is if WSAENOTSOCK is returned.

int
posix_close(int fd)
{
	if (closesocket(fd) == SOCKET_ERROR) {
		int err = WSAGetLastError();
		return err == WSAENOTSOCK ?
			close(fd) : wsa_errno(err);
	}
	return 0;
}

ssize_t
posix_read(int fd, void *buf, size_t count)
{
	ssize_t rc = recv(fd, buf, count, 0);
	if (rc == SOCKET_ERROR) {
		int err = WSAGetLastError();
		return err == WSAENOTSOCK ?
			read(fd, buf, count) : wsa_errno(err);
	}
	return rc;
}

ssize_t
posix_write(int fd, const void *buf, size_t count)
{
	ssize_t rc = send(fd, buf, count, 0);
	if (rc == SOCKET_ERROR) {
		int err = WSAGetLastError();
		return err == WSAENOTSOCK ?
			write(fd, buf, count) : wsa_errno(err);
	}
	return rc;
}
@kinichiro
Copy link
Contributor

Past discussion around posix_close is here #266

@busterb
Copy link
Contributor

busterb commented Jul 7, 2023

See #883 this should be fixed now.

@busterb busterb closed this as completed Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants