From 64330eb00f0c3a2071a86177cbc45fed8eb3d6eb Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Sat, 29 Jun 2013 15:52:48 +0000 Subject: Fix -Wunsequenced warning Submitted by: dt71@gmx.com --- lib/libutil/login_times.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/libutil') diff --git a/lib/libutil/login_times.c b/lib/libutil/login_times.c index e2c7bf2341f20..d13ed9970618f 100644 --- a/lib/libutil/login_times.c +++ b/lib/libutil/login_times.c @@ -96,7 +96,7 @@ parse_lt(const char *str) else m.lt_start = 0; if (*p == '-') - p = parse_time(++p, &m.lt_end); + p = parse_time(p + 1, &m.lt_end); else m.lt_end = 1440; -- cgit v1.3 From 869f276295d78e0f18c88c2966194a4e5594af3e Mon Sep 17 00:00:00 2001 From: Sergey Kandaurov Date: Wed, 21 Aug 2013 16:46:06 +0000 Subject: Check strtoumax(3) for ERANGE in case of non-prefixed string. OK'd by: silence on current@ MFC after: 1 week --- lib/libutil/expand_number.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/libutil') diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index 5d55884d9ac54..0a62c12556a00 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -55,6 +55,10 @@ expand_number(const char *buf, uint64_t *num) number = strtoumax(buf, &endptr, 0); + if (number == UINTMAX_MAX && errno == ERANGE) { + return (-1); + } + if (endptr == buf) { /* No valid digits. */ errno = EINVAL; -- cgit v1.3 From 7261b203c6223fcbb1073fee20b7bbaf8f1804aa Mon Sep 17 00:00:00 2001 From: Sergey Kandaurov Date: Wed, 21 Aug 2013 22:37:15 +0000 Subject: Reset errno before strtoumax() call to properly detect ERANGE. Restore saved errno if strtoumax() call is successful. Reported by: ache Reviewed by: jilles MFC after: 1 week --- lib/libutil/expand_number.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/libutil') diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index 0a62c12556a00..401e2d9ac8b6f 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -50,15 +50,22 @@ int expand_number(const char *buf, uint64_t *num) { uint64_t number; + int saved_errno; unsigned shift; char *endptr; + saved_errno = errno; + errno = 0; + number = strtoumax(buf, &endptr, 0); if (number == UINTMAX_MAX && errno == ERANGE) { return (-1); } + if (errno == 0) + errno = saved_errno; + if (endptr == buf) { /* No valid digits. */ errno = EINVAL; -- cgit v1.3 From d1d4d95209e4ba5da75c16a5653bc46fca6418f8 Mon Sep 17 00:00:00 2001 From: Jilles Tjoelker Date: Wed, 28 Aug 2013 21:10:37 +0000 Subject: libutil: Use O_CLOEXEC for internal file descriptors from open(). --- lib/libutil/login_auth.c | 2 +- lib/libutil/login_cap.c | 2 +- lib/libutil/pidfile.c | 2 +- lib/libutil/quotafile.c | 8 +++++--- lib/libutil/uucplock.c | 7 ++++--- 5 files changed, 12 insertions(+), 9 deletions(-) (limited to 'lib/libutil') diff --git a/lib/libutil/login_auth.c b/lib/libutil/login_auth.c index ce9a2e47c316e..1fc7e73d6bc30 100644 --- a/lib/libutil/login_auth.c +++ b/lib/libutil/login_auth.c @@ -98,7 +98,7 @@ auth_cat(const char *file) int fd, count; char buf[BUFSIZ]; - if ((fd = open(file, O_RDONLY)) < 0) + if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) return 0; while ((count = read(fd, buf, sizeof(buf))) > 0) (void)write(fileno(stdout), buf, count); diff --git a/lib/libutil/login_cap.c b/lib/libutil/login_cap.c index 8fee76050b041..8915d0af279e8 100644 --- a/lib/libutil/login_cap.c +++ b/lib/libutil/login_cap.c @@ -239,7 +239,7 @@ login_getclassbyname(char const *name, const struct passwd *pwd) break; /* Don't retry default on 'me' */ if (i == 0) r = -1; - else if ((r = open(login_dbarray[0], O_RDONLY)) >= 0) + else if ((r = open(login_dbarray[0], O_RDONLY | O_CLOEXEC)) >= 0) close(r); /* * If there's at least one login class database, diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c index 7949e9e9a0345..3a5e5121c1479 100644 --- a/lib/libutil/pidfile.c +++ b/lib/libutil/pidfile.c @@ -73,7 +73,7 @@ pidfile_read(const char *path, pid_t *pidptr) char buf[16], *endptr; int error, fd, i; - fd = open(path, O_RDONLY); + fd = open(path, O_RDONLY | O_CLOEXEC); if (fd == -1) return (errno); diff --git a/lib/libutil/quotafile.c b/lib/libutil/quotafile.c index 03e3de4e4937f..6b5a44d9cdf74 100644 --- a/lib/libutil/quotafile.c +++ b/lib/libutil/quotafile.c @@ -137,7 +137,7 @@ quota_open(struct fstab *fs, int quotatype, int openflags) goto error; } qf->accmode = openflags & O_ACCMODE; - if ((qf->fd = open(qf->qfname, qf->accmode)) < 0 && + if ((qf->fd = open(qf->qfname, qf->accmode|O_CLOEXEC)) < 0 && (openflags & O_CREAT) != O_CREAT) goto error; /* File open worked, so process it */ @@ -168,7 +168,8 @@ quota_open(struct fstab *fs, int quotatype, int openflags) /* not reached */ } /* open failed, but O_CREAT was specified, so create a new file */ - if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) + if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0)) < + 0) goto error; qf->wordsize = 64; memset(&dqh, 0, sizeof(dqh)); @@ -534,7 +535,8 @@ quota_convert(struct quotafile *qf, int wordsize) free(newqf); return (-1); } - if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) { + if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, + 0)) < 0) { serrno = errno; goto error; } diff --git a/lib/libutil/uucplock.c b/lib/libutil/uucplock.c index afbfaa4d05374..424ac26651e1d 100644 --- a/lib/libutil/uucplock.c +++ b/lib/libutil/uucplock.c @@ -76,7 +76,8 @@ uu_lock(const char *tty_name) pid); (void)snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name); - if ((tmpfd = creat(lcktmpname, 0664)) < 0) + if ((tmpfd = open(lcktmpname, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, + 0664)) < 0) GORET(0, UU_LOCK_CREAT_ERR); for (i = 0; i < MAXTRIES; i++) { @@ -88,7 +89,7 @@ uu_lock(const char *tty_name) * check to see if the process holding the lock * still exists */ - if ((fd = open(lckname, O_RDONLY)) < 0) + if ((fd = open(lckname, O_RDONLY | O_CLOEXEC)) < 0) GORET(1, UU_LOCK_OPEN_ERR); if ((pid_old = get_pid (fd, &err)) == -1) @@ -132,7 +133,7 @@ uu_lock_txfr(const char *tty_name, pid_t pid) snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, tty_name); - if ((fd = open(lckname, O_RDWR)) < 0) + if ((fd = open(lckname, O_RDWR | O_CLOEXEC)) < 0) return UU_LOCK_OWNER_ERR; if (get_pid(fd, &err) != getpid()) err = UU_LOCK_OWNER_ERR; -- cgit v1.3 From 8d99b9cebd80ba6c1e85969fe9d9ea0962d31d14 Mon Sep 17 00:00:00 2001 From: Sergey Kandaurov Date: Fri, 30 Aug 2013 11:21:52 +0000 Subject: The round of expand_number() cleanups. o Fix range error checking to detect overflow when uint64_t < uintmax_t. o Remove a non-functional check for no valid digits as pointed out by Bruce. o Remove a rather pointless comment describing what the function does. o Clean up a bunch of style bugs. Brucified by: bde --- lib/libutil/expand_number.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) (limited to 'lib/libutil') diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index 401e2d9ac8b6f..24bcc39a35e94 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -35,42 +35,24 @@ __FBSDID("$FreeBSD$"); #include #include -/* - * Convert an expression of the following forms to a uint64_t. - * 1) A positive decimal number. - * 2) A positive decimal number followed by a 'b' or 'B' (mult by 1). - * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). - * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). - * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). - * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). - * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). - * 8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60). - */ int expand_number(const char *buf, uint64_t *num) { + char *endptr; + uintmax_t umaxval; uint64_t number; - int saved_errno; unsigned shift; - char *endptr; + int serrno; - saved_errno = errno; + serrno = errno; errno = 0; - - number = strtoumax(buf, &endptr, 0); - - if (number == UINTMAX_MAX && errno == ERANGE) { - return (-1); - } - - if (errno == 0) - errno = saved_errno; - - if (endptr == buf) { - /* No valid digits. */ - errno = EINVAL; + umaxval = strtoumax(buf, &endptr, 0); + if (umaxval > UINT64_MAX) + errno = ERANGE; + if (errno != 0) return (-1); - } + errno = serrno; + number = umaxval; switch (tolower((unsigned char)*endptr)) { case 'e': @@ -106,7 +88,6 @@ expand_number(const char *buf, uint64_t *num) errno = ERANGE; return (-1); } - *num = number << shift; return (0); } -- cgit v1.3