about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-08-16 01:52:15 +0000
committerEric Wong <normalperson@yhbt.net>2013-08-16 01:57:04 +0000
commit9f30805bc4ff65ad4ca0be9dfb26b1a9bdc70c51 (patch)
treec974cbd69173710607707545817b66883c5b650f
parent8be51237720fd18cb45188f29c717bbac0ca1964 (diff)
downloadkgio-9f30805bc4ff65ad4ca0be9dfb26b1a9bdc70c51.tar.gz
check syscall returns against < 0 instead of == -1
This may help us avoid errors in case of C library bugs,
and also results in smaller code:

$ ~/linux/scripts/bloat-o-meter before.so after.so
add/remove: 0/0 grow/shrink: 0/9 up/down: 0/-66 (-66)
function                                     old     new   delta
s_trywrite                                   160     159      -1
kgio_write                                   160     159      -1
kgio_trywrite                                160     159      -1
my_recv                                      616     610      -6
my_peek                                      616     610      -6
stream_connect                               456     448      -8
my_socket                                    222     213      -9
my_writev                                   1703    1687     -16
write_check                                  427     409     -18
-rw-r--r--ext/kgio/accept.c4
-rw-r--r--ext/kgio/connect.c8
-rw-r--r--ext/kgio/nonblock.h6
-rw-r--r--ext/kgio/read_write.c6
-rw-r--r--ext/kgio/tryopen.c4
5 files changed, 16 insertions, 12 deletions
diff --git a/ext/kgio/accept.c b/ext/kgio/accept.c
index 5f07532..503b5e9 100644
--- a/ext/kgio/accept.c
+++ b/ext/kgio/accept.c
@@ -68,7 +68,7 @@ static VALUE xaccept(void *ptr)
         int rv;
 
         rv = accept_fn(a->fd, a->addr, a->addrlen, a->flags);
-        if (rv == -1 && errno == ENOSYS && accept_fn != my_accept4) {
+        if (rv < 0 && errno == ENOSYS && accept_fn != my_accept4) {
                 accept_fn = my_accept4;
                 rv = accept_fn(a->fd, a->addr, a->addrlen, a->flags);
         }
@@ -170,7 +170,7 @@ my_accept(struct accept_args *a, int force_nonblock)
 
 retry:
         client_fd = thread_accept(a, force_nonblock);
-        if (client_fd == -1) {
+        if (client_fd < 0) {
                 switch (errno) {
                 case EAGAIN:
                         if (force_nonblock)
diff --git a/ext/kgio/connect.c b/ext/kgio/connect.c
index 63c0bbc..2027795 100644
--- a/ext/kgio/connect.c
+++ b/ext/kgio/connect.c
@@ -36,7 +36,7 @@ static int my_socket(int domain)
 retry:
         fd = socket(domain, MY_SOCK_STREAM, 0);
 
-        if (fd == -1) {
+        if (fd < 0) {
                 switch (errno) {
                 case EMFILE:
                 case ENFILE:
@@ -53,12 +53,12 @@ retry:
                                 goto retry;
                         }
                 }
-                if (fd == -1)
+                if (fd < 0)
                         rb_sys_fail("socket");
         }
 
         if (MY_SOCK_STREAM == SOCK_STREAM) {
-                if (fcntl(fd, F_SETFL, O_RDWR | O_NONBLOCK) == -1)
+                if (fcntl(fd, F_SETFL, O_RDWR | O_NONBLOCK) < 0)
                         close_fail(fd, "fcntl(F_SETFL, O_RDWR | O_NONBLOCK)");
                 rb_fd_fix_cloexec(fd);
         }
@@ -71,7 +71,7 @@ my_connect(VALUE klass, int io_wait, int domain, void *addr, socklen_t addrlen)
 {
         int fd = my_socket(domain);
 
-        if (connect(fd, addr, addrlen) == -1) {
+        if (connect(fd, addr, addrlen) < 0) {
                 if (errno == EINPROGRESS) {
                         VALUE io = sock_for_fd(klass, fd);
 
diff --git a/ext/kgio/nonblock.h b/ext/kgio/nonblock.h
index 43de8cd..2d9a13a 100644
--- a/ext/kgio/nonblock.h
+++ b/ext/kgio/nonblock.h
@@ -5,11 +5,15 @@ static void set_nonblocking(int fd)
 {
         int flags = fcntl(fd, F_GETFL);
 
+        /*
+         * do not check < 0 here, one day we may have enough FD flags
+         * to require negative bit
+         */
         if (flags == -1)
                 rb_sys_fail("fcntl(F_GETFL)");
         if ((flags & O_NONBLOCK) == O_NONBLOCK)
                 return;
         flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
-        if (flags == -1)
+        if (flags < 0)
                 rb_sys_fail("fcntl(F_SETFL)");
 }
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index 4ce3541..f4d658c 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -109,7 +109,7 @@ static void prepare_read(struct io_args *a, int argc, VALUE *argv, VALUE io)
 
 static int read_check(struct io_args *a, long n, const char *msg, int io_wait)
 {
-        if (n == -1) {
+        if (n < 0) {
                 if (errno == EINTR) {
                         a->fd = my_fileno(a->io);
                         return -1;
@@ -344,7 +344,7 @@ static int write_check(struct io_args *a, long n, const char *msg, int io_wait)
         if (a->len == n) {
 done:
                 a->buf = Qnil;
-        } else if (n == -1) {
+        } else if (n < 0) {
                 if (errno == EINTR) {
                         a->fd = my_fileno(a->io);
                         return -1;
@@ -589,7 +589,7 @@ static int writev_check(struct io_args_v *a, long n, const char *msg, int io_wai
         if (n >= 0) {
                 if (n > 0) a->something_written = 1;
                 return trim_writev_buffer(a, n);
-        } else if (n == -1) {
+        } else if (n < 0) {
                 if (errno == EINTR) {
                         a->fd = my_fileno(a->io);
                         return -1;
diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c
index 0ee6a54..5fa6ada 100644
--- a/ext/kgio/tryopen.c
+++ b/ext/kgio/tryopen.c
@@ -106,7 +106,7 @@ static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
 
 retry:
         fd = (int)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
-        if (fd == -1) {
+        if (fd < 0) {
                 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
                         rb_gc();
                         if (retried)
@@ -114,7 +114,7 @@ retry:
                         retried = 1;
                         goto retry;
                 }
-                if (fd == -1) {
+                if (fd < 0) {
                         int saved_errno = errno;
 
                         if (!st_lookup(errno2sym, (st_data_t)errno, &rv)) {