From 1a0afcde32b2740b21c2faebb94e010e5d3163d8 Mon Sep 17 00:00:00 2001 From: deraadt Date: Sun, 28 Nov 2021 19:28:41 +0000 Subject: [PATCH] Stop using MAXBSIZE to eliminate sys/param.h including (which injects a ton of namespace intrusion). Create local sizes, and refactor some code along the way. ok millert --- bin/cp/utils.c | 13 ++++++++----- bin/mv/cp.c | 15 +++++++++------ usr.bin/cdio/cdio.c | 8 ++++---- usr.bin/cdio/extern.h | 5 ++++- usr.bin/cdio/mmc.c | 9 ++++----- usr.bin/cvs/remote.c | 13 +++++++------ usr.bin/rcs/rcs.c | 10 ++++++---- usr.bin/rsync/copy.c | 8 +++++--- usr.bin/split/split.c | 9 +++++---- usr.bin/wc/wc.c | 13 +++++++------ usr.bin/xinstall/xinstall.c | 8 +++++--- usr.sbin/pwd_mkdb/pwd_mkdb.c | 12 +++++++----- 12 files changed, 71 insertions(+), 52 deletions(-) diff --git a/bin/cp/utils.c b/bin/cp/utils.c index 5c9c9e1b6c0..347081151f2 100644 --- a/bin/cp/utils.c +++ b/bin/cp/utils.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utils.c,v 1.49 2021/10/24 21:24:21 deraadt Exp $ */ +/* $OpenBSD: utils.c,v 1.50 2021/11/28 19:28:41 deraadt Exp $ */ /* $NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $ */ /*- @@ -30,7 +30,7 @@ * SUCH DAMAGE. */ -#include /* MAXBSIZE */ +#include #include #include #include @@ -47,6 +47,8 @@ #include "extern.h" +#define _MAXBSIZE (64 * 1024) + int copy_overwrite(void); int @@ -56,17 +58,18 @@ copy_file(FTSENT *entp, int exists) static char *zeroes; struct stat to_stat, *fs; int from_fd, rcount, rval, to_fd, wcount; + const size_t buflen = _MAXBSIZE; #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED char *p; #endif if (!buf) { - buf = malloc(MAXBSIZE); + buf = malloc(buflen); if (!buf) err(1, "malloc"); } if (!zeroes) { - zeroes = calloc(1, MAXBSIZE); + zeroes = calloc(1, buflen); if (!zeroes) err(1, "calloc"); } @@ -141,7 +144,7 @@ copy_file(FTSENT *entp, int exists) struct stat tosb; if (!fstat(to_fd, &tosb) && S_ISREG(tosb.st_mode)) skipholes = 1; - while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { + while ((rcount = read(from_fd, buf, buflen)) > 0) { if (skipholes && memcmp(buf, zeroes, rcount) == 0) wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? -1 : rcount; else diff --git a/bin/mv/cp.c b/bin/mv/cp.c index 02c5d8b6bcb..e04d8d8220c 100644 --- a/bin/mv/cp.c +++ b/bin/mv/cp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cp.c,v 1.9 2021/10/24 21:24:21 deraadt Exp $ */ +/* $OpenBSD: cp.c,v 1.10 2021/11/28 19:28:41 deraadt Exp $ */ /* $NetBSD: cp.c,v 1.14 1995/09/07 06:14:51 jtc Exp $ */ /* @@ -386,7 +386,7 @@ copy(char *argv[], enum op type, int fts_options) } -/* $OpenBSD: cp.c,v 1.9 2021/10/24 21:24:21 deraadt Exp $ */ +/* $OpenBSD: cp.c,v 1.10 2021/11/28 19:28:41 deraadt Exp $ */ /* $NetBSD: utils.c,v 1.6 1997/02/26 14:40:51 cgd Exp $ */ /*- @@ -418,7 +418,7 @@ copy(char *argv[], enum op type, int fts_options) * SUCH DAMAGE. */ -#include /* MAXBSIZE */ +#include #include #include #include @@ -433,6 +433,8 @@ copy(char *argv[], enum op type, int fts_options) #include #include +#define _MAXBSIZE (64 * 1024) + static int copy_file(FTSENT *entp, int dne) { @@ -440,17 +442,18 @@ copy_file(FTSENT *entp, int dne) static char *zeroes; struct stat *fs; int ch, checkch, from_fd, rcount, rval, to_fd, wcount; + const size_t buflen = _MAXBSIZE; #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED char *p; #endif if (!buf) { - buf = malloc(MAXBSIZE); + buf = malloc(buflen); if (!buf) err(1, "malloc"); } if (!zeroes) { - zeroes = calloc(1, MAXBSIZE); + zeroes = calloc(1, buflen); if (!zeroes) err(1, "calloc"); } @@ -532,7 +535,7 @@ copy_file(FTSENT *entp, int dne) struct stat tosb; if (!fstat(to_fd, &tosb) && S_ISREG(tosb.st_mode)) skipholes = 1; - while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { + while ((rcount = read(from_fd, buf, buflen)) > 0) { if (skipholes && memcmp(buf, zeroes, rcount) == 0) wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? -1 : rcount; else diff --git a/usr.bin/cdio/cdio.c b/usr.bin/cdio/cdio.c index d8a7a330bac..0bf6f03c797 100644 --- a/usr.bin/cdio/cdio.c +++ b/usr.bin/cdio/cdio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cdio.c,v 1.82 2021/10/24 21:24:16 deraadt Exp $ */ +/* $OpenBSD: cdio.c,v 1.83 2021/11/28 19:28:41 deraadt Exp $ */ /* Copyright (c) 1995 Serge V. Vakulenko * All rights reserved. @@ -52,7 +52,7 @@ * $FreeBSD: cdcontrol.c,v 1.13 1996/06/25 21:01:27 ache Exp $ */ -#include /* isset */ +#include #include #include #include @@ -526,7 +526,7 @@ run(int cmd, char *arg) warnx("Can't determine media type"); return (0); } - if (isset(mediacap, MMC_FEATURE_CDRW_WRITE) == 0 && + if (cdio_isset(mediacap, MMC_FEATURE_CDRW_WRITE) == 0 && get_media_type() != MEDIATYPE_CDRW) { warnx("The media doesn't support blanking"); return (0); @@ -669,7 +669,7 @@ tao(int argc, char **argv) exit(1); if (get_media_capabilities(mediacap, 1) == -1) errx(1, "Can't determine media type"); - if (isset(mediacap, MMC_FEATURE_CD_TAO) == 0) + if (cdio_isset(mediacap, MMC_FEATURE_CD_TAO) == 0) errx(1, "The media can't be written in TAO mode"); get_disc_size(&availblk); diff --git a/usr.bin/cdio/extern.h b/usr.bin/cdio/extern.h index f7d67f04e41..ee765981b97 100644 --- a/usr.bin/cdio/extern.h +++ b/usr.bin/cdio/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.16 2021/01/18 00:44:00 mortimer Exp $ */ +/* $OpenBSD: extern.h,v 1.17 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 2002 Marc Espie. * @@ -26,6 +26,9 @@ #include +#define cdio_isset(a,i) ((a)[(i)>>3] & (1<<((i)&(NBBY-1)))) +#define cdio_setbit(a,i) ((a)[(i)>>3] |= 1<<((i)&(NBBY-1))) + struct cd_toc_entry; struct track_info { off_t sz; diff --git a/usr.bin/cdio/mmc.c b/usr.bin/cdio/mmc.c index 0e17d31ab8e..9e091cc3bcc 100644 --- a/usr.bin/cdio/mmc.c +++ b/usr.bin/cdio/mmc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mmc.c,v 1.32 2020/09/01 17:20:02 krw Exp $ */ +/* $OpenBSD: mmc.c,v 1.33 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 2006 Michael Coulter * @@ -15,11 +15,10 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include #include #include -#include #include -#include /* setbit, isset */ #include #include #include @@ -274,7 +273,7 @@ get_media_capabilities(u_int8_t *cap, int rt) "" ); } } - setbit(cap, feature); + cdio_setbit(cap, feature); } return (0); @@ -288,7 +287,7 @@ set_speed(int wspeed) memset(&scr, 0, sizeof(scr)); scr.cmd[0] = SET_CD_SPEED; - scr.cmd[1] = (isset(mediacap, MMC_FEATURE_CDRW_CAV)) != 0; + scr.cmd[1] = (cdio_isset(mediacap, MMC_FEATURE_CDRW_CAV)) != 0; *(u_int16_t *)(scr.cmd + 2) = htobe16(DRIVE_SPEED_OPTIMAL); *(u_int16_t *)(scr.cmd + 4) = htobe16(wspeed); diff --git a/usr.bin/cvs/remote.c b/usr.bin/cvs/remote.c index d99b8d59e9b..8d3361e5723 100644 --- a/usr.bin/cvs/remote.c +++ b/usr.bin/cvs/remote.c @@ -1,4 +1,4 @@ -/* $OpenBSD: remote.c,v 1.33 2019/06/28 13:35:00 deraadt Exp $ */ +/* $OpenBSD: remote.c,v 1.34 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 2006 Joris Vink * @@ -15,7 +15,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include /* MAXBSIZE */ +#include #include #include @@ -29,6 +29,7 @@ #include "remote.h" #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) +#define _MAXBSIZE (64 * 1024) struct cvs_resp * cvs_remote_get_response_info(const char *response) @@ -128,7 +129,7 @@ void cvs_remote_receive_file(int fd, size_t len) { FILE *in; - char data[MAXBSIZE]; + char data[_MAXBSIZE]; size_t nread, nleft, toread; if (cvs_server_active) @@ -139,7 +140,7 @@ cvs_remote_receive_file(int fd, size_t len) nleft = len; while (nleft > 0) { - toread = MINIMUM(nleft, MAXBSIZE); + toread = MINIMUM(nleft, sizeof data); nread = fread(data, sizeof(char), toread, in); if (nread == 0) @@ -165,7 +166,7 @@ cvs_remote_send_file(const char *path, int _fd) size_t ret, rw; off_t total; struct stat st; - char buf[18], data[MAXBSIZE]; + char buf[18], data[_MAXBSIZE]; if (cvs_server_active) out = stdout; @@ -194,7 +195,7 @@ cvs_remote_send_file(const char *path, int _fd) fatal("cvs_remote_send_file: fdopen %s", strerror(errno)); total = 0; - while ((ret = fread(data, sizeof(char), MAXBSIZE, in)) != 0) { + while ((ret = fread(data, sizeof(char), sizeof data, in)) != 0) { rw = fwrite(data, sizeof(char), ret, out); if (rw != ret) fatal("failed to write %zu bytes", ret); diff --git a/usr.bin/rcs/rcs.c b/usr.bin/rcs/rcs.c index 30415b9de17..6528a4d1f34 100644 --- a/usr.bin/rcs/rcs.c +++ b/usr.bin/rcs/rcs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rcs.c,v 1.88 2019/01/09 17:57:05 joris Exp $ */ +/* $OpenBSD: rcs.c,v 1.89 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 2004 Jean-Francois Brousseau * All rights reserved. @@ -24,7 +24,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include /* MAXBSIZE */ +#include #include #include @@ -44,6 +44,8 @@ #include "rcsutil.h" #include "xmalloc.h" +#define _MAXBSIZE (64 * 1024) + #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) /* invalid characters in RCS states */ @@ -395,8 +397,8 @@ rcs_movefile(char *from, char *to, mode_t perm, u_int to_flags) return (-1); } - buf = xmalloc(MAXBSIZE); - while ((nread = fread(buf, sizeof(char), MAXBSIZE, src)) != 0) { + buf = xmalloc(_MAXBSIZE); + while ((nread = fread(buf, sizeof(char), _MAXBSIZE, src)) != 0) { if (ferror(src)) { warnx("failed to read `%s'", from); (void)unlink(to); diff --git a/usr.bin/rsync/copy.c b/usr.bin/rsync/copy.c index f133508cda0..f6a43d28434 100644 --- a/usr.bin/rsync/copy.c +++ b/usr.bin/rsync/copy.c @@ -1,4 +1,4 @@ -/* $OpenBSD: copy.c,v 1.2 2021/10/24 21:24:17 deraadt Exp $ */ +/* $OpenBSD: copy.c,v 1.3 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 2021 Claudio Jeker * @@ -15,7 +15,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include /* MAXBSIZE */ +#include #include #include @@ -23,6 +23,8 @@ #include "extern.h" +#define _MAXBSIZE (64 * 1024) + /* * Return true if all bytes in buffer are zero. * A buffer of zero lenght is also considered a zero buffer. @@ -42,7 +44,7 @@ iszero(const void *b, size_t len) static int copy_internal(int fromfd, int tofd) { - char buf[MAXBSIZE]; + char buf[_MAXBSIZE]; ssize_t r, w; while ((r = read(fromfd, buf, sizeof(buf))) > 0) { diff --git a/usr.bin/split/split.c b/usr.bin/split/split.c index 450752eb2eb..08fb8d74934 100644 --- a/usr.bin/split/split.c +++ b/usr.bin/split/split.c @@ -1,4 +1,4 @@ -/* $OpenBSD: split.c,v 1.22 2021/10/24 21:24:17 deraadt Exp $ */ +/* $OpenBSD: split.c,v 1.23 2021/11/28 19:28:42 deraadt Exp $ */ /* $NetBSD: split.c,v 1.5 1995/08/31 22:22:05 jtc Exp $ */ /* @@ -30,7 +30,6 @@ * SUCH DAMAGE. */ -#include /* MAXBSIZE */ #include #include @@ -43,13 +42,15 @@ #include #include +#define _MAXBSIZE (64 * 1024) + #define DEFLINE 1000 /* Default num lines per file. */ ssize_t bytecnt; /* Byte count to split on. */ long numlines; /* Line count to split on. */ int file_open; /* If a file open. */ int ifd = -1, ofd = -1; /* Input/output file descriptors. */ -char bfr[MAXBSIZE]; /* I/O buffer. */ +char bfr[_MAXBSIZE]; /* I/O buffer. */ char fname[PATH_MAX]; /* File name prefix. */ regex_t rgx; int pflag; @@ -176,7 +177,7 @@ split1(void) char *C; for (bcnt = 0;;) - switch ((len = read(ifd, bfr, MAXBSIZE))) { + switch ((len = read(ifd, bfr, sizeof(bfr)))) { case 0: exit(0); case -1: diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c index 8a3c4c1cd57..009933ff802 100644 --- a/usr.bin/wc/wc.c +++ b/usr.bin/wc/wc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: wc.c,v 1.28 2021/11/16 23:34:24 cheloha Exp $ */ +/* $OpenBSD: wc.c,v 1.29 2021/11/28 19:28:42 deraadt Exp $ */ /* * Copyright (c) 1980, 1987, 1991, 1993 @@ -29,7 +29,6 @@ * SUCH DAMAGE. */ -#include /* MAXBSIZE */ #include #include @@ -43,6 +42,8 @@ #include #include +#define _MAXBSIZE (64 * 1024) + int64_t tlinect, twordct, tcharct; int doline, doword, dochar, humanchar, multibyte; int rval; @@ -145,8 +146,8 @@ cnt(const char *path) } if (!doword && !multibyte) { - if (bufsz < MAXBSIZE && - (buf = realloc(buf, MAXBSIZE)) == NULL) + if (bufsz < _MAXBSIZE && + (buf = realloc(buf, _MAXBSIZE)) == NULL) err(1, NULL); /* * Line counting is split out because it's a lot @@ -154,7 +155,7 @@ cnt(const char *path) * the word count requires some logic. */ if (doline) { - while ((len = read(fd, buf, MAXBSIZE)) > 0) { + while ((len = read(fd, buf, _MAXBSIZE)) > 0) { charct += len; for (C = buf; len--; ++C) if (*C == '\n') @@ -184,7 +185,7 @@ cnt(const char *path) || ifmt == S_IFDIR) { charct = sbuf.st_size; } else { - while ((len = read(fd, buf, MAXBSIZE)) > 0) + while ((len = read(fd, buf, _MAXBSIZE)) > 0) charct += len; if (len == -1) { warn("%s", file); diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index b53b7d900d2..db534bc74be 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xinstall.c,v 1.75 2021/10/24 21:24:18 deraadt Exp $ */ +/* $OpenBSD: xinstall.c,v 1.76 2021/11/28 19:28:42 deraadt Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* @@ -30,7 +30,7 @@ * SUCH DAMAGE. */ -#include /* MAXBSIZE */ +#include #include #include #include @@ -51,6 +51,8 @@ #include "pathnames.h" +#define _MAXBSIZE (64 * 1024) + #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) #define DIRECTORY 0x01 /* Tell install it's a directory. */ @@ -403,7 +405,7 @@ copy(int from_fd, char *from_name, int to_fd, char *to_name, off_t size, { ssize_t nr, nw; int serrno; - char *p, buf[MAXBSIZE]; + char *p, buf[_MAXBSIZE]; if (size == 0) return; diff --git a/usr.sbin/pwd_mkdb/pwd_mkdb.c b/usr.sbin/pwd_mkdb/pwd_mkdb.c index b536ad09796..e23405b41c7 100644 --- a/usr.sbin/pwd_mkdb/pwd_mkdb.c +++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pwd_mkdb.c,v 1.58 2021/10/24 21:24:19 deraadt Exp $ */ +/* $OpenBSD: pwd_mkdb.c,v 1.59 2021/11/28 19:28:42 deraadt Exp $ */ /*- * Copyright (c) 1991, 1993, 1994 @@ -31,7 +31,7 @@ * SUCH DAMAGE. */ -#include /* MAXBSIZE */ +#include #include #include @@ -51,6 +51,8 @@ #include #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) +#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) +#define _MAXBSIZE (64 * 1024) #define INSECURE 1 #define SECURE 2 @@ -106,7 +108,7 @@ main(int argc, char **argv) uid_t olduid; gid_t shadow; int ch, tfd, makeold, secureonly, flags, checkonly; - char *username, buf[MAX(PATH_MAX, LINE_MAX * 2)]; + char *username, buf[MAXIMUM(PATH_MAX, LINE_MAX * 2)]; flags = checkonly = makeold = secureonly = 0; username = NULL; @@ -383,14 +385,14 @@ fmt: fatalc(EFTYPE, "%s", pname); void cp(char *from, char *to, mode_t mode) { - static char buf[MAXBSIZE]; + static char buf[_MAXBSIZE]; int from_fd, rcount, to_fd, wcount; if ((from_fd = open(from, O_RDONLY)) == -1) fatal("%s", from); if ((to_fd = open(to, O_WRONLY|O_CREAT|O_EXCL, mode)) == -1) fatal("%s", to); - while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) { + while ((rcount = read(from_fd, buf, sizeof buf)) > 0) { wcount = write(to_fd, buf, rcount); if (rcount != wcount || wcount == -1) fatal("%s to %s", from, to); -- 2.20.1