Skip to content

Commit

Permalink
tilde_expand2(): simplify (re: 1829e86)
Browse files Browse the repository at this point in the history
The nv_putval() and nv_getval() commands are guaranteed to restore
the stack, directly or indirectly. If a discipline shell function
gets executed, the stack is restored by sh_exec() after executing
(see xec.c lines 2633-2636). So the code block handling the
.sh.tilde.* disciplines does not need to.

After this tweak, the tilde.sh regression tests still pass,
including when compiled with ASan.
  • Loading branch information
McDutchie committed Jan 23, 2024
1 parent 1829e86 commit a8ca65f
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -2706,25 +2706,23 @@ static void tilde_expand2(int offset)
char *cp = NULL; /* character pointer for tilde expansion result */
char *stakp = stkptr(sh.stk,0); /* current stack object (&stakp[offset] is tilde string) */
int curoff = stktell(sh.stk); /* current offset of current stack object */
sfputc(sh.stk,0); /* terminate current stack object to avoid data corruption */
/*
* Allow overriding tilde expansion with a .sh.tilde.set or .get discipline function.
*/
if(!sh.tilde_block && SH_TILDENOD->nvfun && SH_TILDENOD->nvfun->disc)
{
sfputc(sh.stk,'\0'); /* terminate current stack object to avoid data corruption */
sh.tilde_block = 1;
nv_putval(SH_TILDENOD, &stakp[offset], 0);
cp = nv_getval(SH_TILDENOD);
sh.tilde_block = 0;
if(cp[0]=='\0' || cp[0]=='~')
cp = NULL; /* do not use empty or unexpanded result */
stkseek(sh.stk,curoff); /* restore stack to state on function entry */
}
/*
* Perform default tilde expansion unless overridden.
* Write the result to the stack, if any.
*/
sfputc(sh.stk,0);
if(!cp)
cp = sh_tilde(&stakp[offset]);
if(cp)
Expand Down

0 comments on commit a8ca65f

Please sign in to comment.