From 2097d45f7f38ccfaa8012ff3f9aab232808352a3 Mon Sep 17 00:00:00 2001 From: tg Date: Fri, 20 Mar 2015 21:46:41 +0000 Subject: [PATCH] oksh rcsid-only sync, plus bonus rewrite of strip_nuls (uses size_t ipv int for buffer size now, and no extra calls to memchr/memmove, input is typically small) --- misc.c | 43 +++++++++++++++++++------------------------ sh.h | 5 +++-- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/misc.c b/misc.c index da7455b..efa4992 100644 --- a/misc.c +++ b/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.39 2015/01/16 06:39:32 deraadt Exp $ */ +/* $OpenBSD: misc.c,v 1.40 2015/03/18 15:12:36 tedu Exp $ */ /* $OpenBSD: path.c,v 1.12 2005/03/30 17:16:37 deraadt Exp $ */ /*- @@ -30,7 +30,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.225 2015/03/01 15:23:04 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.226 2015/03/20 21:46:40 tg Exp $"); #define KSH_CHVT_FLAG #ifdef MKSH_SMALL @@ -1289,32 +1289,27 @@ print_columns(struct shf *shf, unsigned int n, afree(str, ATEMP); } -/* Strip any nul bytes from buf - returns new length (nbytes - # of nuls) */ +/* strip all NUL bytes from buf; output is NUL-terminated if stripped */ void -strip_nuls(char *buf, int nbytes) +strip_nuls(char *buf, size_t len) { - char *dst; + char *cp, *dp, *ep; - /* - * nbytes check because some systems (older FreeBSDs) have a - * buggy memchr() - */ - if (nbytes && (dst = memchr(buf, '\0', nbytes))) { - char *end = buf + nbytes; - char *p, *q; + if (!len || !(dp = memchr(buf, '\0', len))) + return; - for (p = dst; p < end; p = q) { - /* skip a block of nulls */ - while (++p < end && *p == '\0') - ; - /* find end of non-null block */ - if (!(q = memchr(p, '\0', end - p))) - q = end; - memmove(dst, p, q - p); - dst += q - p; - } - *dst = '\0'; - } + ep = buf + len; + cp = dp; + + cp_has_nul_byte: + while (cp++ < ep && *cp == '\0') + ; /* nothing */ + while (cp < ep && *cp != '\0') + *dp++ = *cp++; + if (cp < ep) + goto cp_has_nul_byte; + + *dp = '\0'; } /* diff --git a/sh.h b/sh.h index 1621132..679eec1 100644 --- a/sh.h +++ b/sh.h @@ -169,7 +169,7 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.719 2015/03/14 05:23:17 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.720 2015/03/20 21:46:41 tg Exp $"); #endif #define MKSH_VERSION "R50 2015/03/13" @@ -1869,7 +1869,8 @@ char *quote_value(const char *); void print_columns(struct shf *, unsigned int, char *(*)(char *, size_t, unsigned int, const void *), const void *, size_t, size_t, bool); -void strip_nuls(char *, int); +void strip_nuls(char *, size_t) + MKSH_A_BOUNDED(__string__, 1, 2); ssize_t blocking_read(int, char *, size_t) MKSH_A_BOUNDED(__buffer__, 2, 3); int reset_nonblock(int);