From 2d65148fadd6d343d0c784e5f9f5eab8b8ec67af Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 25 Nov 2021 03:02:34 +0100 Subject: [PATCH] arith.c: scope(): de-obfuscate some code This function adds the NV_ADD flag to its 'flags' variable for nv_serach() calls subject to some checks. However, every call that uses that variable explicitly turns off the NV_ADD bit again. A search in the ast-open-history repo reveals that this check briefly made a difference between versions 2010-06-25 and 2010-08-11, but it's been a complete no-op ever since. src/cmd/ksh93/sh/arith.c: scope(): - Remove no-op code. - Resolve the constant expressions involving the 'flags' variable, get rid of the variable, and just indicate the flag bitmasks directly in the nv_search() calls. - Detangle and split up the excessively long 'if' construct. No change in behaviour. Previously noticed by Kurtis Rader for ksh2020: https://github.com/att/ast/commit/d5ce3b05 --- src/cmd/ksh93/sh/arith.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cmd/ksh93/sh/arith.c b/src/cmd/ksh93/sh/arith.c index 0cc88b674..fa47bed19 100644 --- a/src/cmd/ksh93/sh/arith.c +++ b/src/cmd/ksh93/sh/arith.c @@ -64,7 +64,6 @@ static Namval_t *scope(register Namval_t *np,register struct lval *lvalue,int as register char *sub=0, *cp=(char*)np; register Namval_t *mp; Shell_t *shp = lvalue->shp; - int flags = HASH_NOSCOPE|HASH_SCOPE|HASH_BUCKET; int c=0,nosub = lvalue->nosub; Dt_t *sdict = (shp->st.real_fun? shp->st.real_fun->sdict:0); Dt_t *nsdict = (shp->namespace?nv_dict(shp->namespace):0); @@ -100,10 +99,13 @@ static Namval_t *scope(register Namval_t *np,register struct lval *lvalue,int as flag = 0; cp = (char*)np; } - else if(assign==NV_ASSIGN && nv_isnull(np) && !nv_isattr(np, ~(NV_MINIMAL|NV_NOFREE))) - flags |= NV_ADD; - if((lvalue->emode&ARITH_COMP) && dtvnext(root) && ((sdict && (mp=nv_search(cp,sdict,flags&~NV_ADD))) || (mp=nv_search(cp,root,flags&~(NV_ADD))) || (nsdict && (mp=nv_search(cp,nsdict,flags&~(NV_ADD|HASH_NOSCOPE)))) )) - np = mp; + if((lvalue->emode & ARITH_COMP) && dtvnext(root)) + { + if(mp = nv_search(cp, sdict ? sdict : root, HASH_NOSCOPE|HASH_SCOPE|HASH_BUCKET)) + np = mp; + else if(nsdict && (mp = nv_search(cp, nsdict, HASH_SCOPE|HASH_BUCKET))) + np = mp; + } while(nv_isref(np)) { #if SHOPT_FIXEDARRAY