1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-15 04:32:24 +00:00

Fix oddly specific syntax error corrupting subsequent [[ ... ]]

Reproducer:

    $ x=([x]=1 [y)
    -ksh: syntax error: `)' unexpected
    $ [[ -z $x ]]
    -ksh: [[ -z  ]]: not found

Any '[[' command following that syntax error will fail similarly;
the whole of it (after variable expansion) is incorrectly looked up
as a command name. The syntax error must be generated by an
associative array assignment (with or without an explicit typeset
-A) with at least one valid assignment element followed by an
invalid assignment element starting with '[' but not containing
']='.

This seems to be another bug that is in every ksh93 version ever.
I've confirmed that ksh 1993-12-28 s+ and ksh2020 fail identically.
Presumably, so does everything in between.

Analysis:

The syntax error function, sh_syntax(), calls lexopen() in mode 0
to reset the lexer state. There is a variable that isn't getting
reset there though it should be. Using systematic elimination I
found that the variable that needs to be reset is lp->assignok (set
"when name=value is legal"). If it is set, '[[' is not processed.

src/cmd/ksh93/sh/lex.c: lexopen():
- Reset 'assignok' in the lexer state (regardless of mode).
- In the mode 0 total lexer state reinit, several members of lexd
  (struct _shlex_pvt_lexdata_) were not getting reset; just memset
  the whole thing to zero.
     Note for backporters: this change requires commit da97587e to
  be correct. That commit took the stack size and pointer (lex_max
  and *lex_match) out of this struct; those should not be reset!

Resolves: https://github.com/ksh93/ksh/issues/486
This commit is contained in:
Martijn Dekker 2022-07-09 22:40:25 +02:00
parent 9ce426a8c4
commit 1934686de3
4 changed files with 18 additions and 13 deletions

5
NEWS
View file

@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
Any uppercase BUG_* names are modernish shell bug IDs.
2022-07-09:
- Fixed a bug that broke '[[ ... ]]' test expressions for the command
following a syntax error in an associative array assignment.
2022-07-05:
- Fixed a spurious syntax error on encountering a process substitution

View file

@ -23,7 +23,7 @@
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2022-07-05" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2022-07-09" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

View file

@ -208,20 +208,11 @@ Lex_t *sh_lexopen(Lex_t *lp, int mode)
if(!lp)
lp = (Lex_t*)sh_newof(0,Lex_t,1,0);
fcnotify(lex_advance,lp);
lp->lex.intest = lp->lex.incase = lp->lex.skipword = lp->lexd.warn = 0;
lp->comp_assign = 0;
lp->lex.intest = lp->lex.incase = lp->lex.skipword = lp->comp_assign = lp->comsub = lp->assignok = 0;
lp->lex.reservok = 1;
if(!sh_isoption(SH_DICTIONARY) && sh_isoption(SH_NOEXEC))
lp->lexd.warn=1;
if(!mode)
{
lp->lexd.noarg = lp->lexd.level= lp->lexd.dolparen = 0;
lp->lexd.nocopy = lp->lexd.docword = lp->lexd.nest = lp->lexd.paren = 0;
lp->lexd.lex_state = lp->lexd.lastc=0;
lp->lexd.docend = 0;
lp->lexd.nested_tilde = 0;
}
lp->comsub = 0;
memset(&lp->lexd,0,sizeof(struct _shlex_pvt_lexdata_));
lp->lexd.warn = !sh_isoption(SH_DICTIONARY) && sh_isoption(SH_NOEXEC);
return(lp);
}

View file

@ -494,5 +494,14 @@ $SHELL -c '[[ c < b ]]'; got=$?
((exp == got)) || err_exit "[[ ... ]] cannot handle 'c < b'" \
"(expected $exp, got $got)"
# ======
# https://github.com/ksh93/ksh/issues/486
unset x
savePATH=$PATH
PATH=/dev/null
command eval 'x=([x]=1 [y)' 2>/dev/null
[[ -z $x ]] 2>/dev/null || err_exit "[[ ... ]] breaks after syntax error in associative array assignment (got status $?)"
PATH=$savePATH
# ======
exit $((Errors<125?Errors:125))