From e6395046dfbcdea6389a449aa9748ceebd8d0fa3 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 29 Apr 2017 22:04:31 +0000 Subject: [PATCH] use strnul(); optimise --- edit.c | 12 ++++++------ eval.c | 13 ++++++------- exec.c | 4 ++-- histrap.c | 6 +++--- misc.c | 10 +++++----- os2.c | 8 +++++--- sh.h | 4 ++-- var.c | 4 ++-- 8 files changed, 31 insertions(+), 30 deletions(-) diff --git a/edit.c b/edit.c index 9f58cd7..b8e93f2 100644 --- a/edit.c +++ b/edit.c @@ -28,7 +28,7 @@ #ifndef MKSH_NO_CMDLINE_EDITING -__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.334 2017/04/29 14:20:23 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.335 2017/04/29 22:04:26 tg Exp $"); /* * in later versions we might use libtermcap for this, but since external @@ -747,7 +747,7 @@ x_basename(const char *s, const char *se) const char *p; if (se == NULL) - se = s + strlen(s); + se = strnul(s); if (s == se) return (0); @@ -799,7 +799,7 @@ glob_path(int flags, const char *pat, XPtrV *wp, const char *lpath) while (sp) { xp = Xstring(xs, xp); if (!(p = cstrchr(sp, MKSH_PATHSEPC))) - p = sp + strlen(sp); + p = strnul(sp); pathlen = p - sp; if (pathlen) { /* @@ -1852,7 +1852,7 @@ x_load_hist(char **hp) strlcpy(holdbufp, xbuf, LINE); strlcpy(xbuf, sp, xend - xbuf); xbp = xbuf; - xep = xcp = xbuf + strlen(xbuf); + xep = xcp = strnul(xbuf); x_adjust(); modified = 0; } @@ -3055,7 +3055,7 @@ x_version(int c MKSH_A_UNUSED) strdupx(v, KSH_VERSION, ATEMP); xbuf = xbp = xcp = v; - xend = xep = v + strlen(v); + xend = xep = strnul(v); x_redraw('\r'); x_flush(); @@ -3098,7 +3098,7 @@ x_edit_line(int c MKSH_A_UNUSED) "fc -e ${VISUAL:-${EDITOR:-vi}} --", x_arg); else strlcpy(xbuf, "fc -e ${VISUAL:-${EDITOR:-vi}} --", xend - xbuf); - xep = xbuf + strlen(xbuf); + xep = strnul(xbuf); return (x_newline('\n')); } #endif diff --git a/eval.c b/eval.c index d9720d2..0b2b39c 100644 --- a/eval.c +++ b/eval.c @@ -23,7 +23,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.208 2017/04/29 21:49:06 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.209 2017/04/29 22:04:27 tg Exp $"); /* * string expansion @@ -1405,7 +1405,7 @@ comsub(Expand *xp, const char *cp, int fn) if (!herein(io, &name)) { xp->str = name; /* as $(…) requires, trim trailing newlines */ - name += strlen(name); + name = strnul(name); while (name > xp->str && name[-1] == '\n') --name; *name = '\0'; @@ -1655,7 +1655,7 @@ globit(XString *xs, /* dest string */ *np++ = '\0'; } else { odirsep = '\0'; /* keep gcc quiet */ - se = sp + strlen(sp); + se = strnul(sp); } @@ -1669,7 +1669,7 @@ globit(XString *xs, /* dest string */ if (!has_globbing(sp)) { XcheckN(*xs, xp, se - sp + 1); debunk(xp, sp, Xnleft(*xs, xp)); - xp += strlen(xp); + xp = strnul(xp); *xpp = xp; globit(xs, xpp, np, wp, check); } else { @@ -1698,9 +1698,8 @@ globit(XString *xs, /* dest string */ XcheckN(*xs, xp, len); memcpy(xp, name, len); *xpp = xp + len - 1; - globit(xs, xpp, np, wp, - (check & GF_MARKDIR) | GF_GLOBBED - | (np ? GF_EXCHECK : GF_NONE)); + globit(xs, xpp, np, wp, (check & GF_MARKDIR) | + GF_GLOBBED | (np ? GF_EXCHECK : GF_NONE)); xp = Xstring(*xs, xp) + prefix_len; } closedir(dirp); diff --git a/exec.c b/exec.c index f0149eb..dd0565f 100644 --- a/exec.c +++ b/exec.c @@ -23,7 +23,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.197 2017/04/28 00:38:29 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.198 2017/04/29 22:04:28 tg Exp $"); #ifndef MKSH_DEFAULT_EXECSHELL #define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh" @@ -1341,7 +1341,7 @@ search_path(const char *name, const char *lpath, while (sp != NULL) { xp = Xstring(xs, xp); if (!(p = cstrchr(sp, MKSH_PATHSEPC))) - p = sp + strlen(sp); + p = strnul(sp); if (p != sp) { XcheckN(xs, xp, p - sp); memcpy(xp, sp, p - sp); diff --git a/histrap.c b/histrap.c index 58640f5..79d0340 100644 --- a/histrap.c +++ b/histrap.c @@ -3,7 +3,7 @@ /*- * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, - * 2011, 2012, 2014, 2015, 2016 + * 2011, 2012, 2014, 2015, 2016, 2017 * mirabilos * * Provided that these terms and disclaimer and all copyright notices @@ -27,7 +27,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.161 2017/04/27 19:33:49 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.162 2017/04/29 22:04:28 tg Exp $"); Trap sigtraps[ksh_NSIG + 1]; static struct sigaction Sigact_ign; @@ -629,7 +629,7 @@ histsave(int *lnp, const char *cmd, int svmode, bool ignoredups) if (svmode == HIST_FLUSH) return; - ccp = cmd + strlen(cmd); + ccp = strnul(cmd); while (ccp > cmd && ccp[-1] == '\n') --ccp; strndupx(c, cmd, ccp - cmd, APERM); diff --git a/misc.c b/misc.c index ba837ff..9542a7f 100644 --- a/misc.c +++ b/misc.c @@ -32,7 +32,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.271 2017/04/29 21:49:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.272 2017/04/29 22:04:29 tg Exp $"); #define KSH_CHVT_FLAG #ifdef MKSH_SMALL @@ -556,7 +556,7 @@ simplify_gmatch_pattern(const unsigned char *sp) sp = cp; simplify_gmatch_pat1a: dp = cp; - se = sp + strlen((const void *)sp); + se = strnul(sp); while ((c = *sp++)) { if (!ISMAGIC(c)) { *dp++ = c; @@ -628,8 +628,7 @@ gmatchx(const char *s, const char *p, bool isfile) if (s == NULL || p == NULL) return (0); - se = s + strlen(s); - pe = p + strlen(p); + pe = strnul(p); /* * isfile is false iff no syntax check has been done on * the pattern. If check fails, just do a strcmp(). @@ -641,13 +640,14 @@ gmatchx(const char *s, const char *p, bool isfile) debunk(t, p, len); return (!strcmp(t, s)); } + se = strnul(s); /* * since the do_gmatch() engine sucks so much, we must do some * pattern simplifications */ pnew = simplify_gmatch_pattern((const unsigned char *)p); - pe = pnew + strlen(pnew); + pe = strnul(pnew); rv = do_gmatch((const unsigned char *)s, (const unsigned char *)se, (const unsigned char *)pnew, (const unsigned char *)pe); diff --git a/os2.c b/os2.c index 5d39630..fc27d5a 100644 --- a/os2.c +++ b/os2.c @@ -1,6 +1,8 @@ /*- * Copyright (c) 2015 * KO Myung-Hun + * Copyright (c) 2017 + * mirabilos * * Provided that these terms and disclaimer and all copyright notices * are retained or reproduced in an accompanying document, permission @@ -28,7 +30,7 @@ #include #include -__RCSID("$MirOS: src/bin/mksh/os2.c,v 1.1 2017/04/02 15:00:44 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/os2.c,v 1.2 2017/04/29 22:04:29 tg Exp $"); static char *remove_trailing_dots(char *); static int access_stat_ex(int (*)(), const char *, void *); @@ -247,9 +249,9 @@ setextlibpath(const char *name, const char *val) static char * remove_trailing_dots(char *name) { - char *p; + char *p = strnul(name); - for (p = name + strlen(name); --p > name && *p == '.'; ) + while (--p > name && *p == '.') /* nothing */; if (*p != '.' && *p != '/' && *p != '\\' && *p != ':') diff --git a/sh.h b/sh.h index e1d7171..4a82721 100644 --- a/sh.h +++ b/sh.h @@ -175,7 +175,7 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.833 2017/04/29 21:49:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.834 2017/04/29 22:04:30 tg Exp $"); #endif #define MKSH_VERSION "R55 2017/04/27" @@ -649,7 +649,7 @@ im_sorry_dave(void) #endif /* use this ipv strchr(s, 0) but no side effects in s! */ -#define strnul(s) ((s) + strlen(s)) +#define strnul(s) ((s) + strlen((const void *)s)) #define utf_ptradjx(src, dst) do { \ (dst) = (src) + utf_ptradj(src); \ diff --git a/var.c b/var.c index 6f7493e..73dac13 100644 --- a/var.c +++ b/var.c @@ -28,7 +28,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.216 2017/04/27 19:33:53 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.217 2017/04/29 22:04:31 tg Exp $"); /*- * Variables @@ -464,7 +464,7 @@ setstr(struct tbl *vq, const char *s, int error_ok) #ifndef MKSH_SMALL /* debugging */ if (s >= vq->val.s && - s <= vq->val.s + strlen(vq->val.s)) { + s <= strnul(vq->val.s)) { internal_errorf( "setstr: %s=%s: assigning to self", vq->name, s);