diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-15 16:01:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-07-15 16:01:03 -0300 |
commit | 1ecfbfa1a1debd2258decdf7c1954ac6f9761699 (patch) | |
tree | a03e232e5c6a6c9cf6b4fe7b5b31d73a614ad26a | |
parent | e1d8770f12542d34a3e32b825c95b93f8a341ee1 (diff) | |
download | lua-1ecfbfa1a1debd2258decdf7c1954ac6f9761699.tar.gz lua-1ecfbfa1a1debd2258decdf7c1954ac6f9761699.tar.bz2 lua-1ecfbfa1a1debd2258decdf7c1954ac6f9761699.zip |
Fixed bug: invalid mode can crash 'io.popen'
-rw-r--r-- | liolib.c | 7 | ||||
-rw-r--r-- | testes/files.lua | 15 |
2 files changed, 22 insertions, 0 deletions
@@ -52,6 +52,12 @@ static int l_checkmode (const char *mode) { | |||
52 | ** ======================================================= | 52 | ** ======================================================= |
53 | */ | 53 | */ |
54 | 54 | ||
55 | #if !defined(l_checkmodep) | ||
56 | /* By default, Lua accepts only "r" or "w" as mode */ | ||
57 | #define l_checkmodep(m) ((m[0] == 'r' || m[0] == 'w') && m[1] == '\0') | ||
58 | #endif | ||
59 | |||
60 | |||
55 | #if !defined(l_popen) /* { */ | 61 | #if !defined(l_popen) /* { */ |
56 | 62 | ||
57 | #if defined(LUA_USE_POSIX) /* { */ | 63 | #if defined(LUA_USE_POSIX) /* { */ |
@@ -279,6 +285,7 @@ static int io_popen (lua_State *L) { | |||
279 | const char *filename = luaL_checkstring(L, 1); | 285 | const char *filename = luaL_checkstring(L, 1); |
280 | const char *mode = luaL_optstring(L, 2, "r"); | 286 | const char *mode = luaL_optstring(L, 2, "r"); |
281 | LStream *p = newprefile(L); | 287 | LStream *p = newprefile(L); |
288 | luaL_argcheck(L, l_checkmodep(mode), 2, "invalid mode"); | ||
282 | p->f = l_popen(L, filename, mode); | 289 | p->f = l_popen(L, filename, mode); |
283 | p->closef = &io_pclose; | 290 | p->closef = &io_pclose; |
284 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | 291 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
diff --git a/testes/files.lua b/testes/files.lua index 677c0dc2..16cf9b6a 100644 --- a/testes/files.lua +++ b/testes/files.lua | |||
@@ -721,6 +721,21 @@ if not _port then | |||
721 | progname = '"' .. arg[i + 1] .. '"' | 721 | progname = '"' .. arg[i + 1] .. '"' |
722 | end | 722 | end |
723 | print("testing popen/pclose and execute") | 723 | print("testing popen/pclose and execute") |
724 | -- invalid mode for popen | ||
725 | checkerr("invalid mode", io.popen, "cat", "") | ||
726 | checkerr("invalid mode", io.popen, "cat", "r+") | ||
727 | checkerr("invalid mode", io.popen, "cat", "rw") | ||
728 | do -- basic tests for popen | ||
729 | local file = os.tmpname() | ||
730 | local f = assert(io.popen("cat - > " .. file, "w")) | ||
731 | f:write("a line") | ||
732 | assert(f:close()) | ||
733 | local f = assert(io.popen("cat - < " .. file, "r")) | ||
734 | assert(f:read("a") == "a line") | ||
735 | assert(f:close()) | ||
736 | assert(os.remove(file)) | ||
737 | end | ||
738 | |||
724 | local tests = { | 739 | local tests = { |
725 | -- command, what, code | 740 | -- command, what, code |
726 | {"ls > /dev/null", "ok"}, | 741 | {"ls > /dev/null", "ok"}, |