1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 19:52:20 +00:00

lex.c: simplify fmttoken() by using the stack (re: 3255aed2)

Using the stack makes it impossible for future buffer overflows to
occur. It also simplifies fmttoken() by eliminating the need to
declare a local buffer and pass a pointer to that as an argument.

For info: man src/lib/libast/man/stak.3
This commit is contained in:
Martijn Dekker 2021-04-09 17:27:36 +01:00
parent 0538719e07
commit cb67a01b45

View file

@ -122,7 +122,7 @@ struct lexdata
#define setchar(lp,c) (lp->lexd.lastc = ((lp->lexd.lastc&~0xff)|(c))) #define setchar(lp,c) (lp->lexd.lastc = ((lp->lexd.lastc&~0xff)|(c)))
#define poplevel(lp) (lp->lexd.lastc=lp->lexd.lex_match[--lp->lexd.level]) #define poplevel(lp) (lp->lexd.lastc=lp->lexd.lex_match[--lp->lexd.level])
static char *fmttoken(Lex_t*, int, char*); static char *fmttoken(Lex_t*, int);
static int alias_exceptf(Sfio_t*, int, void*, Sfdisc_t*); static int alias_exceptf(Sfio_t*, int, void*, Sfdisc_t*);
static void setupalias(Lex_t*,const char*, Namval_t*); static void setupalias(Lex_t*,const char*, Namval_t*);
static int comsub(Lex_t*,int); static int comsub(Lex_t*,int);
@ -289,7 +289,6 @@ int sh_lex(Lex_t *lp)
Shell_t *shp = lp->sh; Shell_t *shp = lp->sh;
register int flag; register int flag;
char *quoted, *macro, *split, *expand; char *quoted, *macro, *split, *expand;
char tokstr[4];
register int tok = lextoken(lp); register int tok = lextoken(lp);
quoted = macro = split = expand = ""; quoted = macro = split = expand = "";
if(tok==0 && (flag=lp->arg->argflag)) if(tok==0 && (flag=lp->arg->argflag))
@ -302,7 +301,7 @@ int sh_lex(Lex_t *lp)
quoted = "quoted:"; quoted = "quoted:";
} }
sfprintf(sfstderr,"%d: line %d: %o:%s%s%s%s %s\n",shgd->current_pid,shp->inlineno,tok,quoted, sfprintf(sfstderr,"%d: line %d: %o:%s%s%s%s %s\n",shgd->current_pid,shp->inlineno,tok,quoted,
macro, split, expand, fmttoken(lp,tok,tokstr)); macro, split, expand, fmttoken(lp,tok));
return(tok); return(tok);
} }
#define sh_lex lextoken #define sh_lex lextoken
@ -2042,9 +2041,8 @@ done:
/* /*
* generates string for given token * generates string for given token
*/ */
static char *fmttoken(Lex_t *lp, register int sym, char *tok) static char *fmttoken(Lex_t *lp, register int sym)
{ {
int n=1;
if(sym < 0) if(sym < 0)
return((char*)sh_translate(e_lexzerobyte)); return((char*)sh_translate(e_lexzerobyte));
if(sym==0) if(sym==0)
@ -2062,9 +2060,9 @@ static char *fmttoken(Lex_t *lp, register int sym, char *tok)
return((char*)sh_translate(e_endoffile)); return((char*)sh_translate(e_endoffile));
if(sym==NL) if(sym==NL)
return((char*)sh_translate(e_newline)); return((char*)sh_translate(e_newline));
tok[0] = sym; stakputc(sym);
if(sym&SYMREP) if(sym&SYMREP)
tok[n++] = sym; stakputc(sym);
else else
{ {
switch(sym&SYMMASK) switch(sym&SYMMASK)
@ -2085,17 +2083,16 @@ static char *fmttoken(Lex_t *lp, register int sym, char *tok)
sym = '#'; sym = '#';
break; break;
case SYMSEMI: case SYMSEMI:
if(tok[0]=='<') if(*stakptr(0)=='<')
tok[n++] = '>'; stakputc('>');
sym = ';'; sym = ';';
break; break;
default: default:
sym = 0; sym = 0;
} }
tok[n++] = sym; stakputc(sym);
} }
tok[n] = 0; return(stakfreeze(1));
return(tok);
} }
/* /*
@ -2108,7 +2105,6 @@ noreturn void sh_syntax(Lex_t *lp)
register const char *cp = sh_translate(e_unexpected); register const char *cp = sh_translate(e_unexpected);
register char *tokstr; register char *tokstr;
register int tok = lp->token; register int tok = lp->token;
char tokbuf[4];
Sfio_t *sp; Sfio_t *sp;
if((tok==EOFSYM) && lp->lasttok) if((tok==EOFSYM) && lp->lasttok)
{ {
@ -2117,7 +2113,7 @@ noreturn void sh_syntax(Lex_t *lp)
} }
else else
lp->lastline = shp->inlineno; lp->lastline = shp->inlineno;
tokstr = fmttoken(lp,tok,tokbuf); tokstr = fmttoken(lp,tok);
if((sp=fcfile()) || (shp->infd>=0 && (sp=shp->sftable[shp->infd]))) if((sp=fcfile()) || (shp->infd>=0 && (sp=shp->sftable[shp->infd])))
{ {
/* clear out any pending input */ /* clear out any pending input */