fix warnings reported by Coverity

fd.c:230 negative_returns: fd is passed to a parameter that cannot be negative.
net.c:65 check_return: Calling setsockopt(fd, 1, 2, (char *)&n, 4U) without checking return value.
net.c:77 check_return: Calling fdnoblock(fd) without checking return value.
net.c:112 check_return: Calling fdnoblock(cfd) without checking return value.
net.c:114 check_return: Calling setsockopt(cfd, IPPROTO_TCP, 1, (char *)&one, 4U) without checking return value.
net.c:144 leaked_storage: Variable result going out of scope leaks the storage it points to.
net.c:168 check_return: Calling fdnoblock(fd) without checking return value.
net.c:173 check_return: Calling setsockopt(fd, 1, 6, &n, 4U) without checking return value.
net.c:206 check_return: Calling getsockopt(fd, 1, 4, (void *)&n, &sn) without checking return value.
task.c:59 negative_returns: fd is passed to a parameter that cannot be negative.
tcpload.c:50 Calling fdread without checking return value (as is done elsewhere 4 out of 5 times).
tcpproxy.c:51 check_return: Calling fdnoblock(fd) without checking return value.
This commit is contained in:
David du Colombier 2015-06-24 23:16:58 +02:00
parent 728d616727
commit 798c8c6df8
6 changed files with 42 additions and 11 deletions

5
echo.c
View File

@ -48,7 +48,10 @@ taskmain(int argc, char **argv)
fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno));
taskexitall(1); taskexitall(1);
} }
fdnoblock(fd); if(fdnoblock(fd) < 0){
fprintf(stderr, "fdnoblock\n");
taskexitall(1);
}
while((cfd = netaccept(fd, remote, &rport)) >= 0){ while((cfd = netaccept(fd, remote, &rport)) >= 0){
if(verbose) if(verbose)
fprintf(stderr, "connection from %s:%d\n", remote, rport); fprintf(stderr, "connection from %s:%d\n", remote, rport);

2
fd.c
View File

@ -220,6 +220,8 @@ fdwait(int fd, int rw)
if(r < 0 || errno == EEXIST){ if(r < 0 || errno == EEXIST){
duped = 1; duped = 1;
fd = dup(fd); fd = dup(fd);
if(fd < 0)
abort();
r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if(r != 0) if(r != 0)
abort(); abort();

37
net.c
View File

@ -62,7 +62,10 @@ netannounce(int istcp, char *server, int port)
sn = sizeof n; sn = sizeof n;
if(istcp && getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0){ if(istcp && getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0){
n = 1; n = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n); if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&n, sizeof n) < 0){
close(fd);
return -1;
}
} }
if(bind(fd, (struct sockaddr*)&ss, sizeof ss) < 0){ if(bind(fd, (struct sockaddr*)&ss, sizeof ss) < 0){
@ -74,7 +77,10 @@ netannounce(int istcp, char *server, int port)
if(proto == SOCK_STREAM) if(proto == SOCK_STREAM)
listen(fd, 16); listen(fd, 16);
fdnoblock(fd); if(fdnoblock(fd) < 0){
close(fd);
return -1;
}
taskstate("netannounce succeeded"); taskstate("netannounce succeeded");
return fd; return fd;
} }
@ -109,9 +115,15 @@ netaccept(int fd, char *server, int *port)
*port = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port); *port = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port);
break; break;
} }
fdnoblock(cfd); if(fdnoblock(cfd) < 0){
close(cfd);
return -1;
}
one = 1; one = 1;
setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one); if(setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof one) < 0){
close(cfd);
return -1;
}
taskstate("netaccept succeeded"); taskstate("netaccept succeeded");
return cfd; return cfd;
} }
@ -136,10 +148,12 @@ netlookup(char *name, unsigned char *ip)
break; break;
} }
taskstate("netlookup succeeded"); taskstate("netlookup succeeded");
freeaddrinfo(result);
return 0; return 0;
} }
taskstate("netlookup failed"); taskstate("netlookup failed");
freeaddrinfo(result);
return -1; return -1;
} }
@ -160,12 +174,18 @@ netdial(int istcp, char *server, int port)
taskstate("socket failed"); taskstate("socket failed");
return -1; return -1;
} }
fdnoblock(fd); if(fdnoblock(fd) < 0){
close(fd);
return -1;
}
/* for udp */ /* for udp */
if(!istcp){ if(!istcp){
n = 1; n = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof n); if(setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof n) < 0){
close(fd);
return -1;
}
} }
/* start connecting */ /* start connecting */
@ -198,7 +218,10 @@ netdial(int istcp, char *server, int port)
/* report error */ /* report error */
sn = sizeof n; sn = sizeof n;
getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &sn); if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&n, &sn) < 0){
close(fd);
return -1;
}
if(n == 0) if(n == 0)
n = ECONNREFUSED; n = ECONNREFUSED;
close(fd); close(fd);

3
task.c
View File

@ -46,7 +46,8 @@ taskdebug(char *fmt, ...)
p = argv0; p = argv0;
snprint(buf, sizeof buf, "/tmp/%s.tlog", p); snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
if((fd = open(buf, O_CREAT|O_WRONLY, 0666)) < 0) if((fd = open(buf, O_CREAT|O_WRONLY, 0666)) < 0)
fd = open("/dev/null", O_WRONLY); if((fd = open("/dev/null", O_WRONLY)) < 0)
abort();
} }
va_start(arg, fmt); va_start(arg, fmt);

View File

@ -47,7 +47,6 @@ fetchtask(void *v)
} }
snprintf(buf, sizeof buf, "xxxxxxxxxx"); snprintf(buf, sizeof buf, "xxxxxxxxxx");
fdwrite(fd, buf, strlen(buf)); fdwrite(fd, buf, strlen(buf));
fdread(fd, buf, sizeof buf);
close(fd); close(fd);
} }
} }

View File

@ -51,7 +51,10 @@ taskmain(int argc, char **argv)
fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno));
taskexitall(1); taskexitall(1);
} }
fdnoblock(fd); if(fdnoblock(fd) < 0){
fprintf(stderr, "fdnoblock\n");
taskexitall(1);
}
while((cfd = netaccept(fd, remote, &rport)) >= 0){ while((cfd = netaccept(fd, remote, &rport)) >= 0){
if(verbose) if(verbose)
fprintf(stderr, "connection from %s:%d\n", remote, rport); fprintf(stderr, "connection from %s:%d\n", remote, rport);