瀏覽代碼

'os.date' checks arguments before passing them to 'strftime'

Roberto Ierusalimschy 18 年之前
父節點
當前提交
e7fb0d8a6f
共有 2 個文件被更改,包括 27 次插入5 次删除
  1. 14 4
      loslib.c
  2. 13 1
      luaconf.h

+ 14 - 4
loslib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: loslib.c,v 1.19 2006/04/26 18:19:49 roberto Exp roberto $
+** $Id: loslib.c,v 1.20 2006/09/19 13:57:08 roberto Exp roberto $
 ** Standard Operating System library
 ** See Copyright Notice in lua.h
 */
@@ -148,15 +148,25 @@ static int os_date (lua_State *L) {
   else {
     char cc[3];
     luaL_Buffer b;
-    cc[0] = '%'; cc[2] = '\0';
+    cc[0] = '%';
     luaL_buffinit(L, &b);
     for (; *s; s++) {
-      if (*s != '%' || *(s + 1) == '\0')  /* no conversion specifier? */
+      if (*s != '%')  /* no conversion specifier? */
         luaL_addchar(&b, *s);
       else {
         size_t reslen;
+        int i = 1;
         char buff[200];  /* should be big enough for any conversion result */
-        cc[1] = *(++s);
+        if (*(++s) != '\0' && strchr(LUA_STRFTIMEPREFIX, *s))
+          cc[i++] = *(s++);
+        if (*s != '\0' && strchr(LUA_STRFTIMEOPTIONS, *s))
+          cc[i++] = *s;
+        else {
+          const char *msg = lua_pushfstring(L,
+                              "invalid conversion specifier '%%%c'", *s);
+          return luaL_argerror(L, 1, msg);
+        }
+        cc[i] = '\0';
         reslen = strftime(buff, sizeof(buff), cc, stm);
         luaL_addlstring(&b, buff, reslen);
       }

+ 13 - 1
luaconf.h

@@ -1,5 +1,5 @@
 /*
-** $Id: luaconf.h,v 1.86 2006/09/18 14:03:18 roberto Exp roberto $
+** $Id: luaconf.h,v 1.87 2007/02/07 17:46:20 roberto Exp roberto $
 ** Configuration file for Lua
 ** See Copyright Notice in lua.h
 */
@@ -657,6 +657,18 @@ union luai_Cast { double l_d; long l_l; };
 #endif
 
 
+/*
+@@ LUA_STRFTIMEOPTIONS is the list of valid conversion specifier
+@* characters for the 'strftime' function;
+@@ LUA_STRFTIMEPREFIX is the list of valid modifiers for
+@* that function.
+** CHANGE them if you want to use non-ansi options specific to your system.
+*/
+#define LUA_STRFTIMEOPTIONS	"aAbBcdHIjmMpSUwWxXyYz%"
+#define LUA_STRFTIMEPREFIX	""
+
+
+
 /*
 @@ lua_popen spawns a new process connected to the current one through
 @* the file streams.