diff --git a/llimits.h b/llimits.h index e7da009b..d7ae065b 100644 --- a/llimits.h +++ b/llimits.h @@ -163,13 +163,15 @@ typedef LUAI_UACINT l_uacInt; */ #define ct_diff2sz(df) ((size_t)(df)) +/* ptrdiff_t to lua_Integer */ +#define ct_diff2S(df) cast_st2S(ct_diff2sz(df)) + /* ** Special type equivalent to '(void*)' for functions (to suppress some ** warnings when converting function pointers) */ typedef void (*voidf)(void); - /* ** Macro to convert pointer-to-void* to pointer-to-function. This cast ** is undefined according to ISO C, but POSIX assumes that it works. diff --git a/lmem.c b/lmem.c index d02c9fdc..52a4f999 100644 --- a/lmem.c +++ b/lmem.c @@ -126,10 +126,10 @@ void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize, ** error. */ void *luaM_shrinkvector_ (lua_State *L, void *block, int *size, - int final_n, int size_elem) { + int final_n, unsigned size_elem) { void *newblock; - size_t oldsize = cast_sizet((*size) * size_elem); - size_t newsize = cast_sizet(final_n * size_elem); + size_t oldsize = cast_sizet(*size) * size_elem; + size_t newsize = cast_sizet(final_n) * size_elem; lua_assert(newsize <= oldsize); newblock = luaM_saferealloc_(L, block, oldsize, newsize); *size = final_n; diff --git a/lmem.h b/lmem.h index aa306105..204ce3bc 100644 --- a/lmem.h +++ b/lmem.h @@ -88,7 +88,7 @@ LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *size, unsigned size_elem, int limit, const char *what); LUAI_FUNC void *luaM_shrinkvector_ (lua_State *L, void *block, int *nelem, - int final_n, int size_elem); + int final_n, unsigned size_elem); LUAI_FUNC void *luaM_malloc_ (lua_State *L, size_t size, int tag); #endif diff --git a/lstrlib.c b/lstrlib.c index e9421c27..321d6a0b 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -704,7 +704,8 @@ static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s, if (l_unlikely(capl == CAP_UNFINISHED)) luaL_error(ms->L, "unfinished capture"); else if (capl == CAP_POSITION) - lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); + lua_pushinteger(ms->L, + ct_diff2S(ms->capture[i].init - ms->src_init) + 1); return capl; } } @@ -775,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) { /* do a plain search */ const char *s2 = lmemfind(s + init, ls - init, p, lp); if (s2) { - lua_pushinteger(L, (s2 - s) + 1); + lua_pushinteger(L, ct_diff2S(s2 - s) + 1); lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp)); return 2; } @@ -793,8 +794,8 @@ static int str_find_aux (lua_State *L, int find) { reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (find) { - lua_pushinteger(L, (s1 - s) + 1); /* start */ - lua_pushinteger(L, res - s); /* end */ + lua_pushinteger(L, ct_diff2S(s1 - s) + 1); /* start */ + lua_pushinteger(L, ct_diff2S(res - s)); /* end */ return push_captures(&ms, NULL, 0) + 2; } else diff --git a/ltablib.c b/ltablib.c index 538d585d..4db3768a 100644 --- a/ltablib.c +++ b/ltablib.c @@ -231,10 +231,18 @@ static int tunpack (lua_State *L) { */ -/* type for array indices */ +/* +** Type for array indices. These indices are always limited by INT_MAX, +** so it is safe to cast them to lua_Integer even for Lua 32 bits. +*/ typedef unsigned int IdxT; +/* Versions of lua_seti/lua_geti specialized for IdxT */ +#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx)) +#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx)) + + /* ** Produce a "random" 'unsigned int' to randomize pivot choice. This ** macro is used only when 'sort' detects a big imbalance in the result @@ -251,8 +259,8 @@ typedef unsigned int IdxT; static void set2 (lua_State *L, IdxT i, IdxT j) { - lua_seti(L, 1, i); - lua_seti(L, 1, j); + seti(L, 1, i); + seti(L, 1, j); } @@ -289,14 +297,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ for (;;) { /* next loop: repeat ++i while a[i] < P */ - while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { + while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ /* next loop: repeat --j while P < a[j] */ - while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { + while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[j] */ @@ -335,8 +343,8 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { IdxT p; /* Pivot index */ IdxT n; /* to be used later */ /* sort elements 'lo', 'p', and 'up' */ - lua_geti(L, 1, lo); - lua_geti(L, 1, up); + geti(L, 1, lo); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ set2(L, lo, up); /* swap a[lo] - a[up] */ else @@ -347,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { p = (lo + up)/2; /* middle element is a good pivot */ else /* for larger intervals, it is worth a random pivot */ p = choosePivot(lo, up, rnd); - lua_geti(L, 1, p); - lua_geti(L, 1, lo); + geti(L, 1, p); + geti(L, 1, lo); if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ set2(L, p, lo); /* swap a[p] - a[lo] */ else { lua_pop(L, 1); /* remove a[lo] */ - lua_geti(L, 1, up); + geti(L, 1, up); if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ set2(L, p, up); /* swap a[up] - a[p] */ else @@ -361,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { } if (up - lo == 2) /* only 3 elements? */ return; /* already sorted */ - lua_geti(L, 1, p); /* get middle element (Pivot) */ + geti(L, 1, p); /* get middle element (Pivot) */ lua_pushvalue(L, -1); /* push Pivot */ - lua_geti(L, 1, up - 1); /* push a[up - 1] */ + geti(L, 1, up - 1); /* push a[up - 1] */ set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ p = partition(L, lo, up); /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ diff --git a/ltests.c b/ltests.c index 8a6b4065..7d134e2d 100644 --- a/ltests.c +++ b/ltests.c @@ -1040,14 +1040,14 @@ static int table_query (lua_State *L) { static int query_GCparams (lua_State *L) { global_State *g = G(L); - lua_pushinteger(L, gettotalobjs(g)); - lua_pushinteger(L, g->GCdebt); - lua_pushinteger(L, applygcparam(g, MINORMUL, 100)); - lua_pushinteger(L, applygcparam(g, MAJORMINOR, 100)); - lua_pushinteger(L, applygcparam(g, MINORMAJOR, 100)); - lua_pushinteger(L, applygcparam(g, PAUSE, 100)); - lua_pushinteger(L, applygcparam(g, STEPMUL, 100)); - lua_pushinteger(L, applygcparam(g, STEPSIZE, 100)); + lua_pushinteger(L, cast(lua_Integer, gettotalobjs(g))); + lua_pushinteger(L, cast(lua_Integer, g->GCdebt)); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMUL, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MAJORMINOR, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, MINORMAJOR, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, PAUSE, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPMUL, 100))); + lua_pushinteger(L, cast(lua_Integer, applygcparam(g, STEPSIZE, 100))); return 8; } @@ -1062,7 +1062,7 @@ static int test_codeparam (lua_State *L) { static int test_applyparam (lua_State *L) { lua_Integer p = luaL_checkinteger(L, 1); lua_Integer x = luaL_checkinteger(L, 2); - lua_pushinteger(L, luaO_applyparam(cast_byte(p), x)); + lua_pushinteger(L, cast(lua_Integer, luaO_applyparam(cast_byte(p), x))); return 1; } @@ -1162,7 +1162,7 @@ static int pushuserdata (lua_State *L) { static int udataval (lua_State *L) { - lua_pushinteger(L, cast(long, lua_touserdata(L, 1))); + lua_pushinteger(L, cast(lua_Integer, cast(size_t, lua_touserdata(L, 1)))); return 1; } @@ -1199,7 +1199,7 @@ static int num2int (lua_State *L) { static int makeseed (lua_State *L) { - lua_pushinteger(L, luaL_makeseed(L)); + lua_pushinteger(L, cast(lua_Integer, luaL_makeseed(L))); return 1; } @@ -1486,7 +1486,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *inst = getstring; if EQ("") return 0; else if EQ("absindex") { - lua_pushnumber(L1, lua_absindex(L1, getindex)); + lua_pushinteger(L1, lua_absindex(L1, getindex)); } else if EQ("append") { int t = getindex; @@ -1538,7 +1538,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { } else if EQ("func2num") { lua_CFunction func = lua_tocfunction(L1, getindex); - lua_pushinteger(L1, cast(lua_Integer, func)); + lua_pushinteger(L1, cast(lua_Integer, cast(size_t, func))); } else if EQ("getfield") { int t = getindex; @@ -1901,7 +1901,7 @@ static int Cfunc (lua_State *L) { static int Cfunck (lua_State *L, int status, lua_KContext ctx) { lua_pushstring(L, statcodes[status]); lua_setglobal(L, "status"); - lua_pushinteger(L, ctx); + lua_pushinteger(L, cast(lua_Integer, ctx)); lua_setglobal(L, "ctx"); return runC(L, L, lua_tostring(L, cast_int(ctx))); } diff --git a/lutf8lib.c b/lutf8lib.c index 6dfdd1f4..04bbfa56 100644 --- a/lutf8lib.c +++ b/lutf8lib.c @@ -103,7 +103,7 @@ static int utflen (lua_State *L) { lua_pushinteger(L, posi + 1); /* ... and current position */ return 2; } - posi = s1 - s; + posi = ct_diff2S(s1 - s); n++; } lua_pushinteger(L, n); @@ -137,7 +137,7 @@ static int codepoint (lua_State *L) { s = utf8_decode(s, &code, !lax); if (s == NULL) return luaL_error(L, MSGInvalid); - lua_pushinteger(L, code); + lua_pushinteger(L, l_castU2S(code)); n++; } return n; @@ -240,7 +240,7 @@ static int iter_aux (lua_State *L, int strict) { if (next == NULL || iscontp(next)) return luaL_error(L, MSGInvalid); lua_pushinteger(L, l_castU2S(n + 1)); - lua_pushinteger(L, code); + lua_pushinteger(L, l_castU2S(code)); return 2; } } diff --git a/testes/strings.lua b/testes/strings.lua index a0204309..9bb52b35 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -111,7 +111,7 @@ assert(string.rep('', 10) == '') do checkerror("too large", string.rep, 'aa', math.maxinteger); - checkerror("too large", string.rep, 'a', math.maxinteger/2, ',') + checkerror("too large", string.rep, 'a', math.maxinteger, ',') end -- repetitions with separator