aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-08-28 10:51:57 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-08-28 10:51:57 -0300
commit5b6be84106f41752624b9ee66dbd9d21eda7e0ce (patch)
tree2508e6a296582e1358cb8a94ec0470e13ab01f63
parentb2bb2f7f592c0dfaa3a2aa523a97597d49c2252c (diff)
downloadlua-5b6be84106f41752624b9ee66dbd9d21eda7e0ce.tar.gz
lua-5b6be84106f41752624b9ee66dbd9d21eda7e0ce.tar.bz2
lua-5b6be84106f41752624b9ee66dbd9d21eda7e0ce.zip
ensures that argument 'mode' to 'io.open' matches "[rwa]%+?b?", to
avoid passing invalid modes to 'fopen'.
-rw-r--r--liolib.c13
1 files 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 @@
1/* 1/*
2** $Id: liolib.c,v 2.79 2008/02/12 17:05:36 roberto Exp roberto $ 2** $Id: liolib.c,v 2.80 2009/02/20 13:50:27 roberto Exp roberto $
3** Standard I/O (and system) library 3** Standard I/O (and system) library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -165,7 +165,16 @@ static int io_tostring (lua_State *L) {
165static int io_open (lua_State *L) { 165static int io_open (lua_State *L) {
166 const char *filename = luaL_checkstring(L, 1); 166 const char *filename = luaL_checkstring(L, 1);
167 const char *mode = luaL_optstring(L, 2, "r"); 167 const char *mode = luaL_optstring(L, 2, "r");
168 FILE **pf = newfile(L); 168 FILE **pf;
169 int i = 0;
170 /* check whether 'mode' matches '[rwa]%+?b?' */
171 if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
172 (mode[i] != '+' || ++i) && /* skip if char is '+' */
173 (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
174 (mode[i] == '\0')))
175 luaL_error(L, "invalid mode " LUA_QL("%s")
176 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
177 pf = newfile(L);
169 *pf = fopen(filename, mode); 178 *pf = fopen(filename, mode);
170 return (*pf == NULL) ? pushresult(L, 0, filename) : 1; 179 return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
171} 180}