Skip to content

Commit 6cdd820

Browse files
committed
bugfix: io module: ensured read() resumes read in all modes upon EINTR.
1 parent eb103ae commit 6cdd820

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

src/lib_io.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,46 @@ static int io_file_close(lua_State *L, IOFileUD *iof)
126126
static int io_file_readnum(lua_State *L, FILE *fp)
127127
{
128128
lua_Number d;
129-
if (fscanf(fp, LUA_NUMBER_SCAN, &d) == 1) {
130-
if (LJ_DUALNUM) {
131-
int32_t i = lj_num2int(d);
132-
if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) {
133-
setintV(L->top++, i);
134-
return 1;
135-
}
129+
int rc;
130+
for (;;) {
131+
rc = fscanf(fp, LUA_NUMBER_SCAN, &d);
132+
if (rc == 1) break;
133+
if (ferror(fp)) {
134+
if (errno == EINTR) { clearerr(fp); continue; }
135+
return 0;
136+
} else {
137+
setnilV(L->top++);
138+
return 0;
139+
}
140+
}
141+
if (LJ_DUALNUM) {
142+
int32_t i = lj_num2int(d);
143+
if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) {
144+
setintV(L->top++, i);
145+
return 1;
136146
}
137-
setnumV(L->top++, d);
138-
return 1;
139-
} else {
140-
setnilV(L->top++);
141-
return 0;
142147
}
148+
setnumV(L->top++, d);
149+
return 1;
143150
}
144151

145152
static int io_file_readline(lua_State *L, FILE *fp, MSize chop)
146153
{
147-
MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0;
148-
char *buf;
154+
MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0, s = 0;
155+
char *buf, *rc;
149156
for (;;) {
150-
buf = lj_buf_tmp(L, m);
151-
if (fgets(buf+n, m-n, fp) == NULL) break;
152-
n += (MSize)strlen(buf+n);
157+
buf = lj_buf_tmp(L, m) + n;
158+
s = m - n;
159+
for (;;) {
160+
rc = fgets(buf, s, fp);
161+
if (rc != NULL) break;
162+
if (ferror(fp)) {
163+
if (errno == EINTR) { clearerr(fp); continue; }
164+
lj_gc_check(L);
165+
return 0;
166+
}
167+
}
168+
n += (MSize)strlen(buf);
153169
ok |= n;
154170
if (n && buf[n-1] == '\n') { n -= chop; break; }
155171
if (n >= m - 64) m += m;
@@ -182,8 +198,16 @@ static int io_file_readlen(lua_State *L, FILE *fp, MSize m)
182198
{
183199
if (m) {
184200
char *buf = lj_buf_tmp(L, m);
185-
MSize n = (MSize)fread(buf, 1, m, fp);
186-
setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));
201+
MSize n;
202+
for (;;) {
203+
n = (MSize)fread(buf, 1, m, fp);
204+
if (ferror(fp)) {
205+
if (errno == EINTR) { clearerr(fp); continue; }
206+
} else {
207+
setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n));
208+
}
209+
break;
210+
}
187211
lj_gc_check(L);
188212
return (n > 0 || m == 0);
189213
} else {

0 commit comments

Comments
 (0)