]> git.meshlink.io Git - utcp/commitdiff
Use '== -1' to check for errors from functions.
authorGuus Sliepen <guus@meshlink.io>
Sun, 11 Oct 2015 11:32:55 +0000 (13:32 +0200)
committerGuus Sliepen <guus@sliepen.org>
Sun, 2 Jul 2017 09:53:11 +0000 (11:53 +0200)
As made obvious by commit 123f53f, the type of ssize_t cannot be trusted
on Windows to actually be signed, so don't use '< 0', as the compiler
will optimize the test away. POSIX also specifies only that -1 signals
an error condition, not any other negative value.

Luckily, comparing an unsigned int to -1 still works as intended.

selftest.c
test.c

index f6e4c8c4ce5abfa488964fca5776282fe46d58c3..8abf4cc6fdd56e3081ac0762c6bef78116ab489a 100644 (file)
@@ -82,7 +82,7 @@ int main(int argc, char *argv[]) {
        ssize_t len = utcp_send(c, "Hello world!\n", 13);
 
        if(len != 13) {
-               if(len < 0)
+               if(len == -1)
                        fprintf(stderr, "Error: %s\n", strerror(errno));
                else
                        fprintf(stderr, "Short write %zd!\n", len);
@@ -90,7 +90,7 @@ int main(int argc, char *argv[]) {
        len = utcp_send(c, "This is a test.\n", 16);
 
        if(len != 16) {
-               if(len < 0)
+               if(len == -1)
                        fprintf(stderr, "Error: %s\n", strerror(errno));
                else
                        fprintf(stderr, "Short write %zd!\n", len);
diff --git a/test.c b/test.c
index b5d64a3eb3ba698c8c763010131fbd386df5fbd0..75fce79dfd16d8697f32544a5e0a64c98e448aed 100644 (file)
--- a/test.c
+++ b/test.c
@@ -73,7 +73,7 @@ int main(int argc, char *argv[]) {
                return 1;
 
        int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-       if(s < 0)
+       if(s == -1)
                return 1;
 
        if(server) {
@@ -124,7 +124,7 @@ int main(int argc, char *argv[]) {
                                dir &= ~1;
                                if(c)
                                        utcp_shutdown(c, SHUT_WR);
-                               if(len < 0)
+                               if(len == -1)
                                        break;
                                else
                                        continue;