Skip to content

Commit

Permalink
lex.c: sh_lexopen(): future-proof full reset (re: 0dc0e1a)
Browse files Browse the repository at this point in the history
When fully resetting the lexer state (mode==0), zero out the
complete lexer struct, except for $LINENO-related members. This
might prevent future bugs.
McDutchie committed Nov 3, 2024
1 parent 2010caa commit 2e000c9
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@

#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2024-11-02" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2024-11-03" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK

/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
19 changes: 12 additions & 7 deletions src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
@@ -202,13 +202,18 @@ static int lexfill(Lex_t *lp)
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->comp_assign = lp->comsub = lp->assignok = 0;
lp = sh_newof(0,Lex_t,1,0);
else if(mode)
lp->lex.intest = lp->lex.incase = lp->lex.skipword = lp->comp_assign = lp->comsub = lp->assignok = 0;
else
{ /* full reset: everything except LINENO */
int lastline = lp->lastline, inlineno = lp->inlineno, firstline = lp->firstline;
memset(lp,0,sizeof(Lex_t));
lp->lastline = lastline, lp->inlineno = inlineno, lp->firstline = firstline;
}
lp->lex.reservok = 1;
if(!mode)
memset(&lp->lexd,0,sizeof(struct _shlex_pvt_lexdata_));
lp->lexd.warn = !sh_isoption(SH_DICTIONARY) && sh_isoption(SH_NOEXEC);
fcnotify(lex_advance,lp);
return lp;
}

@@ -2112,8 +2117,6 @@ noreturn void sh_syntax(Lex_t *lp, int special)
fcclose();
sh.inlineno = lp->inlineno;
sh.st.firstline = lp->firstline;
/* reset lexer state */
sh_lexopen(lp, 0);
/* construct error message */
if (sh_isstate(SH_INTERACTIVE) || sh_isstate(SH_PROFILE))
sfprintf(sh.strbuf, sh_translate(e_syntaxerror));
@@ -2129,6 +2132,8 @@ noreturn void sh_syntax(Lex_t *lp, int special)
sfprintf(sh.strbuf, sh_translate(e_unmatched), fmttoken(lp, lp->lasttok));
else
sfprintf(sh.strbuf, sh_translate(e_unexpected), fmttoken(lp, lp->token));
/* reset lexer state */
sh_lexopen(lp, 0);
errormsg(SH_DICT, ERROR_exit(SYNBAD), "%s", sfstruse(sh.strbuf));
UNREACHABLE();
}

0 comments on commit 2e000c9

Please sign in to comment.