1
0
mirror of https://github.com/lua/lua.git synced 2025-02-04 06:13:04 +08:00

allow non-integer arguments to integer formats (%d, %x, etc.),

but check range
This commit is contained in:
Roberto Ierusalimschy 2011-11-30 16:24:56 -02:00
parent c5da4f4cd0
commit f1d2ac3a98

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lstrlib.c,v 1.171 2011/08/09 20:58:29 roberto Exp roberto $ ** $Id: lstrlib.c,v 1.172 2011/10/25 12:01:20 roberto Exp roberto $
** Standard library for string operations and pattern-matching ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -756,6 +756,9 @@ static int str_gsub (lua_State *L) {
#endif #endif
#endif /* } */ #endif /* } */
#define MAX_UINTFRM ((lua_Number)(~(unsigned LUA_INTFRM_T)0))
#define MAX_INTFRM ((lua_Number)((~(unsigned LUA_INTFRM_T)0)/2))
#define MIN_INTFRM (-(lua_Number)((~(unsigned LUA_INTFRM_T)0)/2) - 1)
/* /*
** LUA_FLTFRMLEN is the length modifier for float conversions in ** LUA_FLTFRMLEN is the length modifier for float conversions in
@ -867,20 +870,18 @@ static int str_format (lua_State *L) {
} }
case 'd': case 'i': { case 'd': case 'i': {
lua_Number n = luaL_checknumber(L, arg); lua_Number n = luaL_checknumber(L, arg);
LUA_INTFRM_T r = (LUA_INTFRM_T)n; luaL_argcheck(L, (MIN_INTFRM - 1) < n && n < (MAX_INTFRM + 1), arg,
luaL_argcheck(L, (lua_Number)r == n, arg, "not a number in proper range");
"not an integer in proper range");
addlenmod(form, LUA_INTFRMLEN); addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, r); nb = sprintf(buff, form, (LUA_INTFRM_T)n);
break; break;
} }
case 'o': case 'u': case 'x': case 'X': { case 'o': case 'u': case 'x': case 'X': {
lua_Number n = luaL_checknumber(L, arg); lua_Number n = luaL_checknumber(L, arg);
unsigned LUA_INTFRM_T r = (unsigned LUA_INTFRM_T)n; luaL_argcheck(L, 0 <= n && n < (MAX_UINTFRM + 1), arg,
luaL_argcheck(L, (lua_Number)r == n, arg, "not a non-negative number in proper range");
"not a non-negative integer in proper range");
addlenmod(form, LUA_INTFRMLEN); addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, r); nb = sprintf(buff, form, (unsigned LUA_INTFRM_T)n);
break; break;
} }
case 'e': case 'E': case 'f': case 'e': case 'E': case 'f':