[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua bug - loadfile gets stdin confused
- From: "Mark Edgar" <medgar123@...>
- Date: Tue, 17 Apr 2007 11:00:18 -0700
The obvious (?) fix to this bug:
--- - 2007-04-17 10:46:22.525585000 -0700
+++ lua-5.1.2/src/lauxlib.c 2007-04-17 10:36:38.000000000 -0700
@@ -554,8 +554,9 @@
int status, readstatus;
int c;
int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */
+ int usestdin = filename == NULL;
lf.extraline = 0;
- if (filename == NULL) {
+ if (usestdin) {
lua_pushliteral(L, "=stdin");
lf.f = stdin;
}
@@ -570,7 +571,7 @@
while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
if (c == '\n') c = getc(lf.f);
}
- if (c == LUA_SIGNATURE[0] && lf.f != stdin) { /* binary file? */
+ if (c == LUA_SIGNATURE[0] && !usestdin) { /* binary file? */
fclose(lf.f);
lf.f = fopen(filename, "rb"); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
@@ -581,7 +582,7 @@
ungetc(c, lf.f);
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
readstatus = ferror(lf.f);
- if (lf.f != stdin) fclose(lf.f); /* close file (even in case of errors) */
+ if (!usestdin) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) {
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
return errfile(L, "read", fnameindex);
Also, I'm certain that the same bug lies in wait here too, and the fix
isn't nearly as obvious:
http://www.lua.org/source/5.1/liolib.c.html#io_gc
-Mark