From: Guus Sliepen Date: Sun, 11 Oct 2015 11:32:55 +0000 (+0200) Subject: Use '== -1' to check for errors from functions. X-Git-Url: http://git.meshlink.io/?p=utcp;a=commitdiff_plain;h=9c93346a7b4a539d111a2ebb1a95d70b394bc7f4 Use '== -1' to check for errors from functions. 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. --- diff --git a/selftest.c b/selftest.c index f6e4c8c..8abf4cc 100644 --- a/selftest.c +++ b/selftest.c @@ -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 b5d64a3..75fce79 100644 --- 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;