diff options
| -rw-r--r-- | liolib.c | 21 |
1 files changed, 11 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.109 2013/03/16 21:10:18 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.110 2013/03/20 19:40:07 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 | */ |
| @@ -32,9 +32,15 @@ | |||
| 32 | #if !defined(lua_checkmode) | 32 | #if !defined(lua_checkmode) |
| 33 | 33 | ||
| 34 | /* | 34 | /* |
| 35 | ** this macro can accept other 'modes' for 'fopen' besides the standard ones | 35 | ** Check whether 'mode' matches '[rwa]%+?b?'. |
| 36 | ** Change this macro to accept other modes for 'fopen' besides | ||
| 37 | ** the standard ones. | ||
| 36 | */ | 38 | */ |
| 37 | #define lua_checkmode(mode) 0 | 39 | #define lua_checkmode(mode) \ |
| 40 | (*mode != '\0' && strchr("rwa", *(mode++)) != NULL && \ | ||
| 41 | (*mode != '+' || ++mode) && /* skip if char is '+' */ \ | ||
| 42 | (*mode != 'b' || ++mode) && /* skip if char is 'b' */ \ | ||
| 43 | (*mode == '\0')) | ||
| 38 | 44 | ||
| 39 | #endif | 45 | #endif |
| 40 | 46 | ||
| @@ -220,13 +226,8 @@ static int io_open (lua_State *L) { | |||
| 220 | const char *filename = luaL_checkstring(L, 1); | 226 | const char *filename = luaL_checkstring(L, 1); |
| 221 | const char *mode = luaL_optstring(L, 2, "r"); | 227 | const char *mode = luaL_optstring(L, 2, "r"); |
| 222 | LStream *p = newfile(L); | 228 | LStream *p = newfile(L); |
| 223 | int i = 0; | 229 | const char *md = mode; /* to traverse/check mode */ |
| 224 | /* check whether 'mode' matches '[rwa]%+?b?' */ | 230 | luaL_argcheck(L, lua_checkmode(md), 2, "invalid mode"); |
| 225 | if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL && | ||
| 226 | (mode[i] != '+' || ++i) && /* skip if char is '+' */ | ||
| 227 | (mode[i] != 'b' || ++i) && /* skip if char is 'b' */ | ||
| 228 | (mode[i] == '\0')) && !lua_checkmode(mode)) | ||
| 229 | return luaL_argerror(L, 2, "invalid mode"); | ||
| 230 | p->f = fopen(filename, mode); | 231 | p->f = fopen(filename, mode); |
| 231 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; | 232 | return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; |
| 232 | } | 233 | } |
