lua-users home
lua-l archive

Re: lua bug - loadfile gets stdin confused

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


David Manura <dm.gmane <at> math2.org> writes:
> The following code demonstrates what seems to be a bug in Lua 5.1.2.
> It affects at least Win32.
>
> io.input():close() -- breaks it
> --io.input(io.open("NUL")) -- unbreaks it
> loadfile("1.lua") -- does not close file!
> -- culprit in lauxlib.c:luaL_loadfile:
> -- if (lf.f != stdin) fclose(lf.f);
> collectgarbage() -- no help
> ...
> os.execute("notepad") -- pause to allow user to test above
It's more directly seen with this test case:
local f = io.open("1.lua", "w")
f:write("print('test')")
f:close()
io.input():close() -- breaks it
--local f = io.open("NUL") -- unbreaks it
local f = io.open("1.lua") -- overwrites C stdin !
dofile() -- prints "test" !
-- culprit is "lf.f = stdin;" in lauxlib.c:luaL_loadfile
Here's a similar example in C:
#include <stdio.h>
int main()
{
 FILE * f;
 int c;
 fclose(stdin);
 f = fopen("in.txt", "r");
 while((c = fgetc(stdin)) != -1) fputc(c, stdout); /* prints in.txt ! */
 return 0;
}

AltStyle によって変換されたページ (->オリジナル) /