Skip to content

Commit 4b32898

Browse files
committed
CDRIVER-843 & CDRIVER-756: Incorrect error checking
Use int for WSASend() and debug both return value and sent bytes Also check the return value for SOCKET_ERROR, not !0
1 parent 1b58443 commit 4b32898

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/mongoc/mongoc-socket.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ int
238238
mongoc_socket_errno (mongoc_socket_t *sock) /* IN */
239239
{
240240
BSON_ASSERT (sock);
241+
TRACE("Current errno: %d", sock->errno_);
241242
return sock->errno_;
242243
}
243244

@@ -900,10 +901,11 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
900901
{
901902
#ifdef _WIN32
902903
DWORD dwNumberofBytesSent = 0;
904+
int ret;
903905
#else
904906
struct msghdr msg;
905-
#endif
906907
ssize_t ret = -1;
908+
#endif
907909

908910
ENTRY;
909911

@@ -916,8 +918,7 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
916918
#ifdef _WIN32
917919
ret = WSASend (sock->sd, (LPWSABUF)iov, iovcnt, &dwNumberofBytesSent,
918920
0, NULL, NULL);
919-
ret = ret ? -1 : dwNumberofBytesSent;
920-
errno = WSAGetLastError();
921+
TRACE("WSASend sent: %ld (out of: %ld), ret: %d", dwNumberofBytesSent, iov->iov_len, ret);
921922
#else
922923
memset (&msg, 0, sizeof msg);
923924
msg.msg_iov = iov;
@@ -928,11 +929,15 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
928929
# else
929930
0);
930931
# endif
932+
TRACE("Send %ld out of %ld bytes", ret, iov->iov_len);
931933
#endif
932934

933-
TRACE("Send %ld out of %ld bytes", ret, iov->iov_len);
934935

936+
#ifdef _WIN32
937+
if (ret == SOCKET_ERROR) {
938+
#else
935939
if (ret == -1) {
940+
#endif
936941
_mongoc_socket_capture_errno (sock);
937942

938943
/*
@@ -941,15 +946,21 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */
941946
* send() commands.
942947
*/
943948
#ifdef _WIN32
944-
if (errno == WSAEMSGSIZE) {
949+
if (mongoc_socket_errno (sock) == WSAEMSGSIZE) {
945950
#else
946-
if (errno == EMSGSIZE) {
951+
if (mongoc_socket_errno (sock) == EMSGSIZE) {
947952
#endif
948953
RETURN(_mongoc_socket_try_sendv_slow (sock, iov, iovcnt));
949954
}
955+
956+
RETURN (-1);
950957
}
951958

959+
#ifdef _WIN32
960+
RETURN (dwNumberofBytesSent);
961+
#else
952962
RETURN (ret);
963+
#endif
953964
}
954965

955966

0 commit comments

Comments
 (0)