From f9d00fb58e8e9fed24751ad26dbdbc394f30290e Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Tue, 19 May 2009 23:31:55 +0200 Subject: Simplify the logic of tsocket_bsd_pending Remove two indentation levels by returning early on error. Metze, please check! --- lib/tsocket/tsocket_bsd.c | 50 ++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/tsocket/tsocket_bsd.c b/lib/tsocket/tsocket_bsd.c index 0ec47f8466..054bb3de3a 100644 --- a/lib/tsocket/tsocket_bsd.c +++ b/lib/tsocket/tsocket_bsd.c @@ -149,40 +149,42 @@ static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd) static ssize_t tsocket_bsd_pending(int fd) { - int ret; + int ret, error; int value = 0; + socklen_t len; ret = ioctl(fd, FIONREAD, &value); if (ret == -1) { return ret; } - if (ret == 0) { - if (value == 0) { - int error=0; - socklen_t len = sizeof(error); - /* - * if no data is available check if the socket - * is in error state. For dgram sockets - * it's the way to return ICMP error messages - * of connected sockets to the caller. - */ - ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, - &error, &len); - if (ret == -1) { - return ret; - } - if (error != 0) { - errno = error; - return -1; - } - } + if (ret != 0) { + /* this should not be reached */ + errno = EIO; + return -1; + } + + if (value != 0) { return value; } - /* this should not be reached */ - errno = EIO; - return -1; + error = 0; + len = sizeof(error); + + /* + * if no data is available check if the socket is in error state. For + * dgram sockets it's the way to return ICMP error messages of + * connected sockets to the caller. + */ + ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len); + if (ret == -1) { + return ret; + } + if (error != 0) { + errno = error; + return -1; + } + return 0; } static const struct tsocket_address_ops tsocket_address_bsd_ops; -- cgit