[BUG] C-undefined behavior exposed via io.popen mode parameter

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

[BUG] C-undefined behavior exposed via io.popen mode parameter

Viacheslav Usov
The IO library's popen function passes its second parameter, if given,
unmodified to either POSIX popen() or MSVC _popen(). In the first
case, any value except "r" and "w" is explicitly documented to cause
undefined behavior. In the second case, more values are permitted, and
nothing is said about the behavior when other values are used, which
normally means again undefined behavior.

I have seen that io.popen("foo", "r+") crashes the host at least with
some versions of MSVC on some versions of Windows. I do not think it
is correct that a function callable from Lua should be able to cause
the C language undefined behavior, so I'd say the IO library should
not allow arbitrary arguments for the mode parameter.

That was found in Lua 5.3, and looking at the code on Github, will
apply to Lua 5.4.