Skip to content

Commit

Permalink
Verify return code from fcntl calls.
Browse files Browse the repository at this point in the history
Have connectnonblocking() warn and fail if setting O_NONBLOCK fails.
Have it warn if restoring of flags fail.

coverity: 1449515
  • Loading branch information
Linus Nordberg committed Aug 1, 2017
1 parent 011523e commit d6f4fab
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, st
socklen_t len;

origflags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, origflags | O_NONBLOCK);
if (origflags == -1) {
debugerrno(errno, DBG_WARN, "Failed to get flags");
return -1;
}
if (fcntl(s, F_SETFL, origflags | O_NONBLOCK) == -1) {
debugerrno(errno, DBG_WARN, "Failed to set O_NONBLOCK");
return -1;
}
if (!connect(s, addr, addrlen)) {
r = 0;
goto exit;
Expand All @@ -189,7 +196,8 @@ int connectnonblocking(int s, const struct sockaddr *addr, socklen_t addrlen, st
r = 0;

exit:
fcntl(s, F_SETFL, origflags);
if (fcntl(s, F_SETFL, origflags) == -1)
debugerrno(errno, DBG_WARN, "Failed to set original flags back");
return r;
}

Expand Down

0 comments on commit d6f4fab

Please sign in to comment.