aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-08-25 16:58:08 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2009-08-25 16:58:08 -0300
commitf096ab5421e09acf3cb70661862b69fa100bff80 (patch)
treebcae570f3583cc5cdbdb11416be6720eae7b64d1
parent4e1ffc482a7eadde2cace3102a8d22c5baa4d7a9 (diff)
downloadlua-f096ab5421e09acf3cb70661862b69fa100bff80.tar.gz
lua-f096ab5421e09acf3cb70661862b69fa100bff80.tar.bz2
lua-f096ab5421e09acf3cb70661862b69fa100bff80.zip
correct way to check arguments to 'strftime'
-rw-r--r--loslib.c44
-rw-r--r--luaconf.h20
2 files changed, 41 insertions, 23 deletions
diff --git a/loslib.c b/loslib.c
index 643fa9c1..6286dd75 100644
--- a/loslib.c
+++ b/loslib.c
@@ -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
124static 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
124static int os_date (lua_State *L) { 148static 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 }
diff --git a/luaconf.h b/luaconf.h
index 334ee17c..1358633d 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.107 2009/07/15 17:26:14 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.108 2009/07/15 17:57:30 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -667,14 +667,18 @@ union luai_Cast { double l_d; long l_l; };
667 667
668 668
669/* 669/*
670@@ LUA_STRFTIMEOPTIONS is the list of valid conversion specifier 670@@ LUA_STRFTIMEOPTIONS is the list of valid conversion specifiers
671@* characters for the 'strftime' function; 671@* for the 'strftime' function;
672@@ LUA_STRFTIMEPREFIX is the list of valid modifiers for 672** CHANGE it if you want to use non-ansi options specific to your system.
673@* that function.
674** CHANGE them if you want to use non-ansi options specific to your system.
675*/ 673*/
676#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYz%" 674#if !defined(LUA_USE_POSIX)
677#define LUA_STRFTIMEPREFIX "" 675#define LUA_STRFTIMEOPTIONS "aAbBcdHIjmMpSUwWxXyYz%", ""
676
677#else
678#define LUA_STRFTIMEOPTIONS "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "", \
679 "E", "cCxXyY", \
680 "O", "deHImMSuUVwWy"
681#endif
678 682
679 683
680 684