aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-11-30 16:24:56 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2011-11-30 16:24:56 -0200
commitf1d2ac3a98b1225d9b086c46c60f3d2cd7e56cf7 (patch)
treebaa5d11364f8c6341662022e226ebd6f1a064d74
parentc5da4f4cd067a2efa2a8f0f14f3034ed0b635f4d (diff)
downloadlua-f1d2ac3a98b1225d9b086c46c60f3d2cd7e56cf7.tar.gz
lua-f1d2ac3a98b1225d9b086c46c60f3d2cd7e56cf7.tar.bz2
lua-f1d2ac3a98b1225d9b086c46c60f3d2cd7e56cf7.zip
allow non-integer arguments to integer formats (%d, %x, etc.),
but check range
-rw-r--r--lstrlib.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 85a9321c..b41ccd20 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.171 2011/08/09 20:58:29 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.172 2011/10/25 12:01:20 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*/
@@ -756,6 +756,9 @@ static int str_gsub (lua_State *L) {
756#endif 756#endif
757#endif /* } */ 757#endif /* } */
758 758
759#define MAX_UINTFRM ((lua_Number)(~(unsigned LUA_INTFRM_T)0))
760#define MAX_INTFRM ((lua_Number)((~(unsigned LUA_INTFRM_T)0)/2))
761#define MIN_INTFRM (-(lua_Number)((~(unsigned LUA_INTFRM_T)0)/2) - 1)
759 762
760/* 763/*
761** LUA_FLTFRMLEN is the length modifier for float conversions in 764** LUA_FLTFRMLEN is the length modifier for float conversions in
@@ -867,20 +870,18 @@ static int str_format (lua_State *L) {
867 } 870 }
868 case 'd': case 'i': { 871 case 'd': case 'i': {
869 lua_Number n = luaL_checknumber(L, arg); 872 lua_Number n = luaL_checknumber(L, arg);
870 LUA_INTFRM_T r = (LUA_INTFRM_T)n; 873 luaL_argcheck(L, (MIN_INTFRM - 1) < n && n < (MAX_INTFRM + 1), arg,
871 luaL_argcheck(L, (lua_Number)r == n, arg, 874 "not a number in proper range");
872 "not an integer in proper range");
873 addlenmod(form, LUA_INTFRMLEN); 875 addlenmod(form, LUA_INTFRMLEN);
874 nb = sprintf(buff, form, r); 876 nb = sprintf(buff, form, (LUA_INTFRM_T)n);
875 break; 877 break;
876 } 878 }
877 case 'o': case 'u': case 'x': case 'X': { 879 case 'o': case 'u': case 'x': case 'X': {
878 lua_Number n = luaL_checknumber(L, arg); 880 lua_Number n = luaL_checknumber(L, arg);
879 unsigned LUA_INTFRM_T r = (unsigned LUA_INTFRM_T)n; 881 luaL_argcheck(L, 0 <= n && n < (MAX_UINTFRM + 1), arg,
880 luaL_argcheck(L, (lua_Number)r == n, arg, 882 "not a non-negative number in proper range");
881 "not a non-negative integer in proper range");
882 addlenmod(form, LUA_INTFRMLEN); 883 addlenmod(form, LUA_INTFRMLEN);
883 nb = sprintf(buff, form, r); 884 nb = sprintf(buff, form, (unsigned LUA_INTFRM_T)n);
884 break; 885 break;
885 } 886 }
886 case 'e': case 'E': case 'f': 887 case 'e': case 'E': case 'f':