diff --git a/mozilla/nsprpub/pr/src/pthreads/ptio.c b/mozilla/nsprpub/pr/src/pthreads/ptio.c index f0aee649841..a1f4695e30b 100644 --- a/mozilla/nsprpub/pr/src/pthreads/ptio.c +++ b/mozilla/nsprpub/pr/src/pthreads/ptio.c @@ -1064,7 +1064,7 @@ static PRBool pt_send_cont(pt_Continuation *op, PRInt16 revents) op->arg1.osfd, op->arg2.buffer, op->arg3.amount, op->arg4.flags); #endif op->syserrno = errno; - if (bytes > 0) /* this is progress */ + if (bytes >= 0) /* this is progress */ { char *bp = (char*)op->arg2.buffer; bp += bytes; /* adjust the buffer pointer */ @@ -1073,8 +1073,7 @@ static PRBool pt_send_cont(pt_Continuation *op, PRInt16 revents) op->arg3.amount -= bytes; /* and reduce the required count */ return (0 == op->arg3.amount) ? PR_TRUE : PR_FALSE; } - else if ((-1 == bytes) && (EWOULDBLOCK != op->syserrno) - && (EAGAIN != op->syserrno)) + else if ((EWOULDBLOCK != op->syserrno) && (EAGAIN != op->syserrno)) { op->result.code = -1; return PR_TRUE; @@ -1094,7 +1093,7 @@ static PRBool pt_write_cont(pt_Continuation *op, PRInt16 revents) */ bytes = write(op->arg1.osfd, op->arg2.buffer, op->arg3.amount); op->syserrno = errno; - if (bytes > 0) /* this is progress */ + if (bytes >= 0) /* this is progress */ { char *bp = (char*)op->arg2.buffer; bp += bytes; /* adjust the buffer pointer */ @@ -1103,8 +1102,7 @@ static PRBool pt_write_cont(pt_Continuation *op, PRInt16 revents) op->arg3.amount -= bytes; /* and reduce the required count */ return (0 == op->arg3.amount) ? PR_TRUE : PR_FALSE; } - else if ((-1 == bytes) && (EWOULDBLOCK != op->syserrno) - && (EAGAIN != op->syserrno)) + else if ((EWOULDBLOCK != op->syserrno) && (EAGAIN != op->syserrno)) { op->result.code = -1; return PR_TRUE; @@ -1125,7 +1123,7 @@ static PRBool pt_writev_cont(pt_Continuation *op, PRInt16 revents) */ bytes = writev(op->arg1.osfd, iov, op->arg3.amount); op->syserrno = errno; - if (bytes > 0) /* this is progress */ + if (bytes >= 0) /* this is progress */ { PRIntn iov_index; op->result.code += bytes; /* accumulate the number sent */ @@ -1146,8 +1144,7 @@ static PRBool pt_writev_cont(pt_Continuation *op, PRInt16 revents) op->arg3.amount -= iov_index; /* and array length */ return (0 == op->arg3.amount) ? PR_TRUE : PR_FALSE; } - else if ((-1 == bytes) && (EWOULDBLOCK != op->syserrno) - && (EAGAIN != op->syserrno)) + else if ((EWOULDBLOCK != op->syserrno) && (EAGAIN != op->syserrno)) { op->result.code = -1; return PR_TRUE; @@ -1161,7 +1158,7 @@ static PRBool pt_sendto_cont(pt_Continuation *op, PRInt16 revents) op->arg1.osfd, op->arg2.buffer, op->arg3.amount, op->arg4.flags, (struct sockaddr*)op->arg5.addr, PR_NETADDR_SIZE(op->arg5.addr)); op->syserrno = errno; - if (bytes > 0) /* this is progress */ + if (bytes >= 0) /* this is progress */ { char *bp = (char*)op->arg2.buffer; bp += bytes; /* adjust the buffer pointer */ @@ -1170,8 +1167,7 @@ static PRBool pt_sendto_cont(pt_Continuation *op, PRInt16 revents) op->arg3.amount -= bytes; /* and reduce the required count */ return (0 == op->arg3.amount) ? PR_TRUE : PR_FALSE; } - else if ((-1 == bytes) && (EWOULDBLOCK != op->syserrno) - && (EAGAIN != op->syserrno)) + else if ((EWOULDBLOCK != op->syserrno) && (EAGAIN != op->syserrno)) { op->result.code = -1; return PR_TRUE; diff --git a/mozilla/nsprpub/pr/tests/Makefile b/mozilla/nsprpub/pr/tests/Makefile index 7bc2d367f61..a194de5cc05 100644 --- a/mozilla/nsprpub/pr/tests/Makefile +++ b/mozilla/nsprpub/pr/tests/Makefile @@ -162,6 +162,7 @@ CSRCS = \ xnotify.c \ y2k.c \ y2ktmo.c \ + zerolen.c \ $(NULL) ifeq ($(OS_TARGET),OS2) diff --git a/mozilla/nsprpub/pr/tests/Makefile.in b/mozilla/nsprpub/pr/tests/Makefile.in index f36f2f253ae..8496ed62af2 100644 --- a/mozilla/nsprpub/pr/tests/Makefile.in +++ b/mozilla/nsprpub/pr/tests/Makefile.in @@ -165,6 +165,7 @@ CSRCS = \ xnotify.c \ y2k.c \ y2ktmo.c \ + zerolen.c \ $(NULL) ifeq ($(OS_TARGET),OS2) diff --git a/mozilla/nsprpub/pr/tests/runtests.ksh b/mozilla/nsprpub/pr/tests/runtests.ksh index 28759a12f6f..50817e9c962 100755 --- a/mozilla/nsprpub/pr/tests/runtests.ksh +++ b/mozilla/nsprpub/pr/tests/runtests.ksh @@ -164,7 +164,8 @@ udpsrv vercheck version writev -xnotify" +xnotify +zerolen" rval=0 diff --git a/mozilla/nsprpub/pr/tests/zerolen.c b/mozilla/nsprpub/pr/tests/zerolen.c new file mode 100644 index 00000000000..0192f6beba5 --- /dev/null +++ b/mozilla/nsprpub/pr/tests/zerolen.c @@ -0,0 +1,263 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * The contents of this file are subject to the Netscape Public License + * Version 1.1 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1999 Netscape Communications Corporation. All Rights + * Reserved. + */ + +/* + * Test: zerolen.c + * + * Description: a test for Bugzilla bug #17699. We perform + * the same test for PR_Writev, PR_Write, and PR_Send. In + * each test the server thread first fills up the connection + * to the client so that the next write operation will fail + * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send + * with a zero-length buffer. The client thread initially + * does not read so that the connection can be filled up. + * Then it empties the connection so that the server thread's + * PR_Writev, PR_Write, or PR_Send call can succeed. + * + * Bug #17699 is specific to the pthreads version on Unix, + * so on other platforms this test does nothing. + */ + +#ifndef XP_UNIX + +#include + +int main() +{ + printf("PASS\n"); + return 0; +} + +#else /* XP_UNIX */ + +#include "nspr.h" +#include "private/pprio.h" + +#include +#include +#include +#include +#include + +static void ClientThread(void *arg) +{ + PRFileDesc *sock; + PRNetAddr addr; + PRUint16 port = (PRUint16) arg; + char buf[1024]; + PRInt32 nbytes; + + sock = PR_NewTCPSocket(); + if (NULL == sock) { + fprintf(stderr, "PR_NewTCPSocket failed\n"); + exit(1); + } + if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) { + fprintf(stderr, "PR_InitializeNetAddr failed\n"); + exit(1); + } + if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) { + fprintf(stderr, "PR_Connect failed\n"); + exit(1); + } + /* + * Sleep 5 seconds to force the server thread to get EAGAIN. + */ + if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) { + fprintf(stderr, "PR_Sleep failed\n"); + exit(1); + } + /* + * Then start reading. + */ + while ((nbytes = PR_Read(sock, buf, sizeof(buf))) > 0) { + /* empty loop body */ + } + if (-1 == nbytes) { + fprintf(stderr, "PR_Read failed\n"); + exit(1); + } + if (PR_Close(sock) == PR_FAILURE) { + fprintf(stderr, "PR_Close failed\n"); + exit(1); + } +} + +int main() +{ + PRFileDesc *listenSock; + PRFileDesc *acceptSock; + int osfd; + PRThread *clientThread; + PRNetAddr addr; + char buf[1024]; + PRInt32 nbytes; + PRIOVec iov; + + memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */ + listenSock = PR_NewTCPSocket(); + if (NULL == listenSock) { + fprintf(stderr, "PR_NewTCPSocket failed\n"); + exit(1); + } + if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) { + fprintf(stderr, "PR_InitializeNetAddr failed\n"); + exit(1); + } + if (PR_Bind(listenSock, &addr) == PR_FAILURE) { + fprintf(stderr, "PR_Bind failed\n"); + exit(1); + } + /* Find out what port number we are bound to. */ + if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) { + fprintf(stderr, "PR_GetSockName failed\n"); + exit(1); + } + if (PR_Listen(listenSock, 5) == PR_FAILURE) { + fprintf(stderr, "PR_Listen failed\n"); + exit(1); + } + + /* + * First test PR_Writev. + */ + clientThread = PR_CreateThread(PR_USER_THREAD, + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); + if (NULL == clientThread) { + fprintf(stderr, "PR_CreateThread failed\n"); + exit(1); + } + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); + if (NULL == acceptSock) { + fprintf(stderr, "PR_Accept failed\n"); + exit(1); + } + osfd = PR_FileDesc2NativeHandle(acceptSock); + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { + /* empty loop body */ + } + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { + fprintf(stderr, "write failed\n"); + exit(1); + } + iov.iov_base = buf; + iov.iov_len = 0; + printf("calling PR_Writev with a zero-length buffer\n"); + fflush(stdout); + nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT); + if (nbytes != 0) { + fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes); + exit(1); + } + if (PR_Close(acceptSock) == PR_FAILURE) { + fprintf(stderr, "PR_Close failed\n"); + exit(1); + } + if (PR_JoinThread(clientThread) == PR_FAILURE) { + fprintf(stderr, "PR_JoinThread failed\n"); + exit(1); + } + + /* + * Then test PR_Write. + */ + clientThread = PR_CreateThread(PR_USER_THREAD, + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); + if (NULL == clientThread) { + fprintf(stderr, "PR_CreateThread failed\n"); + exit(1); + } + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); + if (NULL == acceptSock) { + fprintf(stderr, "PR_Accept failed\n"); + exit(1); + } + osfd = PR_FileDesc2NativeHandle(acceptSock); + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { + /* empty loop body */ + } + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { + fprintf(stderr, "write failed\n"); + exit(1); + } + printf("calling PR_Write with a zero-length buffer\n"); + fflush(stdout); + nbytes = PR_Write(acceptSock, buf, 0); + if (nbytes != 0) { + fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes); + exit(1); + } + if (PR_Close(acceptSock) == PR_FAILURE) { + fprintf(stderr, "PR_Close failed\n"); + exit(1); + } + if (PR_JoinThread(clientThread) == PR_FAILURE) { + fprintf(stderr, "PR_JoinThread failed\n"); + exit(1); + } + + /* + * Finally test PR_Send. + */ + clientThread = PR_CreateThread(PR_USER_THREAD, + ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)), + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); + if (NULL == clientThread) { + fprintf(stderr, "PR_CreateThread failed\n"); + exit(1); + } + acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT); + if (NULL == acceptSock) { + fprintf(stderr, "PR_Accept failed\n"); + exit(1); + } + osfd = PR_FileDesc2NativeHandle(acceptSock); + while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) { + /* empty loop body */ + } + if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) { + fprintf(stderr, "write failed\n"); + exit(1); + } + printf("calling PR_Send with a zero-length buffer\n"); + fflush(stdout); + nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT); + if (nbytes != 0) { + fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes); + exit(1); + } + if (PR_Close(acceptSock) == PR_FAILURE) { + fprintf(stderr, "PR_Close failed\n"); + exit(1); + } + if (PR_JoinThread(clientThread) == PR_FAILURE) { + fprintf(stderr, "PR_JoinThread failed\n"); + exit(1); + } + + if (PR_Close(listenSock) == PR_FAILURE) { + fprintf(stderr, "PR_Close failed\n"); + exit(1); + } + printf("PASS\n"); + return 0; +} + +#endif /* XP_UNIX */