Skip to content

Commit

Permalink
Avoid printing a [0] element for ${!.sh.match} (#709)
Browse files Browse the repository at this point in the history
This fix prevents name_match() in init.c from printing a
spurious extra subscript when using ${!.sh.match}.

src/cmd/ksh93/sh/init.c:
- Remove the name_match() function and roll SH_MATCH_disc
  back to the 93u+ version.

Resolves: #308
  • Loading branch information
JohnoKing authored Jan 25, 2024
1 parent 12474b0 commit d5e45f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
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 spurious elements.

2024-01-22:

- Fixed a bug in the loop invariants optimizer (SHOPT_OPTIMIZE) that caused
Expand Down
10 changes: 1 addition & 9 deletions src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,15 +1012,7 @@ static char* get_match(Namval_t* np, Namfun_t *fp)
return mp->rval[i];
}

static char *name_match(Namval_t *np, Namfun_t *fp)
{
int sub = nv_aindex(SH_MATCHNOD);
sfprintf(sh.strbuf,".sh.match[%d]",sub);
return sfstruse(sh.strbuf);
}

static const Namdisc_t SH_MATCH_disc = { sizeof(struct match), 0, get_match,
0,0,0,0,name_match };
static const Namdisc_t SH_MATCH_disc = { sizeof(struct match), 0, get_match };

static char* get_version(Namval_t* np, Namfun_t *fp)
{
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/ksh93/tests/sh_match.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1072,5 +1072,13 @@ fi
[[ "[a] b [c] d" =~ ^\[[^]]+\] ]]
[[ ${.sh.match} == '[a]' ]] || err_exit 'pattern ^\[[^]]+ broken'

# Avoid printing excessive elements for .sh.match
# https://github.com/ksh93/ksh/issues/308#issuecomment-1033259414
# https://github.com/ksh93/ksh/pull/709
exp='.sh.match .sh.match[1] .sh.match[2]'
got=${ $SHELL -c 'print ${!.sh.match} ${!.sh.match[1]} ${!.sh.match[2]}' }
[[ $exp == "$got" ]] || err_exit "'print \${!.sh.match}' should not print excessive elements" \
"(expected ${ printf %q "$exp" }, got ${ printf %q "$got" })"

# ======
exit $((Errors<125?Errors:125))

0 comments on commit d5e45f4

Please sign in to comment.