From e5e55cd69074f5b2bcdf93742c70f0e748959522 Mon Sep 17 00:00:00 2001 From: deraadt <> Date: Fri, 28 Jun 2019 13:32:49 +0000 Subject: [PATCH] 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. --- src/lib/libc/hash/helper.c | 8 ++++---- src/lib/libc/stdlib/malloc.c | 4 ++-- src/lib/libutil/check_expire.c | 8 ++++---- src/lib/libutil/getmaxpartitions.c | 4 ++-- src/lib/libutil/getrawpartition.c | 4 ++-- src/lib/libutil/logout.c | 4 ++-- src/lib/libutil/logwtmp.c | 4 ++-- src/lib/libutil/passwd.c | 4 ++-- src/lib/libutil/readlabel.c | 10 +++++----- src/lib/libutil/uucplock.c | 10 +++++----- src/usr.sbin/ntpd/constraint.c | 6 +++--- src/usr.sbin/ntpd/ntpd.c | 4 ++-- 12 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/lib/libc/hash/helper.c b/src/lib/libc/hash/helper.c index 8fa692af..f36d385e 100644 --- a/src/lib/libc/hash/helper.c +++ b/src/lib/libc/hash/helper.c @@ -1,4 +1,4 @@ -/* $OpenBSD: helper.c,v 1.17 2017/10/23 14:33:07 millert Exp $ */ +/* $OpenBSD: helper.c,v 1.18 2019/06/28 13:32:41 deraadt Exp $ */ /* * Copyright (c) 2000 Poul-Henning Kamp @@ -67,7 +67,7 @@ HASHFileChunk(const char *filename, char *buf, off_t off, off_t len) HASHInit(&ctx); - if ((fd = open(filename, O_RDONLY)) < 0) + if ((fd = open(filename, O_RDONLY)) == -1) return (NULL); if (len == 0) { if (fstat(fd, &sb) == -1) { @@ -78,7 +78,7 @@ HASHFileChunk(const char *filename, char *buf, off_t off, off_t len) } len = sb.st_size; } - if (off > 0 && lseek(fd, off, SEEK_SET) < 0) { + if (off > 0 && lseek(fd, off, SEEK_SET) == -1) { save_errno = errno; close(fd); errno = save_errno; @@ -94,7 +94,7 @@ HASHFileChunk(const char *filename, char *buf, off_t off, off_t len) save_errno = errno; close(fd); errno = save_errno; - return (nr < 0 ? NULL : HASHEnd(&ctx, buf)); + return (nr == -1 ? NULL : HASHEnd(&ctx, buf)); } DEF_WEAK(HASHFileChunk); diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index f2e82679..7d49438b 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.c,v 1.261 2019/05/23 06:43:18 otto Exp $ */ +/* $OpenBSD: malloc.c,v 1.262 2019/06/28 13:32:42 deraadt Exp $ */ /* * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek * Copyright (c) 2012 Matthew Dempsky @@ -897,7 +897,7 @@ omalloc_make_chunks(struct dir_info *d, int bits, int listnum) return NULL; /* memory protect the page allocated in the malloc(0) case */ - if (bits == 0 && mprotect(pp, MALLOC_PAGESIZE, PROT_NONE) < 0) + if (bits == 0 && mprotect(pp, MALLOC_PAGESIZE, PROT_NONE) == -1) goto err; bp = alloc_chunk_info(d, bits); diff --git a/src/lib/libutil/check_expire.c b/src/lib/libutil/check_expire.c index 7379cdbe..ebbaa2ac 100644 --- a/src/lib/libutil/check_expire.c +++ b/src/lib/libutil/check_expire.c @@ -1,4 +1,4 @@ -/* $OpenBSD: check_expire.c,v 1.12 2015/11/26 23:32:52 millert Exp $ */ +/* $OpenBSD: check_expire.c,v 1.13 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. @@ -166,7 +166,7 @@ pwd_update(const struct passwd *pwd, const struct passwd *opwd) pw_init(); tfd = pw_lock(0); - if (tfd < 0) { + if (tfd == -1) { if (errno == EEXIST) return("the passwd file is busy."); else @@ -174,13 +174,13 @@ pwd_update(const struct passwd *pwd, const struct passwd *opwd) } pfd = open(_PATH_MASTERPASSWD, O_RDONLY|O_CLOEXEC, 0); - if (pfd < 0) { + if (pfd == -1) { pw_abort(); return(strerror(errno)); } pw_copy(pfd, tfd, pwd, opwd); - if (pw_mkdb(pwd->pw_name, 0) < 0) { + if (pw_mkdb(pwd->pw_name, 0) == -1) { pw_abort(); return("unable to update password database"); } diff --git a/src/lib/libutil/getmaxpartitions.c b/src/lib/libutil/getmaxpartitions.c index 22f3c8d3..dd9d2bc6 100644 --- a/src/lib/libutil/getmaxpartitions.c +++ b/src/lib/libutil/getmaxpartitions.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getmaxpartitions.c,v 1.9 2016/08/27 03:54:20 guenther Exp $ */ +/* $OpenBSD: getmaxpartitions.c,v 1.10 2019/06/28 13:32:43 deraadt Exp $ */ /* $NetBSD: getmaxpartitions.c,v 1.1 1996/05/16 07:03:31 thorpej Exp $ */ /*- @@ -45,7 +45,7 @@ getmaxpartitions(void) mib[0] = CTL_KERN; mib[1] = KERN_MAXPARTITIONS; varlen = sizeof(maxpart); - if (sysctl(mib, 2, &maxpart, &varlen, NULL, (size_t)0) < 0) + if (sysctl(mib, 2, &maxpart, &varlen, NULL, (size_t)0) == -1) return (-1); return (maxpart); diff --git a/src/lib/libutil/getrawpartition.c b/src/lib/libutil/getrawpartition.c index 10324472..bc7fa06d 100644 --- a/src/lib/libutil/getrawpartition.c +++ b/src/lib/libutil/getrawpartition.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getrawpartition.c,v 1.9 2016/08/27 03:54:20 guenther Exp $ */ +/* $OpenBSD: getrawpartition.c,v 1.10 2019/06/28 13:32:43 deraadt Exp $ */ /* $NetBSD: getrawpartition.c,v 1.1 1996/05/16 07:03:33 thorpej Exp $ */ /*- @@ -45,7 +45,7 @@ getrawpartition(void) mib[0] = CTL_KERN; mib[1] = KERN_RAWPARTITION; varlen = sizeof(rawpart); - if (sysctl(mib, 2, &rawpart, &varlen, NULL, (size_t)0) < 0) + if (sysctl(mib, 2, &rawpart, &varlen, NULL, (size_t)0) == -1) return (-1); return (rawpart); diff --git a/src/lib/libutil/logout.c b/src/lib/libutil/logout.c index 13c11fa8..c2b68186 100644 --- a/src/lib/libutil/logout.c +++ b/src/lib/libutil/logout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: logout.c,v 1.9 2015/12/28 20:11:36 guenther Exp $ */ +/* $OpenBSD: logout.c,v 1.10 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -47,7 +47,7 @@ logout(const char *line) int fd, rval; UTMP ut; - if ((fd = open(_PATH_UTMP, O_RDWR|O_CLOEXEC)) < 0) + if ((fd = open(_PATH_UTMP, O_RDWR|O_CLOEXEC)) == -1) return(0); rval = 0; while (read(fd, &ut, sizeof(UTMP)) == sizeof(UTMP)) { diff --git a/src/lib/libutil/logwtmp.c b/src/lib/libutil/logwtmp.c index decde069..1223d7a4 100644 --- a/src/lib/libutil/logwtmp.c +++ b/src/lib/libutil/logwtmp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: logwtmp.c,v 1.10 2016/08/30 14:44:45 guenther Exp $ */ +/* $OpenBSD: logwtmp.c,v 1.11 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -46,7 +46,7 @@ logwtmp(const char *line, const char *name, const char *host) struct utmp ut; int fd; - if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) < 0) + if ((fd = open(_PATH_WTMP, O_WRONLY|O_APPEND|O_CLOEXEC)) == -1) return; if (fstat(fd, &buf) == 0) { (void) strncpy(ut.ut_line, line, sizeof(ut.ut_line)); diff --git a/src/lib/libutil/passwd.c b/src/lib/libutil/passwd.c index cd292942..7acd7085 100644 --- a/src/lib/libutil/passwd.c +++ b/src/lib/libutil/passwd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: passwd.c,v 1.55 2018/08/10 17:03:26 deraadt Exp $ */ +/* $OpenBSD: passwd.c,v 1.56 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1987, 1993, 1994, 1995 @@ -101,7 +101,7 @@ pw_lock(int retries) /* Acquire the lock file. */ old_mode = umask(0); fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600); - for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) { + for (i = 0; i < retries && fd == -1 && errno == EEXIST; i++) { sleep(1); fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0600); } diff --git a/src/lib/libutil/readlabel.c b/src/lib/libutil/readlabel.c index d53820e6..9d811b5c 100644 --- a/src/lib/libutil/readlabel.c +++ b/src/lib/libutil/readlabel.c @@ -1,4 +1,4 @@ -/* $OpenBSD: readlabel.c,v 1.14 2016/08/30 14:44:45 guenther Exp $ */ +/* $OpenBSD: readlabel.c,v 1.15 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1996, Jason Downs. All rights reserved. @@ -74,7 +74,7 @@ readlabelfs(char *device, int verbose) } /* Assuming device is of the form /dev/??p, build a raw partition. */ - if (stat(device, &sbuf) < 0) { + if (stat(device, &sbuf) == -1) { if (verbose) warn("%s", device); return (NULL); @@ -106,12 +106,12 @@ readlabelfs(char *device, int verbose) /* If rpath doesn't exist, change that partition back. */ fd = open(rpath, O_RDONLY|O_CLOEXEC); - if (fd < 0) { + if (fd == -1) { if (errno == ENOENT) { rpath[strlen(rpath) - 1] = part; fd = open(rpath, O_RDONLY|O_CLOEXEC); - if (fd < 0) { + if (fd == -1) { if (verbose) warn("%s", rpath); return (NULL); @@ -125,7 +125,7 @@ readlabelfs(char *device, int verbose) disklabel: - if (ioctl(fd, DIOCGDINFO, &dk) < 0) { + if (ioctl(fd, DIOCGDINFO, &dk) == -1) { if (verbose) warn("%s: couldn't read disklabel", rpath); close(fd); diff --git a/src/lib/libutil/uucplock.c b/src/lib/libutil/uucplock.c index f3838228..0ec64942 100644 --- a/src/lib/libutil/uucplock.c +++ b/src/lib/libutil/uucplock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uucplock.c,v 1.19 2016/08/30 14:52:09 guenther Exp $ */ +/* $OpenBSD: uucplock.c,v 1.20 2019/06/28 13:32:43 deraadt Exp $ */ /* * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. @@ -71,11 +71,11 @@ uu_lock(const char *ttyname) (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); tmpfd = open(lcktmpname, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0664); - if (tmpfd < 0) + if (tmpfd == -1) GORET(0, UU_LOCK_CREAT_ERR); for (i = 0; i < MAXTRIES; i++) { - if (link(lcktmpname, lckname) < 0) { + if (link(lcktmpname, lckname) == -1) { if (errno != EEXIST) GORET(1, UU_LOCK_LINK_ERR); /* @@ -83,7 +83,7 @@ uu_lock(const char *ttyname) * check to see if the process holding the lock * still exists */ - if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0) + if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) == -1) GORET(1, UU_LOCK_OPEN_ERR); if ((pid_old = get_pid(fd, &err)) == -1) @@ -127,7 +127,7 @@ uu_lock_txfr(const char *ttyname, pid_t pid) snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); - if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0) + if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) == -1) return UU_LOCK_OWNER_ERR; if (get_pid(fd, &err) != getpid()) ret = UU_LOCK_OWNER_ERR; diff --git a/src/usr.sbin/ntpd/constraint.c b/src/usr.sbin/ntpd/constraint.c index f1af7fa6..f478ec78 100644 --- a/src/usr.sbin/ntpd/constraint.c +++ b/src/usr.sbin/ntpd/constraint.c @@ -1,4 +1,4 @@ -/* $OpenBSD: constraint.c,v 1.46 2019/06/16 07:36:25 otto Exp $ */ +/* $OpenBSD: constraint.c,v 1.47 2019/06/28 13:32:49 deraadt Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -955,7 +955,7 @@ httpsdate_request(struct httpsdate *httpsdate, struct timeval *when) ret = tls_write(httpsdate->tls_ctx, buf, len); if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) continue; - if (ret < 0) { + if (ret == -1) { log_warnx("tls write failed: %s (%s): %s", httpsdate->tls_addr, httpsdate->tls_hostname, tls_error(httpsdate->tls_ctx)); @@ -1091,7 +1091,7 @@ tls_readline(struct tls *tls, size_t *lenp, size_t *maxlength, ret = tls_read(tls, &c, 1); if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) goto again; - if (ret < 0) { + if (ret == -1) { /* SSL read error, ignore */ free(buf); return (NULL); diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 33037db8..ef1b43e1 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.123 2019/06/27 15:18:42 otto Exp $ */ +/* $OpenBSD: ntpd.c,v 1.124 2019/06/28 13:32:49 deraadt Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -111,7 +111,7 @@ auto_preconditions(const struct ntpd_conf *cnf) int constraints, securelevel; size_t sz = sizeof(int); - if (sysctl(mib, 2, &securelevel, &sz, NULL, 0) < 0) + if (sysctl(mib, 2, &securelevel, &sz, NULL, 0) == -1) err(1, "sysctl"); constraints = !TAILQ_EMPTY(&cnf->constraints); return !cnf->settime && constraints && securelevel == 0;