From 5b6be84106f41752624b9ee66dbd9d21eda7e0ce Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 28 Aug 2009 10:51:57 -0300 Subject: [PATCH] ensures that argument 'mode' to 'io.open' matches "[rwa]%+?b?", to avoid passing invalid modes to 'fopen'. --- liolib.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/liolib.c b/liolib.c index 1978c510..8a543a81 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.79 2008/02/12 17:05:36 roberto Exp roberto $ +** $Id: liolib.c,v 2.80 2009/02/20 13:50:27 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -165,7 +165,16 @@ static int io_tostring (lua_State *L) { static int io_open (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); - FILE **pf = newfile(L); + FILE **pf; + int i = 0; + /* check whether 'mode' matches '[rwa]%+?b?' */ + if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL && + (mode[i] != '+' || ++i) && /* skip if char is '+' */ + (mode[i] != 'b' || ++i) && /* skip if char is 'b' */ + (mode[i] == '\0'))) + luaL_error(L, "invalid mode " LUA_QL("%s") + " (should match " LUA_QL("[rwa]%%+?b?") ")", mode); + pf = newfile(L); *pf = fopen(filename, mode); return (*pf == NULL) ? pushresult(L, 0, filename) : 1; }