aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-08-09 17:58:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-08-09 17:58:29 -0300
commitb5bf7d9ef414ce05da8523b966d94252c32a4010 (patch)
treeff64b748a69e59c6bf55414ef443f3a80ed84011
parent6a5d89b39fdd22fdac03cbaeab375c58756c5b5f (diff)
downloadlua-b5bf7d9ef414ce05da8523b966d94252c32a4010.tar.gz
lua-b5bf7d9ef414ce05da8523b966d94252c32a4010.tar.bz2
lua-b5bf7d9ef414ce05da8523b966d94252c32a4010.zip
'string.format' checks whether values for integer formats are
actually integers
-rw-r--r--lstrlib.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 979522ae..020ed757 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.169 2011/06/16 14:14:05 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.170 2011/06/28 17:13:52 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -154,7 +154,7 @@ static int str_char (lua_State *L) {
154 char *p = luaL_buffinitsize(L, &b, n); 154 char *p = luaL_buffinitsize(L, &b, n);
155 for (i=1; i<=n; i++) { 155 for (i=1; i<=n; i++) {
156 int c = luaL_checkint(L, i); 156 int c = luaL_checkint(L, i);
157 luaL_argcheck(L, uchar(c) == c, i, "invalid value"); 157 luaL_argcheck(L, uchar(c) == c, i, "value out of range");
158 p[i - 1] = uchar(c); 158 p[i - 1] = uchar(c);
159 } 159 }
160 luaL_pushresultsize(&b, n); 160 luaL_pushresultsize(&b, n);
@@ -865,11 +865,20 @@ static int str_format (lua_State *L) {
865 nb = sprintf(buff, form, luaL_checkint(L, arg)); 865 nb = sprintf(buff, form, luaL_checkint(L, arg));
866 break; 866 break;
867 } 867 }
868 case 'd': case 'i': 868 case 'd': case 'i': {
869 lua_Number n = luaL_checknumber(L, arg);
870 LUA_INTFRM_T r = (LUA_INTFRM_T)n;
871 luaL_argcheck(L, (lua_Number)r == n, arg,
872 "not an integer in proper range");
873 addlenmod(form, LUA_INTFRMLEN);
874 nb = sprintf(buff, form, r);
875 break;
876 }
869 case 'o': case 'u': case 'x': case 'X': { 877 case 'o': case 'u': case 'x': case 'X': {
870 lua_Number n = luaL_checknumber(L, arg); 878 lua_Number n = luaL_checknumber(L, arg);
871 LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : 879 unsigned LUA_INTFRM_T r = (unsigned LUA_INTFRM_T)n;
872 (LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n; 880 luaL_argcheck(L, (lua_Number)r == n, arg,
881 "not a non-negative integer in proper range");
873 addlenmod(form, LUA_INTFRMLEN); 882 addlenmod(form, LUA_INTFRMLEN);
874 nb = sprintf(buff, form, r); 883 nb = sprintf(buff, form, r);
875 break; 884 break;