Skip to content

Commit

Permalink
Avoid printing a [0] element for .sh.match
Browse files Browse the repository at this point in the history
This long overdue bugfix prevents name_match() in init.c from printing a
spurious zero element when using ${!.sh.match}. I haven't been able to
break this bugfix during testing and it's one of the last remaining
issues with .sh.match, so it's about time this is submitted as a pull
request.

(See ksh93#308 (comment)
 and the related comments in the thread.)
  • Loading branch information
JohnoKing committed Jan 23, 2024
1 parent acaac0a commit 727f6fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library.
- Fixed a rare crash or rare incorrect behaviour in .sh.tilde.{get,set}
discipline functions for ~ expansion (see 2021-03-16 below).

- Fixed a bug that caused listing the .sh.match variable with ${!.sh.match} to
show a spurious zero element.

2024-01-22:

- Fixed a bug in the loop invariants optimizer (SHOPT_OPTIMIZE) that caused
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,10 @@ static char* get_match(Namval_t* np, Namfun_t *fp)
static char *name_match(Namval_t *np, Namfun_t *fp)
{
int sub = nv_aindex(SH_MATCHNOD);
sfprintf(sh.strbuf,".sh.match[%d]",sub);
if(sub==0)
sfprintf(sh.strbuf,".sh.match");
else
sfprintf(sh.strbuf,".sh.match[%d]",sub);
return sfstruse(sh.strbuf);
}

Expand Down
7 changes: 7 additions & 0 deletions src/cmd/ksh93/tests/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1650,5 +1650,12 @@ done
unset i got bound
SRANDOM=0
# ======
# Avoid printing a [0] element for .sh.match
exp='.sh.match'
got=${ $SHELL -c 'print ${!.sh.match}' }
[[ $exp == "$got" ]] || err_exit "'print \${!.sh.match}' should not print a [0] subscript" \
"(expected ${ printf %q "$exp" }, got ${ printf %q "$got" })"
# ======
exit $((Errors<125?Errors:125))

0 comments on commit 727f6fe

Please sign in to comment.