From 798c8c6df8b80e59f7a8a2e30e1408b18dfc3af5 Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Wed, 24 Jun 2015 23:16:58 +0200 Subject: [PATCH] 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. --- echo.c | 5 ++++- fd.c | 2 ++ net.c | 37 ++++++++++++++++++++++++++++++------- task.c | 3 ++- tcpload.c | 1 - tcpproxy.c | 5 ++++- 6 files changed, 42 insertions(+), 11 deletions(-) diff --git a/echo.c b/echo.c index 168dec7..963d225 100644 --- a/echo.c +++ b/echo.c @@ -48,7 +48,10 @@ taskmain(int argc, char **argv) fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); taskexitall(1); } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + fprintf(stderr, "fdnoblock\n"); + taskexitall(1); + } while((cfd = netaccept(fd, remote, &rport)) >= 0){ if(verbose) fprintf(stderr, "connection from %s:%d\n", remote, rport); diff --git a/fd.c b/fd.c index 5b4f579..4e0fdcc 100644 --- a/fd.c +++ b/fd.c @@ -220,6 +220,8 @@ fdwait(int fd, int rw) if(r < 0 || errno == EEXIST){ duped = 1; fd = dup(fd); + if(fd < 0) + abort(); r = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); if(r != 0) abort(); diff --git a/net.c b/net.c index e635518..f3ec128 100644 --- a/net.c +++ b/net.c @@ -62,7 +62,10 @@ netannounce(int istcp, char *server, int port) sn = sizeof n; if(istcp && getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&n, &sn) >= 0){ 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){ @@ -74,7 +77,10 @@ netannounce(int istcp, char *server, int port) if(proto == SOCK_STREAM) listen(fd, 16); - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + close(fd); + return -1; + } taskstate("netannounce succeeded"); return fd; } @@ -109,9 +115,15 @@ netaccept(int fd, char *server, int *port) *port = nhgets(&((struct sockaddr_in6*)&ss)->sin6_port); break; } - fdnoblock(cfd); + if(fdnoblock(cfd) < 0){ + close(cfd); + return -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"); return cfd; } @@ -136,10 +148,12 @@ netlookup(char *name, unsigned char *ip) break; } taskstate("netlookup succeeded"); + freeaddrinfo(result); return 0; } taskstate("netlookup failed"); + freeaddrinfo(result); return -1; } @@ -160,12 +174,18 @@ netdial(int istcp, char *server, int port) taskstate("socket failed"); return -1; } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + close(fd); + return -1; + } /* for udp */ if(!istcp){ 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 */ @@ -198,7 +218,10 @@ netdial(int istcp, char *server, int port) /* report error */ 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) n = ECONNREFUSED; close(fd); diff --git a/task.c b/task.c index 074b6bf..6360d31 100644 --- a/task.c +++ b/task.c @@ -46,7 +46,8 @@ taskdebug(char *fmt, ...) p = argv0; snprint(buf, sizeof buf, "/tmp/%s.tlog", p); 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); diff --git a/tcpload.c b/tcpload.c index 9f6dfc8..d507a88 100644 --- a/tcpload.c +++ b/tcpload.c @@ -47,7 +47,6 @@ fetchtask(void *v) } snprintf(buf, sizeof buf, "xxxxxxxxxx"); fdwrite(fd, buf, strlen(buf)); - fdread(fd, buf, sizeof buf); close(fd); } } diff --git a/tcpproxy.c b/tcpproxy.c index 349d6c2..5098ae3 100644 --- a/tcpproxy.c +++ b/tcpproxy.c @@ -51,7 +51,10 @@ taskmain(int argc, char **argv) fprintf(stderr, "cannot announce on tcp port %d: %s\n", atoi(argv[1]), strerror(errno)); taskexitall(1); } - fdnoblock(fd); + if(fdnoblock(fd) < 0){ + fprintf(stderr, "fdnoblock\n"); + taskexitall(1); + } while((cfd = netaccept(fd, remote, &rport)) >= 0){ if(verbose) fprintf(stderr, "connection from %s:%d\n", remote, rport);