lua-users home
lua-l archive

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




On Wed, Mar 9, 2022 at 8:28 AM Pierre Chapuis <lua@catwell.info> wrote:
I came across this post, which discusses the lack of error validation in naive "Hello World" programs in different languages: https://blog.sunfishcode.online/bugs-in-hello-world/

Lua isn't mentioned but it would fail this author's test. I do not personally think it is an issue with the language, but to make it work in Lua one would have to write Hello World like this:

    io.stdout:write("Hello World!\n")
    assert(io.stdout:flush())


Standard output is not a 'tty', so the standard C library sets the file to "fully buffered". The actual "write" system call call gets delayed until either the buffer is full or "flush" is called. You can avoid this by using unbuffered I/O (io.stdout:setvbuf("no")). "write" will return an error if it actually ends up calling the "write" system call.

I/O errors that happen after the main program exits do not seem to be reported. I'd suggest always checking the return value of <file>:write, and calling <file>:flush (or <file>:close if appropriate) before exiting the program.