[CLEANUP] add a few checks for functions return values
Markus Elfring suggested adding a few checks which were missing
after a bunch of getsockopt() and 2 strdup(). While those are
unlikely to fail where they are used, it makes the code cleaner.
diff --git a/src/checks.c b/src/checks.c
index 3085eab..9643a2b 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -176,9 +176,7 @@
socklen_t lskerr = sizeof(skerr);
result = len = -1;
-
- getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
- if (!skerr) {
+ if (!getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) && !skerr) {
#ifndef MSG_NOSIGNAL
len = recv(fd, reply, sizeof(reply), 0);
#else
diff --git a/src/standard.c b/src/standard.c
index 64357a2..a32267b 100644
--- a/src/standard.c
+++ b/src/standard.c
@@ -96,6 +96,8 @@
memset(&sa, 0, sizeof(sa));
str = strdup(str);
+ if (str == NULL)
+ goto out_nofree;
if ((c = strrchr(str,':')) != NULL) {
*c++ = '\0';
@@ -120,6 +122,7 @@
sa.sin_family = AF_INET;
free(str);
+ out_nofree:
return &sa;
}
@@ -137,6 +140,8 @@
memset(mask, 0, sizeof(*mask));
memset(addr, 0, sizeof(*addr));
str = strdup(str);
+ if (str == NULL)
+ return 0;
if ((c = strrchr(str, '/')) != NULL) {
*c++ = '\0';
diff --git a/src/stream_sock.c b/src/stream_sock.c
index f0f80d1..2f91b0f 100644
--- a/src/stream_sock.c
+++ b/src/stream_sock.c
@@ -86,8 +86,8 @@
int skerr;
socklen_t lskerr = sizeof(skerr);
- getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
- if (skerr)
+ ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
+ if (ret == -1 || skerr)
ret = -1;
else
ret = recv(fd, b->r, max, 0);
@@ -171,8 +171,8 @@
if (fdtab[fd].state == FD_STCONN) {
int skerr;
socklen_t lskerr = sizeof(skerr);
- getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
- if (skerr) {
+ ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
+ if (ret == -1 || skerr) {
b->flags |= BF_WRITE_ERROR;
fdtab[fd].state = FD_STERROR;
task_wakeup(&rq, fdtab[fd].owner);
@@ -195,8 +195,8 @@
int skerr;
socklen_t lskerr = sizeof(skerr);
- getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
- if (skerr)
+ ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
+ if (ret == -1 || skerr)
ret = -1;
else
ret = send(fd, b->w, max, MSG_DONTWAIT);