mirror of
https://github.com/lua/lua
synced 2024-11-24 21:59:41 +03:00
Fixed bug: invalid mode can crash 'io.popen'
This commit is contained in:
parent
e1d8770f12
commit
1ecfbfa1a1
7
liolib.c
7
liolib.c
@ -52,6 +52,12 @@ static int l_checkmode (const char *mode) {
|
||||
** =======================================================
|
||||
*/
|
||||
|
||||
#if !defined(l_checkmodep)
|
||||
/* By default, Lua accepts only "r" or "w" as mode */
|
||||
#define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0')
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(l_popen) /* { */
|
||||
|
||||
#if defined(LUA_USE_POSIX) /* { */
|
||||
@ -279,6 +285,7 @@ static int io_popen (lua_State *L) {
|
||||
const char *filename = luaL_checkstring(L, 1);
|
||||
const char *mode = luaL_optstring(L, 2, "r");
|
||||
LStream *p = newprefile(L);
|
||||
luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode");
|
||||
p->f = l_popen(L, filename, mode);
|
||||
p->closef = &io_pclose;
|
||||
return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1;
|
||||
|
@ -721,6 +721,21 @@ if not _port then
|
||||
progname = '"' .. arg[i + 1] .. '"'
|
||||
end
|
||||
print("testing popen/pclose and execute")
|
||||
-- invalid mode for popen
|
||||
checkerr("invalid mode", io.popen, "cat", "")
|
||||
checkerr("invalid mode", io.popen, "cat", "r+")
|
||||
checkerr("invalid mode", io.popen, "cat", "rw")
|
||||
do -- basic tests for popen
|
||||
local file = os.tmpname()
|
||||
local f = assert(io.popen("cat - > " .. file, "w"))
|
||||
f:write("a line")
|
||||
assert(f:close())
|
||||
local f = assert(io.popen("cat - < " .. file, "r"))
|
||||
assert(f:read("a") == "a line")
|
||||
assert(f:close())
|
||||
assert(os.remove(file))
|
||||
end
|
||||
|
||||
local tests = {
|
||||
-- command, what, code
|
||||
{"ls > /dev/null", "ok"},
|
||||
|
Loading…
Reference in New Issue
Block a user