different scheme to check arguments to '%d', '%x', etc. Old scheme

did not work well when lua_Number is an integer.
This commit is contained in:
Roberto Ierusalimschy 2012-04-20 10:16:48 -03:00
parent 1485ea2ee7
commit 048be9cf27

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.173 2011/11/30 18:24:56 roberto Exp roberto $
** $Id: lstrlib.c,v 1.174 2012/04/03 19:06:19 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -758,9 +758,6 @@ static int str_gsub (lua_State *L) {
#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
@ -872,18 +869,22 @@ static int str_format (lua_State *L) {
}
case 'd': case 'i': {
lua_Number n = luaL_checknumber(L, arg);
luaL_argcheck(L, (MIN_INTFRM - 1) < n && n < (MAX_INTFRM + 1), arg,
LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni;
luaL_argcheck(L, -1 < diff && diff < 1, arg,
"not a number in proper range");
addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, (LUA_INTFRM_T)n);
nb = sprintf(buff, form, ni);
break;
}
case 'o': case 'u': case 'x': case 'X': {
lua_Number n = luaL_checknumber(L, arg);
luaL_argcheck(L, 0 <= n && n < (MAX_UINTFRM + 1), arg,
unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni;
luaL_argcheck(L, -1 < diff && diff < 1, arg,
"not a non-negative number in proper range");
addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, (unsigned LUA_INTFRM_T)n);
nb = sprintf(buff, form, ni);
break;
}
case 'e': case 'E': case 'f':