diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-08-25 16:58:08 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2009-08-25 16:58:08 -0300 |
| commit | f096ab5421e09acf3cb70661862b69fa100bff80 (patch) | |
| tree | bcae570f3583cc5cdbdb11416be6720eae7b64d1 /loslib.c | |
| parent | 4e1ffc482a7eadde2cace3102a8d22c5baa4d7a9 (diff) | |
| download | lua-f096ab5421e09acf3cb70661862b69fa100bff80.tar.gz lua-f096ab5421e09acf3cb70661862b69fa100bff80.tar.bz2 lua-f096ab5421e09acf3cb70661862b69fa100bff80.zip | |
correct way to check arguments to 'strftime'
Diffstat (limited to 'loslib.c')
| -rw-r--r-- | loslib.c | 44 |
1 files changed, 29 insertions, 15 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: loslib.c,v 1.23 2008/01/18 15:37:10 roberto Exp roberto $ | 2 | ** $Id: loslib.c,v 1.24 2008/06/13 16:59:00 roberto Exp roberto $ |
| 3 | ** Standard Operating System library | 3 | ** Standard Operating System library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -121,6 +121,30 @@ static int getfield (lua_State *L, const char *key, int d) { | |||
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | 123 | ||
| 124 | static const char *checkoption (lua_State *L, const char *conv, char *buff) { | ||
| 125 | static const char *const options[] = { LUA_STRFTIMEOPTIONS }; | ||
| 126 | unsigned int i; | ||
| 127 | for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) { | ||
| 128 | if (*conv != '\0' && strchr(options[i], *conv) != NULL) { | ||
| 129 | buff[1] = *conv; | ||
| 130 | if (*options[i + 1] == '\0') { /* one-char conversion specifier? */ | ||
| 131 | buff[2] = '\0'; /* end buffer */ | ||
| 132 | return conv + 1; | ||
| 133 | } | ||
| 134 | else if (*(conv + 1) != '\0' && | ||
| 135 | strchr(options[i + 1], *(conv + 1)) != NULL) { | ||
| 136 | buff[2] = *(conv + 1); /* valid two-char conversion specifier */ | ||
| 137 | buff[3] = '\0'; /* end buffer */ | ||
| 138 | return conv + 2; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | luaL_argerror(L, 1, | ||
| 143 | lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv)); | ||
| 144 | return conv; /* to avoid warnings */ | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 124 | static int os_date (lua_State *L) { | 148 | static int os_date (lua_State *L) { |
| 125 | const char *s = luaL_optstring(L, 1, "%c"); | 149 | const char *s = luaL_optstring(L, 1, "%c"); |
| 126 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); | 150 | time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); |
| @@ -146,27 +170,17 @@ static int os_date (lua_State *L) { | |||
| 146 | setboolfield(L, "isdst", stm->tm_isdst); | 170 | setboolfield(L, "isdst", stm->tm_isdst); |
| 147 | } | 171 | } |
| 148 | else { | 172 | else { |
| 149 | char cc[3]; | 173 | char cc[4]; |
| 150 | luaL_Buffer b; | 174 | luaL_Buffer b; |
| 151 | cc[0] = '%'; | 175 | cc[0] = '%'; |
| 152 | luaL_buffinit(L, &b); | 176 | luaL_buffinit(L, &b); |
| 153 | for (; *s; s++) { | 177 | while (*s) { |
| 154 | if (*s != '%') /* no conversion specifier? */ | 178 | if (*s != '%') /* no conversion specifier? */ |
| 155 | luaL_addchar(&b, *s); | 179 | luaL_addchar(&b, *s++); |
| 156 | else { | 180 | else { |
| 157 | size_t reslen; | 181 | size_t reslen; |
| 158 | int i = 1; | ||
| 159 | char buff[200]; /* should be big enough for any conversion result */ | 182 | char buff[200]; /* should be big enough for any conversion result */ |
| 160 | if (*(++s) != '\0' && strchr(LUA_STRFTIMEPREFIX, *s)) | 183 | s = checkoption(L, s + 1, cc); |
| 161 | cc[i++] = *(s++); | ||
| 162 | if (*s != '\0' && strchr(LUA_STRFTIMEOPTIONS, *s)) | ||
| 163 | cc[i++] = *s; | ||
| 164 | else { | ||
| 165 | const char *msg = lua_pushfstring(L, | ||
| 166 | "invalid conversion specifier '%%%c'", *s); | ||
| 167 | return luaL_argerror(L, 1, msg); | ||
| 168 | } | ||
| 169 | cc[i] = '\0'; | ||
| 170 | reslen = strftime(buff, sizeof(buff), cc, stm); | 184 | reslen = strftime(buff, sizeof(buff), cc, stm); |
| 171 | luaL_addlstring(&b, buff, reslen); | 185 | luaL_addlstring(&b, buff, reslen); |
| 172 | } | 186 | } |
