From 61656abc7ff84215165af1bd464bc053b3b66158 Mon Sep 17 00:00:00 2001 From: Theo de Raadt Date: Fri, 28 Jun 2019 13:35:06 +0000 Subject: When system calls indicate an error they return -1, not some arbitrary value < 0. errno is only updated in this case. Change all (most?) callers of syscalls to follow this better, and let's see if this strictness helps us in the future. --- usr.bin/nc/netcat.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'usr.bin/nc') diff --git a/usr.bin/nc/netcat.c b/usr.bin/nc/netcat.c index 26288560b06..c2e769c4c05 100644 --- a/usr.bin/nc/netcat.c +++ b/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.204 2019/06/27 18:03:37 deraadt Exp $ */ +/* $OpenBSD: netcat.c,v 1.205 2019/06/28 13:35:02 deraadt Exp $ */ /* * Copyright (c) 2001 Eric Jackson * Copyright (c) 2015 Bob Beck. All rights reserved. @@ -568,7 +568,7 @@ main(int argc, char *argv[]) close(s); s = local_listen(host, uport, hints); } - if (s < 0) + if (s == -1) err(1, NULL); if (uflag && kflag) { /* @@ -590,11 +590,11 @@ main(int argc, char *argv[]) len = sizeof(z); rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, (struct sockaddr *)&z, &len); - if (rv < 0) + if (rv == -1) err(1, "recvfrom"); rv = connect(s, (struct sockaddr *)&z, len); - if (rv < 0) + if (rv == -1) err(1, "connect"); if (vflag) @@ -628,7 +628,7 @@ main(int argc, char *argv[]) tls_free(tls_cctx); } if (family == AF_UNIX && uflag) { - if (connect(s, NULL, 0) < 0) + if (connect(s, NULL, 0) == -1) err(1, "connect"); } @@ -739,7 +739,7 @@ unix_bind(char *path, int flags) /* Create unix domain socket. */ if ((s = socket(AF_UNIX, flags | (uflag ? SOCK_DGRAM : SOCK_STREAM), - 0)) < 0) + 0)) == -1) return -1; memset(&s_un, 0, sizeof(struct sockaddr_un)); @@ -752,7 +752,7 @@ unix_bind(char *path, int flags) return -1; } - if (bind(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) { + if (bind(s, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) { save_errno = errno; close(s); errno = save_errno; @@ -862,10 +862,10 @@ unix_connect(char *path) int s, save_errno; if (uflag) { - if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) < 0) + if ((s = unix_bind(unix_dg_tmp_socket, SOCK_CLOEXEC)) == -1) return -1; } else { - if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) + if ((s = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) return -1; } @@ -878,7 +878,7 @@ unix_connect(char *path) errno = ENAMETOOLONG; return -1; } - if (connect(s, (struct sockaddr *)&s_un, sizeof(s_un)) < 0) { + if (connect(s, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) { save_errno = errno; close(s); errno = save_errno; @@ -897,9 +897,9 @@ unix_listen(char *path) { int s; - if ((s = unix_bind(path, 0)) < 0) + if ((s = unix_bind(path, 0)) == -1) return -1; - if (listen(s, 5) < 0) { + if (listen(s, 5) == -1) { close(s); return -1; } @@ -926,7 +926,7 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) for (res = res0; res; res = res->ai_next) { if ((s = socket(res->ai_family, res->ai_socktype | - SOCK_NONBLOCK, res->ai_protocol)) < 0) + SOCK_NONBLOCK, res->ai_protocol)) == -1) continue; /* Bind to a local port or source address if specified. */ @@ -944,7 +944,7 @@ remote_connect(const char *host, const char *port, struct addrinfo hints) errx(1, "getaddrinfo: %s", gai_strerror(error)); if (bind(s, (struct sockaddr *)ares->ai_addr, - ares->ai_addrlen) < 0) + ares->ai_addrlen) == -1) err(1, "bind failed"); freeaddrinfo(ares); } @@ -1023,7 +1023,7 @@ local_listen(const char *host, const char *port, struct addrinfo hints) for (res = res0; res; res = res->ai_next) { if ((s = socket(res->ai_family, res->ai_socktype, - res->ai_protocol)) < 0) + res->ai_protocol)) == -1) continue; ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x)); @@ -1043,7 +1043,7 @@ local_listen(const char *host, const char *port, struct addrinfo hints) } if (!uflag && s != -1) { - if (listen(s, 1) < 0) + if (listen(s, 1) == -1) err(1, "listen"); } if (vflag && s != -1) { -- cgit v1.2.3