[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2 string formatting check fails when using integers
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 13 Apr 2012 15:22:16 -0300
> This is how I ended up solving it (note the LUA_NUMBER_INTEGER define):
>
>
> case 'd': case 'i': {
> lua_Number n = luaL_checknumber(L, arg);
> #ifndef LUA_NUMBER_INTEGER
> luaL_argcheck(L, (MIN_INTFRM - 1) < n && n < (MAX_INTFRM + 1), arg,
> "not a number in proper range");
> #endif
> addlenmod(form, LUA_INTFRMLEN);
> nb = sprintf(buff, form, (LUA_INTFRM_T)n);
> break;
> }
> case 'o': case 'u': case 'x': case 'X': {
> lua_Number n = luaL_checknumber(L, arg);
> #ifndef LUA_NUMBER_INTEGER
> luaL_argcheck(L, 0 <= n && n < (MAX_UINTFRM + 1), arg,
> "not a non-negative number in proper range");
> #endif
> addlenmod(form, LUA_INTFRMLEN);
> nb = sprintf(buff, form, (unsigned LUA_INTFRM_T)n);
> break;
> }
Maybe it would be a little more "elegant" to use some macros to
abstract the tests, and then add those 'ifdefs' around the macros.
Something like this:
#ifdef LUA_NUMBER_INTEGER
#define checkintrange(L,n,arg) (void)0;
#else
#define MAX_INTFRM ((lua_Number)((~(unsigned LUA_INTFRM_T)0)/2))
#define MIN_INTFRM (-(lua_Number)((~(unsigned LUA_INTFRM_T)0)/2) - 1)
#define checkintrange(L,n,arg)
luaL_argcheck(L, (MIN_INTFRM - 1) < n && n < (MAX_INTFRM + 1), arg, \
"not a number in proper range");
#endif
...
case 'd': case 'i': {
lua_Number n = luaL_checknumber(L, arg);
checkintrange(L, n, arg);
addlenmod(form, LUA_INTFRMLEN);
-- Roberto