mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-27 21:00:50 +00:00
Bugzilla bug #17728: pt_writev_cont, pt_write_cont, pt_send_cont, and
pt_sendto_cont need to deal with the possibility of a zero-length buffer. r=srinivas@netscape.com. Added a test zerolen.c for this bug. Modified files: ptio.c, pr/tests/Makefile, pr/tests/Makefile.in, pr/tests/runtests.ksh. Added file: pr/tests/zerolen.c
This commit is contained in:
parent
81513dba8a
commit
0c9675a0c6
@ -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;
|
||||
|
@ -162,6 +162,7 @@ CSRCS = \
|
||||
xnotify.c \
|
||||
y2k.c \
|
||||
y2ktmo.c \
|
||||
zerolen.c \
|
||||
$(NULL)
|
||||
|
||||
ifeq ($(OS_TARGET),OS2)
|
||||
|
@ -165,6 +165,7 @@ CSRCS = \
|
||||
xnotify.c \
|
||||
y2k.c \
|
||||
y2ktmo.c \
|
||||
zerolen.c \
|
||||
$(NULL)
|
||||
|
||||
ifeq ($(OS_TARGET),OS2)
|
||||
|
@ -164,7 +164,8 @@ udpsrv
|
||||
vercheck
|
||||
version
|
||||
writev
|
||||
xnotify"
|
||||
xnotify
|
||||
zerolen"
|
||||
|
||||
rval=0
|
||||
|
||||
|
263
nsprpub/pr/tests/zerolen.c
Normal file
263
nsprpub/pr/tests/zerolen.c
Normal file
@ -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 <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else /* XP_UNIX */
|
||||
|
||||
#include "nspr.h"
|
||||
#include "private/pprio.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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 */
|
Loading…
x
Reference in New Issue
Block a user