Skip to content

Commit 8d57369

Browse files
committed
Fix buffer overflow in sh_lex()
This macro expansion in lex.c may assign -1 to n if EOF is reached: 1178: fcgetc(n); As a result, n may be -1 when this code is reached: 1190: if(sh_isoption(SH_BRACEEXPAND) && c==LBRACE && !assignment && state[n]!=S_BREAK 'state[n]' is a buffer overflow if n==-1. src/cmd/ksh93/sh/lex.c: sh_lex(): case S_BRACE: - Apart from the buffer overflow, if n<=0, none of the code following fcget(n) does anything until 'break' on line 1199 is reached. So, if fcget(n) yields <=0, just break. This allows some code simplification. Progresses: ksh93/ksh#518
1 parent 59a5672 commit 8d57369

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/cmd/ksh93/sh/lex.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,14 +1175,12 @@ int sh_lex(Lex_t* lp)
11751175
goto do_reg;
11761176
}
11771177
isfirst = (lp->lexd.first&&fcseek(0)==lp->lexd.first+1);
1178-
fcgetc(n);
1178+
if(fcgetc(n)<=0)
1179+
break;
11791180
/* check for {} */
11801181
if(c==LBRACE && n==RBRACE)
11811182
break;
1182-
if(n>0)
1183-
fcseek(-LEN);
1184-
else if(lp->lex.reservok)
1185-
break;
1183+
fcseek(-LEN);
11861184
/* check for reserved word { or } */
11871185
if(lp->lex.reservok && state[n]==S_BREAK && isfirst)
11881186
break;

0 commit comments

Comments
 (0)