Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit d5ce3b0

Browse files
committed
Simplify scope()
The `scope()` function doesn't need its `flags` variable. Nor is there any point in merging `NV_ADD` into its value since the only places `flags` is used explicitly exclude that bit mask. Finally, simplify the hard to understand compound `if()` expresssion. This function is probably broken in some subtle fashion but this change at least clarifies the code. Related #1072
1 parent 1bb46b7 commit d5ce3b0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/cmd/ksh93/sh/arith.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ static_fn Namval_t *scope(Namval_t *np, struct lval *lvalue, int assign) {
382382
char *sub = 0, *cp = (char *)np;
383383
Namval_t *mp;
384384
Shell_t *shp = lvalue->shp;
385-
int flags = HASH_NOSCOPE | HASH_SCOPE | HASH_BUCKET;
386385
int c = 0, nosub = lvalue->nosub;
387386
Dt_t *sdict = (shp->st.real_fun ? shp->st.real_fun->sdict : 0);
388387
Dt_t *nsdict = (shp->namespace ? nv_dict(shp->namespace) : 0);
@@ -422,19 +421,20 @@ static_fn Namval_t *scope(Namval_t *np, struct lval *lvalue, int assign) {
422421
flag = 0;
423422
}
424423
cp = (char *)np;
425-
} else if (assign == NV_ASSIGN && nv_isnull(np) && !nv_isattr(np, ~(NV_MINIMAL | NV_NOFREE))) {
426-
flags |= NV_ADD;
427424
}
428-
if ((lvalue->emode & ARITH_COMP) && dtvnext(root) &&
429-
((sdict && (mp = nv_search(cp, sdict, flags & ~NV_ADD))) ||
430-
(mp = nv_search(cp, root, flags & ~(NV_ADD))) ||
431-
(nsdict && (mp = nv_search(cp, nsdict, flags & ~(NV_ADD | HASH_NOSCOPE))))))
432-
np = mp;
425+
426+
if ((lvalue->emode & ARITH_COMP) && dtvnext(root)) {
427+
mp = nv_search(cp, sdict ? sdict : root, HASH_NOSCOPE | HASH_SCOPE | HASH_BUCKET);
428+
if (!mp && nsdict) mp = nv_search(cp, nsdict, HASH_SCOPE | HASH_BUCKET);
429+
if (mp) np = mp;
430+
}
431+
433432
while (nv_isref(np)) {
434433
sub = nv_refsub(np);
435434
np = nv_refnode(np);
436435
if (sub) nv_putsub(np, sub, 0, assign == NV_ASSIGN ? ARRAY_ADD : 0);
437436
}
437+
438438
if (!nosub && flag) {
439439
int hasdot = 0;
440440
cp = (char *)&lvalue->expr[flag];

0 commit comments

Comments
 (0)