"read_pattern" could lock when reading a lookahead from stdin.

This commit is contained in:
Roberto Ierusalimschy
1999-04-14 17:40:32 -03:00
parent b4ad600b93
commit d9d04a9274

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 1.36 1999/03/26 13:48:26 roberto Exp roberto $ ** $Id: liolib.c,v 1.37 1999/04/05 19:47:05 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -221,9 +221,17 @@ static void io_appendto (void) {
** ======================================================= ** =======================================================
*/ */
/*
** We cannot lookahead without need, because this can lock stdin.
** This flag signals when we need to read a next char.
*/
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
static int read_pattern (FILE *f, char *p) { static int read_pattern (FILE *f, char *p) {
int inskip = 0; /* {skip} level */ int inskip = 0; /* {skip} level */
int c = getc(f); int c = NEED_OTHER;
while (*p != '\0') { while (*p != '\0') {
switch (*p) { switch (*p) {
case '{': case '{':
@@ -238,6 +246,7 @@ static int read_pattern (FILE *f, char *p) {
default: { default: {
char *ep; /* get what is next */ char *ep; /* get what is next */
int m; /* match result */ int m; /* match result */
if (c == NEED_OTHER) c = getc(f);
if (c != EOF) if (c != EOF)
m = luaI_singlematch(c, p, &ep); m = luaI_singlematch(c, p, &ep);
else { else {
@@ -246,7 +255,7 @@ static int read_pattern (FILE *f, char *p) {
} }
if (m) { if (m) {
if (!inskip) luaL_addchar(c); if (!inskip) luaL_addchar(c);
c = getc(f); c = NEED_OTHER;
} }
switch (*ep) { switch (*ep) {
case '*': /* repetition */ case '*': /* repetition */
@@ -256,17 +265,14 @@ static int read_pattern (FILE *f, char *p) {
p = ep+1; /* continues reading the pattern */ p = ep+1; /* continues reading the pattern */
continue; continue;
default: default:
if (!m) { /* pattern fails? */ if (!m) goto break_while; /* pattern fails? */
ungetc(c, f); p = ep; /* else continues reading the pattern */
return 0;
}
p = ep; /* continues reading the pattern */
} }
} }
} }
} } break_while:
ungetc(c, f); if (c != NEED_OTHER) ungetc(c, f);
return 1; return (*p == '\0');
} }