commit 0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b
parent 15231d4fb2f6984b25e0353ff46eda1a180b686d
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Sat, 27 Jul 2024 13:32:32 -0300
Added gcc option '-Wconversion'
No warnings for standard numerical types. Still pending alternative
numerical types.
Diffstat:
44 files changed, 398 insertions(+), 359 deletions(-)
diff --git a/lapi.c b/lapi.c
@@ -58,7 +58,7 @@ static void advancegc (lua_State *L, size_t delta) {
delta >>= 5; /* one object for each 32 bytes (empirical) */
if (delta > 0) {
global_State *g = G(L);
- luaE_setdebt(g, g->GCdebt - delta);
+ luaE_setdebt(g, g->GCdebt - cast(l_obj, delta));
}
}
@@ -437,9 +437,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
LUA_API lua_Unsigned lua_rawlen (lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
switch (ttypetag(o)) {
- case LUA_VSHRSTR: return tsvalue(o)->shrlen;
- case LUA_VLNGSTR: return tsvalue(o)->u.lnglen;
- case LUA_VUSERDATA: return uvalue(o)->len;
+ case LUA_VSHRSTR: return cast(lua_Unsigned, tsvalue(o)->shrlen);
+ case LUA_VLNGSTR: return cast(lua_Unsigned, tsvalue(o)->u.lnglen);
+ case LUA_VUSERDATA: return cast(lua_Unsigned, uvalue(o)->len);
case LUA_VTABLE: return luaH_getn(hvalue(o));
default: return 0;
}
@@ -667,7 +667,7 @@ LUA_API int lua_pushthread (lua_State *L) {
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
- int tag;
+ lu_byte tag;
TString *str = luaS_new(L, k);
luaV_fastget(t, str, s2v(L->top.p), luaH_getstr, tag);
if (!tagisempty(tag)) {
@@ -685,7 +685,7 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
static void getGlobalTable (lua_State *L, TValue *gt) {
Table *registry = hvalue(&G(L)->l_registry);
- int tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
+ lu_byte tag = luaH_getint(registry, LUA_RIDX_GLOBALS, gt);
(void)tag; /* avoid not-used warnings when checks are off */
api_check(L, novariant(tag) == LUA_TTABLE, "global table must exist");
}
@@ -700,7 +700,7 @@ LUA_API int lua_getglobal (lua_State *L, const char *name) {
LUA_API int lua_gettable (lua_State *L, int idx) {
- int tag;
+ lu_byte tag;
TValue *t;
lua_lock(L);
api_checkpop(L, 1);
@@ -721,7 +721,7 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
TValue *t;
- int tag;
+ lu_byte tag;
lua_lock(L);
t = index2value(L, idx);
luaV_fastgeti(t, n, s2v(L->top.p), tag);
@@ -736,7 +736,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
}
-static int finishrawget (lua_State *L, int tag) {
+static int finishrawget (lua_State *L, lu_byte tag) {
if (tagisempty(tag)) /* avoid copying empty items to the stack */
setnilvalue(s2v(L->top.p));
api_incr_top(L);
@@ -754,7 +754,7 @@ l_sinline Table *gettable (lua_State *L, int idx) {
LUA_API int lua_rawget (lua_State *L, int idx) {
Table *t;
- int tag;
+ lu_byte tag;
lua_lock(L);
api_checkpop(L, 1);
t = gettable(L, idx);
@@ -766,7 +766,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) {
LUA_API int lua_rawgeti (lua_State *L, int idx, lua_Integer n) {
Table *t;
- int tag;
+ lu_byte tag;
lua_lock(L);
t = gettable(L, idx);
luaH_fastgeti(t, n, s2v(L->top.p), tag);
@@ -1231,7 +1231,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
api_check(L, 0 <= param && param < LUA_GCPN, "invalid parameter");
res = cast_int(luaO_applyparam(g->gcparams[param], 100));
if (value >= 0)
- g->gcparams[param] = luaO_codeparam(value);
+ g->gcparams[param] = luaO_codeparam(cast_uint(value));
break;
}
default: res = -1; /* invalid option */
@@ -1353,7 +1353,7 @@ LUA_API void *lua_newuserdatauv (lua_State *L, size_t size, int nuvalue) {
Udata *u;
lua_lock(L);
api_check(L, 0 <= nuvalue && nuvalue < USHRT_MAX, "invalid value");
- u = luaS_newudata(L, size, nuvalue);
+ u = luaS_newudata(L, size, cast(unsigned short, nuvalue));
setuvalue(L, s2v(L->top.p), u);
api_incr_top(L);
advancegc(L, size);
diff --git a/lauxlib.c b/lauxlib.c
@@ -539,7 +539,7 @@ static void newbox (lua_State *L) {
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */
if (l_unlikely(sz > MAX_SIZE - B->n - 1))
- return luaL_error(B->L, "resulting string too large");
+ return cast_sizet(luaL_error(B->L, "resulting string too large"));
if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) {
/* newsize was not big enough or too big */
newsize = B->n + sz + 1;
@@ -725,7 +725,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
*/
typedef struct LoadF {
- int n; /* number of pre-read characters */
+ unsigned n; /* number of pre-read characters */
FILE *f; /* file being read */
char buff[BUFSIZ]; /* area for reading file */
} LoadF;
@@ -825,7 +825,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
}
}
if (c != EOF)
- lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
+ lf.buff[lf.n++] = cast_char(c); /* 'c' is the first character */
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
readstatus = ferror(lf.f);
errno = 0; /* no useful error number until here */
@@ -1020,7 +1020,7 @@ LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
const char *wild;
size_t l = strlen(p);
while ((wild = strstr(s, p)) != NULL) {
- luaL_addlstring(b, s, wild - s); /* push prefix */
+ luaL_addlstring(b, s, ct_diff2sz(wild - s)); /* push prefix */
luaL_addstring(b, r); /* push replacement in place of pattern */
s = wild + l; /* continue after 'p' */
}
diff --git a/lbaselib.c b/lbaselib.c
@@ -58,21 +58,22 @@ static int luaB_warn (lua_State *L) {
#define SPACECHARS " \f\n\r\t\v"
-static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
+static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle sign */
else if (*s == '+') s++;
- if (!isalnum((unsigned char)*s)) /* no digit? */
+ if (!isalnum(cast_uchar(*s))) /* no digit? */
return NULL;
do {
- int digit = (isdigit((unsigned char)*s)) ? *s - '0'
- : (toupper((unsigned char)*s) - 'A') + 10;
+ unsigned digit = cast_uint(isdigit(cast_uchar(*s))
+ ? *s - '0'
+ : (toupper(cast_uchar(*s)) - 'A') + 10);
if (digit >= base) return NULL; /* invalid numeral */
n = n * base + digit;
s++;
- } while (isalnum((unsigned char)*s));
+ } while (isalnum(cast_uchar(*s)));
s += strspn(s, SPACECHARS); /* skip trailing spaces */
*pn = (lua_Integer)((neg) ? (0u - n) : n);
return s;
@@ -102,7 +103,7 @@ static int luaB_tonumber (lua_State *L) {
luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
s = lua_tolstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
- if (b_str2int(s, (int)base, &n) == s + l) {
+ if (b_str2int(s, cast_uint(base), &n) == s + l) {
lua_pushinteger(L, n);
return 1;
} /* else not a number */
@@ -159,7 +160,7 @@ static int luaB_rawlen (lua_State *L) {
int t = lua_type(L, 1);
luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
"table or string");
- lua_pushinteger(L, lua_rawlen(L, 1));
+ lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1)));
return 1;
}
diff --git a/lcode.c b/lcode.c
@@ -335,7 +335,7 @@ static void savelineinfo (FuncState *fs, Proto *f, int line) {
}
luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
INT_MAX, "opcodes");
- f->lineinfo[pc] = linedif;
+ f->lineinfo[pc] = cast(ls_byte, linedif);
fs->previousline = line; /* last line saved */
}
@@ -409,7 +409,7 @@ int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
/*
** Format and emit an 'iABx' instruction.
*/
-int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) {
+int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bc) {
lua_assert(getOpMode(o) == iABx);
lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
return luaK_code(fs, CREATE_ABx(o, A, Bc));
@@ -420,7 +420,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) {
** Format and emit an 'iAsBx' instruction.
*/
static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
- unsigned int b = cast_uint(Bc) + OFFSET_sBx;
+ int b = Bc + OFFSET_sBx;
lua_assert(getOpMode(o) == iAsBx);
lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
return luaK_code(fs, CREATE_ABx(o, A, b));
@@ -431,7 +431,7 @@ static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
** Format and emit an 'isJ' instruction.
*/
static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
- unsigned int j = cast_uint(sj) + OFFSET_sJ;
+ int j = sj + OFFSET_sJ;
lua_assert(getOpMode(o) == isJ);
lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
return luaK_code(fs, CREATE_sJ(o, j, k));
@@ -483,7 +483,7 @@ void luaK_checkstack (FuncState *fs, int n) {
*/
void luaK_reserveregs (FuncState *fs, int n) {
luaK_checkstack(fs, n);
- fs->freereg += n;
+ fs->freereg = cast_byte(fs->freereg + n);
}
@@ -1290,25 +1290,25 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
luaK_exp2anyreg(fs, t); /* put it in a register */
if (t->k == VUPVAL) {
- int temp = t->u.info; /* upvalue index */
+ lu_byte temp = cast_byte(t->u.info); /* upvalue index */
lua_assert(isKstr(fs, k));
t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */
- t->u.ind.idx = k->u.info; /* literal short string */
+ t->u.ind.idx = cast(short, k->u.info); /* literal short string */
t->k = VINDEXUP;
}
else {
/* register index of the table */
- t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
+ t->u.ind.t = cast_byte((t->k == VLOCAL) ? t->u.var.ridx: t->u.info);
if (isKstr(fs, k)) {
- t->u.ind.idx = k->u.info; /* literal short string */
+ t->u.ind.idx = cast(short, k->u.info); /* literal short string */
t->k = VINDEXSTR;
}
- else if (isCint(k)) {
- t->u.ind.idx = cast_int(k->u.ival); /* int. constant in proper range */
+ else if (isCint(k)) { /* int. constant in proper range? */
+ t->u.ind.idx = cast(short, k->u.ival);
t->k = VINDEXI;
}
else {
- t->u.ind.idx = luaK_exp2anyreg(fs, k); /* register */
+ t->u.ind.idx = cast(short, luaK_exp2anyreg(fs, k)); /* register */
t->k = VINDEXED;
}
}
@@ -1623,7 +1623,7 @@ void luaK_prefix (FuncState *fs, UnOpr opr, expdesc *e, int line) {
luaK_dischargevars(fs, e);
switch (opr) {
case OPR_MINUS: case OPR_BNOT: /* use 'ef' as fake 2nd operand */
- if (constfolding(fs, opr + LUA_OPUNM, e, &ef))
+ if (constfolding(fs, cast_int(opr + LUA_OPUNM), e, &ef))
break;
/* else */ /* FALLTHROUGH */
case OPR_LEN:
@@ -1711,7 +1711,7 @@ static void codeconcat (FuncState *fs, expdesc *e1, expdesc *e2, int line) {
void luaK_posfix (FuncState *fs, BinOpr opr,
expdesc *e1, expdesc *e2, int line) {
luaK_dischargevars(fs, e2);
- if (foldbinop(opr) && constfolding(fs, opr + LUA_OPADD, e1, e2))
+ if (foldbinop(opr) && constfolding(fs, cast_int(opr + LUA_OPADD), e1, e2))
return; /* done by folding */
switch (opr) {
case OPR_AND: {
@@ -1797,11 +1797,11 @@ void luaK_fixline (FuncState *fs, int line) {
void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
Instruction *inst = &fs->f->code[pc];
- int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */
int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */
int k = (extra > 0); /* true iff needs extra argument */
- *inst = CREATE_vABCk(OP_NEWTABLE, ra, rb, rc, k);
+ hsize = (hsize != 0) ? luaO_ceillog2(cast_uint(hsize)) + 1 : 0;
+ *inst = CREATE_vABCk(OP_NEWTABLE, ra, hsize, rc, k);
*(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
}
@@ -1825,7 +1825,7 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
codeextraarg(fs, extra);
}
- fs->freereg = base + 1; /* free registers with list values */
+ fs->freereg = cast_byte(base + 1); /* free registers with list values */
}
diff --git a/lcode.h b/lcode.h
@@ -60,7 +60,7 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
#define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
-LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned Bx);
+LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, int Bx);
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C,
int k);
LUAI_FUNC int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C,
diff --git a/ldebug.c b/ldebug.c
@@ -63,7 +63,7 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
return f->linedefined;
}
else {
- int i = cast_uint(pc) / MAXIWTHABS - 1; /* get an estimate */
+ int i = pc / MAXIWTHABS - 1; /* get an estimate */
/* estimate must be a lower bound of the correct base */
lua_assert(i < 0 ||
(i < f->sizeabslineinfo && f->abslineinfo[i].pc <= pc));
@@ -921,7 +921,7 @@ int luaG_tracecall (lua_State *L) {
*/
int luaG_traceexec (lua_State *L, const Instruction *pc) {
CallInfo *ci = L->ci;
- lu_byte mask = L->hookmask;
+ lu_byte mask = cast_byte(L->hookmask);
const Proto *p = ci_func(ci)->p;
int counthook;
if (!(mask & (LUA_MASKLINE | LUA_MASKCOUNT))) { /* no hooks? */
diff --git a/ldo.c b/ldo.c
@@ -241,7 +241,7 @@ int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
int oldsize = stacksize(L);
int i;
StkId newstack;
- int oldgcstop = G(L)->gcstopem;
+ lu_byte oldgcstop = G(L)->gcstopem;
lua_assert(newsize <= MAXSTACK || newsize == ERRORSTACKSIZE);
relstack(L); /* change pointers to offsets */
G(L)->gcstopem = 1; /* stop emergency collection */
@@ -357,7 +357,7 @@ void luaD_hook (lua_State *L, int event, int line,
int ftransfer, int ntransfer) {
lua_Hook hook = L->hook;
if (hook && L->allowhook) { /* make sure there is a hook */
- int mask = CIST_HOOKED;
+ unsigned mask = CIST_HOOKED;
CallInfo *ci = L->ci;
ptrdiff_t top = savestack(L, L->top.p); /* preserve original 'top' */
ptrdiff_t ci_top = savestack(L, ci->top.p); /* idem for 'ci->top' */
@@ -1058,9 +1058,9 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
luaZ_initbuffer(L, &p.buff);
status = luaD_pcall(L, f_parser, &p, savestack(L, L->top.p), L->errfunc);
luaZ_freebuffer(L, &p.buff);
- luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
- luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
- luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
+ luaM_freearray(L, p.dyd.actvar.arr, cast_sizet(p.dyd.actvar.size));
+ luaM_freearray(L, p.dyd.gt.arr, cast_sizet(p.dyd.gt.size));
+ luaM_freearray(L, p.dyd.label.arr, cast_sizet(p.dyd.label.size));
decnny(L);
return status;
}
diff --git a/ldump.c b/ldump.c
@@ -27,7 +27,7 @@ typedef struct {
lua_State *L;
lua_Writer writer;
void *data;
- lu_mem offset; /* current position relative to beginning of dump */
+ size_t offset; /* current position relative to beginning of dump */
int strip;
int status;
Table *h; /* table to track saved strings */
@@ -63,11 +63,11 @@ static void dumpBlock (DumpState *D, const void *b, size_t size) {
** Dump enough zeros to ensure that current position is a multiple of
** 'align'.
*/
-static void dumpAlign (DumpState *D, int align) {
- int padding = align - (D->offset % align);
+static void dumpAlign (DumpState *D, unsigned align) {
+ unsigned padding = align - cast_uint(D->offset % align);
if (padding < align) { /* padding == align means no padding */
static lua_Integer paddingContent = 0;
- lua_assert(cast_uint(align) <= sizeof(lua_Integer));
+ lua_assert(align <= sizeof(lua_Integer));
dumpBlock(D, &paddingContent, padding);
}
lua_assert(D->offset % align == 0);
@@ -94,10 +94,10 @@ static void dumpByte (DumpState *D, int y) {
*/
static void dumpVarint (DumpState *D, size_t x) {
lu_byte buff[DIBS];
- int n = 1;
+ unsigned n = 1;
buff[DIBS - 1] = x & 0x7f; /* fill least-significant byte */
while ((x >>= 7) != 0) /* fill other bytes in reverse order */
- buff[DIBS - (++n)] = (x & 0x7f) | 0x80;
+ buff[DIBS - (++n)] = cast_byte((x & 0x7f) | 0x80);
dumpVector(D, buff + DIBS - n, n);
}
@@ -159,7 +159,7 @@ static void dumpCode (DumpState *D, const Proto *f) {
dumpInt(D, f->sizecode);
dumpAlign(D, sizeof(f->code[0]));
lua_assert(f->code != NULL);
- dumpVector(D, f->code, f->sizecode);
+ dumpVector(D, f->code, cast_uint(f->sizecode));
}
@@ -216,13 +216,13 @@ static void dumpDebug (DumpState *D, const Proto *f) {
n = (D->strip) ? 0 : f->sizelineinfo;
dumpInt(D, n);
if (f->lineinfo != NULL)
- dumpVector(D, f->lineinfo, n);
+ dumpVector(D, f->lineinfo, cast_uint(n));
n = (D->strip) ? 0 : f->sizeabslineinfo;
dumpInt(D, n);
if (n > 0) {
/* 'abslineinfo' is an array of structures of int's */
dumpAlign(D, sizeof(int));
- dumpVector(D, f->abslineinfo, n);
+ dumpVector(D, f->abslineinfo, cast_uint(n));
}
n = (D->strip) ? 0 : f->sizelocvars;
dumpInt(D, n);
diff --git a/lfunc.c b/lfunc.c
@@ -266,14 +266,14 @@ Proto *luaF_newproto (lua_State *L) {
void luaF_freeproto (lua_State *L, Proto *f) {
if (!(f->flag & PF_FIXED)) {
- luaM_freearray(L, f->code, f->sizecode);
- luaM_freearray(L, f->lineinfo, f->sizelineinfo);
- luaM_freearray(L, f->abslineinfo, f->sizeabslineinfo);
+ luaM_freearray(L, f->code, cast_sizet(f->sizecode));
+ luaM_freearray(L, f->lineinfo, cast_sizet(f->sizelineinfo));
+ luaM_freearray(L, f->abslineinfo, cast_sizet(f->sizeabslineinfo));
}
- luaM_freearray(L, f->p, f->sizep);
- luaM_freearray(L, f->k, f->sizek);
- luaM_freearray(L, f->locvars, f->sizelocvars);
- luaM_freearray(L, f->upvalues, f->sizeupvalues);
+ luaM_freearray(L, f->p, cast_sizet(f->sizep));
+ luaM_freearray(L, f->k, cast_sizet(f->sizek));
+ luaM_freearray(L, f->locvars, cast_sizet(f->sizelocvars));
+ luaM_freearray(L, f->upvalues, cast_sizet(f->sizeupvalues));
luaM_free(L, f);
}
diff --git a/lfunc.h b/lfunc.h
@@ -11,11 +11,11 @@
#include "lobject.h"
-#define sizeCclosure(n) (cast_int(offsetof(CClosure, upvalue)) + \
- cast_int(sizeof(TValue)) * (n))
+#define sizeCclosure(n) \
+ (offsetof(CClosure, upvalue) + sizeof(TValue) * cast_uint(n))
-#define sizeLclosure(n) (cast_int(offsetof(LClosure, upvals)) + \
- cast_int(sizeof(TValue *)) * (n))
+#define sizeLclosure(n) \
+ (offsetof(LClosure, upvals) + sizeof(TValue *) * cast_uint(n))
/* test whether thread is in 'twups' list */
diff --git a/lgc.c b/lgc.c
@@ -246,7 +246,7 @@ void luaC_fix (lua_State *L, GCObject *o) {
** create a new collectable object (with given type, size, and offset)
** and link it to 'allgc' list.
*/
-GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
+GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz, size_t offset) {
global_State *g = G(L);
char *p = cast_charp(luaM_newobject(L, novariant(tt), sz));
GCObject *o = cast(GCObject *, p + offset);
@@ -262,7 +262,7 @@ GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz, size_t offset) {
/*
** create a new collectable object with no offset.
*/
-GCObject *luaC_newobj (lua_State *L, int tt, size_t sz) {
+GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz) {
return luaC_newobjdt(L, tt, sz, 0);
}
@@ -813,7 +813,7 @@ static void freeobj (lua_State *L, GCObject *o) {
case LUA_VSHRSTR: {
TString *ts = gco2ts(o);
luaS_remove(L, ts); /* remove it from hash table */
- luaM_freemem(L, ts, sizestrshr(ts->shrlen));
+ luaM_freemem(L, ts, sizestrshr(cast_uint(ts->shrlen)));
break;
}
case LUA_VLNGSTR: {
@@ -922,7 +922,7 @@ static void GCTM (lua_State *L) {
if (!notm(tm)) { /* is there a finalizer? */
int status;
lu_byte oldah = L->allowhook;
- int oldgcstp = g->gcstp;
+ lu_byte oldgcstp = g->gcstp;
g->gcstp |= GCSTPGC; /* avoid GC steps */
L->allowhook = 0; /* stop debug hooks during GC metamethod */
setobj2s(L, L->top.p++, tm); /* push finalizer... */
@@ -1235,7 +1235,7 @@ static void finishgencycle (lua_State *L, global_State *g) {
** the "sweep all" state to clear all objects, which are mostly black
** in generational mode.
*/
-static void minor2inc (lua_State *L, global_State *g, int kind) {
+static void minor2inc (lua_State *L, global_State *g, lu_byte kind) {
g->GCmajorminor = g->marked; /* number of live objects */
g->gckind = kind;
g->reallyold = g->old1 = g->survival = NULL;
@@ -1522,7 +1522,7 @@ static l_obj atomic (lua_State *L) {
** elements. The fast case sweeps the whole list.
*/
static void sweepstep (lua_State *L, global_State *g,
- int nextstate, GCObject **nextlist, int fast) {
+ lu_byte nextstate, GCObject **nextlist, int fast) {
if (g->sweepgc)
g->sweepgc = sweeplist(L, g->sweepgc, fast ? MAX_LOBJ : GCSWEEPMAX);
else { /* enter next state */
@@ -1706,7 +1706,7 @@ static void fullinc (lua_State *L, global_State *g) {
void luaC_fullgc (lua_State *L, int isemergency) {
global_State *g = G(L);
lua_assert(!g->gcemergency);
- g->gcemergency = isemergency; /* set flag */
+ g->gcemergency = cast_byte(isemergency); /* set flag */
switch (g->gckind) {
case KGC_GENMINOR: fullgen(L, g); break;
case KGC_INC: fullinc(L, g); break;
diff --git a/lgc.h b/lgc.h
@@ -245,8 +245,8 @@ LUAI_FUNC void luaC_freeallobjects (lua_State *L);
LUAI_FUNC void luaC_step (lua_State *L);
LUAI_FUNC void luaC_runtilstate (lua_State *L, int state, int fast);
LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
-LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz);
-LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, int tt, size_t sz,
+LUAI_FUNC GCObject *luaC_newobj (lua_State *L, lu_byte tt, size_t sz);
+LUAI_FUNC GCObject *luaC_newobjdt (lua_State *L, lu_byte tt, size_t sz,
size_t offset);
LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
diff --git a/liolib.c b/liolib.c
@@ -443,7 +443,7 @@ static int nextc (RN *rn) {
return 0; /* fail */
}
else {
- rn->buff[rn->n++] = rn->c; /* save current char */
+ rn->buff[rn->n++] = cast_char(rn->c); /* save current char */
rn->c = l_getc(rn->f); /* read next one */
return 1;
}
@@ -524,15 +524,15 @@ static int read_line (lua_State *L, FILE *f, int chop) {
luaL_buffinit(L, &b);
do { /* may need to read several chunks to get whole line */
char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */
- int i = 0;
+ unsigned i = 0;
l_lockfile(f); /* no memory errors can happen inside the lock */
while (i < LUAL_BUFFERSIZE && (c = l_getc(f)) != EOF && c != '\n')
- buff[i++] = c; /* read up to end of line or buffer limit */
+ buff[i++] = cast_char(c); /* read up to end of line or buffer limit */
l_unlockfile(f);
luaL_addsize(&b, i);
} while (c != EOF && c != '\n'); /* repeat until end of line */
if (!chop && c == '\n') /* want a newline and have one? */
- luaL_addchar(&b, c); /* add ending newline to result */
+ luaL_addchar(&b, '\n'); /* add ending newline to result */
luaL_pushresult(&b); /* close buffer */
/* return ok if read something (either a newline or something else) */
return (c == '\n' || lua_rawlen(L, -1) > 0);
diff --git a/llex.c b/llex.c
@@ -350,7 +350,7 @@ static unsigned long readutf8esc (LexState *ls) {
int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */
save_and_next(ls); /* skip 'u' */
esccheck(ls, ls->current == '{', "missing '{'");
- r = gethexa(ls); /* must have at least one digit */
+ r = cast_ulong(gethexa(ls)); /* must have at least one digit */
while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
i++;
esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
diff --git a/llimits.h b/llimits.h
@@ -130,6 +130,7 @@ typedef LUAI_UACINT l_uacInt;
#define cast_num(i) cast(lua_Number, (i))
#define cast_int(i) cast(int, (i))
#define cast_uint(i) cast(unsigned int, (i))
+#define cast_ulong(i) cast(unsigned long, (i))
#define cast_byte(i) cast(lu_byte, (i))
#define cast_uchar(i) cast(unsigned char, (i))
#define cast_char(i) cast(char, (i))
@@ -151,6 +152,16 @@ typedef LUAI_UACINT l_uacInt;
#define l_castU2S(i) ((lua_Integer)(i))
#endif
+/*
+** cast a size_t to lua_Integer: These casts are always valid for
+** sizes of Lua objects (see MAX_SIZE)
+*/
+#define cast_st2S(sz) ((lua_Integer)(sz))
+
+/* Cast a ptrdiff_t to size_t, when it is known that the minuend
+** comes from the subtraend (the base)
+*/
+#define ct_diff2sz(df) ((size_t)(df))
/*
** Special type equivalent to '(void*)' for functions (to suppress some
diff --git a/lmathlib.c b/lmathlib.c
@@ -578,7 +578,7 @@ static int math_random (lua_State *L) {
low = 1;
up = luaL_checkinteger(L, 1);
if (up == 0) { /* single 0 as argument? */
- lua_pushinteger(L, I2UInt(rv)); /* full random integer */
+ lua_pushinteger(L, l_castU2S(I2UInt(rv))); /* full random integer */
return 1;
}
break;
@@ -594,7 +594,7 @@ static int math_random (lua_State *L) {
luaL_argcheck(L, low <= up, 1, "interval is empty");
/* project random integer into the interval [0, up - low] */
p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
- lua_pushinteger(L, p + (lua_Unsigned)low);
+ lua_pushinteger(L, l_castU2S(p) + low);
return 1;
}
@@ -608,8 +608,8 @@ static void setseed (lua_State *L, Rand64 *state,
state[3] = Int2I(0);
for (i = 0; i < 16; i++)
nextrand(state); /* discard initial values to "spread" seed */
- lua_pushinteger(L, n1);
- lua_pushinteger(L, n2);
+ lua_pushinteger(L, l_castU2S(n1));
+ lua_pushinteger(L, l_castU2S(n2));
}
@@ -621,8 +621,8 @@ static int math_randomseed (lua_State *L) {
n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */
}
else {
- n1 = luaL_checkinteger(L, 1);
- n2 = luaL_optinteger(L, 2, 0);
+ n1 = l_castS2U(luaL_checkinteger(L, 1));
+ n2 = l_castS2U(luaL_optinteger(L, 2, 0));
}
setseed(L, state->s, n1, n2);
return 2; /* return seeds */
diff --git a/lmem.c b/lmem.c
@@ -95,7 +95,7 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
void *luaM_growaux_ (lua_State *L, void *block, int nelems, int *psize,
- int size_elems, int limit, const char *what) {
+ unsigned size_elems, int limit, const char *what) {
void *newblock;
int size = *psize;
if (nelems + 1 <= size) /* does one extra element still fit? */
@@ -203,9 +203,9 @@ void *luaM_malloc_ (lua_State *L, size_t size, int tag) {
return NULL; /* that's all */
else {
global_State *g = G(L);
- void *newblock = firsttry(g, NULL, tag, size);
+ void *newblock = firsttry(g, NULL, cast_sizet(tag), size);
if (l_unlikely(newblock == NULL)) {
- newblock = tryagain(L, NULL, tag, size);
+ newblock = tryagain(L, NULL, cast_sizet(tag), size);
if (newblock == NULL)
luaM_error(L);
}
diff --git a/lmem.h b/lmem.h
@@ -85,7 +85,7 @@ LUAI_FUNC void *luaM_saferealloc_ (lua_State *L, void *block, size_t oldsize,
size_t size);
LUAI_FUNC void luaM_free_ (lua_State *L, void *block, size_t osize);
LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int nelems,
- int *size, int size_elem, int limit,
+ 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);
diff --git a/loadlib.c b/loadlib.c
@@ -288,13 +288,13 @@ static void setpath (lua_State *L, const char *fieldname,
luaL_Buffer b;
luaL_buffinit(L, &b);
if (path < dftmark) { /* is there a prefix before ';;'? */
- luaL_addlstring(&b, path, dftmark - path); /* add it */
+ luaL_addlstring(&b, path, ct_diff2sz(dftmark - path)); /* add it */
luaL_addchar(&b, *LUA_PATH_SEP);
}
luaL_addstring(&b, dft); /* add default */
if (dftmark < path + len - 2) { /* is there a suffix after ';;'? */
luaL_addchar(&b, *LUA_PATH_SEP);
- luaL_addlstring(&b, dftmark + 2, (path + len - 2) - dftmark);
+ luaL_addlstring(&b, dftmark + 2, ct_diff2sz((path + len - 2) - dftmark));
}
luaL_pushresult(&b);
}
@@ -543,7 +543,7 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
mark = strchr(modname, *LUA_IGMARK);
if (mark) {
int stat;
- openfunc = lua_pushlstring(L, modname, mark - modname);
+ openfunc = lua_pushlstring(L, modname, ct_diff2sz(mark - modname));
openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc);
stat = lookforfunc(L, filename, openfunc);
if (stat != ERRFUNC) return stat;
@@ -568,7 +568,7 @@ static int searcher_Croot (lua_State *L) {
const char *p = strchr(name, '.');
int stat;
if (p == NULL) return 0; /* is root */
- lua_pushlstring(L, name, p - name);
+ lua_pushlstring(L, name, ct_diff2sz(p - name));
filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
if (filename == NULL) return 1; /* root not found */
if ((stat = loadfunc(L, filename, name)) != 0) {
diff --git a/lobject.c b/lobject.c
@@ -32,7 +32,7 @@
/*
** Computes ceil(log2(x))
*/
-int luaO_ceillog2 (unsigned int x) {
+lu_byte luaO_ceillog2 (unsigned int x) {
static const lu_byte log_2[256] = { /* log_2[i - 1] = ceil(log2(i)) */
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
@@ -46,7 +46,7 @@ int luaO_ceillog2 (unsigned int x) {
int l = 0;
x--;
while (x >= 256) { l += 8; x >>= 8; }
- return l + log_2[x];
+ return cast_byte(l + log_2[x]);
}
/*
@@ -57,16 +57,19 @@ int luaO_ceillog2 (unsigned int x) {
** to signal that. So, the real value is (1xxxx) * 2^(eeee - 7 - 1) if
** eeee != 0, and (xxxx) * 2^-7 otherwise (subnormal numbers).
*/
-unsigned int luaO_codeparam (unsigned int p) {
+lu_byte luaO_codeparam (unsigned int p) {
if (p >= (cast(lu_mem, 0x1F) << (0xF - 7 - 1)) * 100u) /* overflow? */
return 0xFF; /* return maximum value */
else {
p = (cast(l_uint32, p) * 128 + 99) / 100; /* round up the division */
- if (p < 0x10) /* subnormal number? */
- return p; /* exponent bits are already zero; nothing else to do */
- else {
- int log = luaO_ceillog2(p + 1) - 5; /* preserve 5 bits */
- return ((p >> log) - 0x10) | ((log + 1) << 4);
+ if (p < 0x10) { /* subnormal number? */
+ /* exponent bits are already zero; nothing else to do */
+ return cast_byte(p);
+ }
+ else { /* p >= 0x10 implies ceil(log2(p + 1)) >= 5 */
+ /* preserve 5 bits in 'p' */
+ unsigned log = luaO_ceillog2(p + 1) - 5u;
+ return cast_byte(((p >> log) - 0x10) | ((log + 1) << 4));
}
}
}
@@ -81,7 +84,7 @@ unsigned int luaO_codeparam (unsigned int p) {
** more significant bits, as long as the multiplication does not
** overflow, so we check which order is best.
*/
-l_obj luaO_applyparam (unsigned int p, l_obj x) {
+l_obj luaO_applyparam (lu_byte p, l_obj x) {
unsigned int m = p & 0xF; /* mantissa */
int e = (p >> 4); /* exponent */
if (e > 0) { /* normalized? */
@@ -189,9 +192,9 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
}
-int luaO_hexavalue (int c) {
- if (lisdigit(c)) return c - '0';
- else return (ltolower(c) - 'a') + 10;
+lu_byte luaO_hexavalue (int c) {
+ if (lisdigit(c)) return cast_byte(c - '0');
+ else return cast_byte((ltolower(c) - 'a') + 10);
}
@@ -349,7 +352,7 @@ static const char *l_str2int (const char *s, lua_Integer *result) {
int d = *s - '0';
if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */
return NULL; /* do not accept it (as integer) */
- a = a * 10 + d;
+ a = a * 10 + cast_uint(d);
empty = 0;
}
}
@@ -373,7 +376,7 @@ size_t luaO_str2num (const char *s, TValue *o) {
}
else
return 0; /* conversion failed */
- return (e - s) + 1; /* success; return string size */
+ return ct_diff2sz(e - s) + 1; /* success; return string size */
}
@@ -409,7 +412,7 @@ int luaO_utf8esc (char *buff, unsigned long x) {
/*
** Convert a number object to a string, adding it to a buffer
*/
-static int tostringbuff (TValue *obj, char *buff) {
+static unsigned tostringbuff (TValue *obj, char *buff) {
int len;
lua_assert(ttisnumber(obj));
if (ttisinteger(obj))
@@ -421,7 +424,7 @@ static int tostringbuff (TValue *obj, char *buff) {
buff[len++] = '0'; /* adds '.0' to result */
}
}
- return len;
+ return cast_uint(len);
}
@@ -430,7 +433,7 @@ static int tostringbuff (TValue *obj, char *buff) {
*/
void luaO_tostring (lua_State *L, TValue *obj) {
char buff[MAXNUMBER2STR];
- int len = tostringbuff(obj, buff);
+ unsigned len = tostringbuff(obj, buff);
setsvalue(L, obj, luaS_newlstr(L, buff, len));
}
@@ -448,13 +451,13 @@ void luaO_tostring (lua_State *L, TValue *obj) {
** (LUA_IDSIZE + MAXNUMBER2STR) + a minimal space for basic messages,
** so that 'luaG_addinfo' can work directly on the buffer.
*/
-#define BUFVFS (LUA_IDSIZE + MAXNUMBER2STR + 95)
+#define BUFVFS cast_uint(LUA_IDSIZE + MAXNUMBER2STR + 95)
/* buffer used by 'luaO_pushvfstring' */
typedef struct BuffFS {
lua_State *L;
int pushed; /* true if there is a part of the result on the stack */
- int blen; /* length of partial string in 'space' */
+ unsigned blen; /* length of partial string in 'space' */
char space[BUFVFS]; /* holds last part of the result */
} BuffFS;
@@ -492,7 +495,7 @@ static void clearbuff (BuffFS *buff) {
** Get a space of size 'sz' in the buffer. If buffer has not enough
** space, empty it. 'sz' must fit in an empty buffer.
*/
-static char *getbuff (BuffFS *buff, int sz) {
+static char *getbuff (BuffFS *buff, unsigned sz) {
lua_assert(buff->blen <= BUFVFS); lua_assert(sz <= BUFVFS);
if (sz > BUFVFS - buff->blen) /* not enough space? */
clearbuff(buff);
@@ -509,9 +512,9 @@ static char *getbuff (BuffFS *buff, int sz) {
*/
static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
if (slen <= BUFVFS) { /* does string fit into buffer? */
- char *bf = getbuff(buff, cast_int(slen));
+ char *bf = getbuff(buff, cast_uint(slen));
memcpy(bf, str, slen); /* add string to buffer */
- addsize(buff, cast_int(slen));
+ addsize(buff, cast_uint(slen));
}
else { /* string larger than buffer */
clearbuff(buff); /* string comes after buffer's content */
@@ -525,7 +528,7 @@ static void addstr2buff (BuffFS *buff, const char *str, size_t slen) {
*/
static void addnum2buff (BuffFS *buff, TValue *num) {
char *numbuff = getbuff(buff, MAXNUMBER2STR);
- int len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
+ unsigned len = tostringbuff(num, numbuff); /* format number into 'numbuff' */
addsize(buff, len);
}
@@ -537,10 +540,10 @@ static void addnum2buff (BuffFS *buff, TValue *num) {
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
BuffFS buff; /* holds last part of the result */
const char *e; /* points to next '%' */
- buff.pushed = buff.blen = 0;
+ buff.pushed = 0; buff.blen = 0;
buff.L = L;
while ((e = strchr(fmt, '%')) != NULL) {
- addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
+ addstr2buff(&buff, fmt, ct_diff2sz(e - fmt)); /* add 'fmt' up to '%' */
switch (*(e + 1)) { /* conversion specifier */
case 's': { /* zero-terminated string */
const char *s = va_arg(argp, char *);
@@ -549,7 +552,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break;
}
case 'c': { /* an 'int' as a character */
- char c = cast_uchar(va_arg(argp, int));
+ char c = cast_char(va_arg(argp, int));
addstr2buff(&buff, &c, sizeof(char));
break;
}
@@ -572,17 +575,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break;
}
case 'p': { /* a pointer */
- const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
+ const unsigned sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
char *bf = getbuff(&buff, sz);
void *p = va_arg(argp, void *);
int len = lua_pointer2str(bf, sz, p);
- addsize(&buff, len);
+ addsize(&buff, cast_uint(len));
break;
}
- case 'U': { /* a 'long' as a UTF-8 sequence */
+ case 'U': { /* an 'unsigned long' as a UTF-8 sequence */
char bf[UTF8BUFFSZ];
- int len = luaO_utf8esc(bf, va_arg(argp, long));
- addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
+ int len = luaO_utf8esc(bf, va_arg(argp, unsigned long));
+ addstr2buff(&buff, bf + UTF8BUFFSZ - len, cast_uint(len));
break;
}
case '%': {
@@ -648,7 +651,8 @@ void luaO_chunkid (char *out, const char *source, size_t srclen) {
addstr(out, source, srclen); /* keep it */
}
else {
- if (nl != NULL) srclen = nl - source; /* stop at first newline */
+ if (nl != NULL)
+ srclen = ct_diff2sz(nl - source); /* stop at first newline */
if (srclen > bufflen) srclen = bufflen;
addstr(out, source, srclen);
addstr(out, RETS, LL(RETS));
diff --git a/lobject.h b/lobject.h
@@ -432,13 +432,13 @@ typedef struct TString {
/* get string length from 'TString *ts' */
#define tsslen(ts) \
- (strisshr(ts) ? cast_uint((ts)->shrlen) : (ts)->u.lnglen)
+ (strisshr(ts) ? cast_sizet((ts)->shrlen) : (ts)->u.lnglen)
/*
** Get string and length */
#define getlstr(ts, len) \
(strisshr(ts) \
- ? (cast_void((len) = (ts)->shrlen), rawgetshrstr(ts)) \
+ ? (cast_void((len) = cast_sizet((ts)->shrlen)), rawgetshrstr(ts)) \
: (cast_void((len) = (ts)->u.lnglen), (ts)->contents))
/* }================================================================== */
@@ -517,8 +517,8 @@ typedef struct Udata0 {
/* compute the offset of the memory area of a userdata */
#define udatamemoffset(nuv) \
- ((nuv) == 0 ? offsetof(Udata0, bindata) \
- : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
+ ((nuv) == 0 ? offsetof(Udata0, bindata) \
+ : offsetof(Udata, uv) + (sizeof(UValue) * (nuv)))
/* get the address of the memory block inside 'Udata' */
#define getudatamem(u) (cast_charp(u) + udatamemoffset((u)->nuvalue))
@@ -825,10 +825,10 @@ typedef struct Table {
** 'module' operation for hashing (size is always a power of 2)
*/
#define lmod(s,size) \
- (check_exp((size&(size-1))==0, (cast_int((s) & ((size)-1)))))
+ (check_exp((size&(size-1))==0, (cast_uint(s) & cast_uint((size)-1))))
-#define twoto(x) (1<<(x))
+#define twoto(x) (1u<<(x))
#define sizenode(t) (twoto((t)->lsizenode))
@@ -836,16 +836,16 @@ typedef struct Table {
#define UTF8BUFFSZ 8
LUAI_FUNC int luaO_utf8esc (char *buff, unsigned long x);
-LUAI_FUNC int luaO_ceillog2 (unsigned int x);
-LUAI_FUNC unsigned int luaO_codeparam (unsigned int p);
-LUAI_FUNC l_obj luaO_applyparam (unsigned int p, l_obj x);
+LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x);
+LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);
+LUAI_FUNC l_obj luaO_applyparam (lu_byte p, l_obj x);
LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
const TValue *p2, TValue *res);
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
const TValue *p2, StkId res);
LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
-LUAI_FUNC int luaO_hexavalue (int c);
+LUAI_FUNC lu_byte luaO_hexavalue (int c);
LUAI_FUNC void luaO_tostring (lua_State *L, TValue *obj);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
va_list argp);
diff --git a/lopcodes.h b/lopcodes.h
@@ -72,8 +72,11 @@ enum OpMode {iABC, ivABC, iABx, iAsBx, iAx, isJ};
** so they must fit in ints.
*/
-/* Check whether type 'int' has at least 'b' bits ('b' < 32) */
-#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)
+/*
+** Check whether type 'int' has at least 'b' + 1 bits.
+** 'b' < 32; +1 for the sign bit.
+*/
+#define L_INTHASBITS(b) ((UINT_MAX >> (b)) >= 1)
#if L_INTHASBITS(SIZE_Bx)
diff --git a/loslib.c b/loslib.c
@@ -275,7 +275,7 @@ static int getfield (lua_State *L, const char *key, int d, int delta) {
static const char *checkoption (lua_State *L, const char *conv,
ptrdiff_t convlen, char *buff) {
const char *option = LUA_STRFTIMEOPTIONS;
- int oplen = 1; /* length of options being checked */
+ unsigned oplen = 1; /* length of options being checked */
for (; *option != '\0' && oplen <= convlen; option += oplen) {
if (*option == '|') /* next block? */
oplen++; /* will check options with next length (+1) */
diff --git a/lparser.c b/lparser.c
@@ -172,7 +172,8 @@ static void codename (LexState *ls, expdesc *e) {
** Register a new local variable in the active 'Proto' (for debug
** information).
*/
-static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
+static short registerlocalvar (LexState *ls, FuncState *fs,
+ TString *varname) {
Proto *f = fs->f;
int oldsize = f->sizelocvars;
luaM_growvector(ls->L, f->locvars, fs->ndebugvars, f->sizelocvars,
@@ -190,7 +191,7 @@ static int registerlocalvar (LexState *ls, FuncState *fs, TString *varname) {
** Create a new local variable with the given 'name' and given 'kind'.
** Return its index in the function.
*/
-static int new_localvarkind (LexState *ls, TString *name, int kind) {
+static int new_localvarkind (LexState *ls, TString *name, lu_byte kind) {
lua_State *L = ls->L;
FuncState *fs = ls->fs;
Dyndata *dyd = ls->dyd;
@@ -234,11 +235,11 @@ static Vardesc *getlocalvardesc (FuncState *fs, int vidx) {
** register. For that, search for the highest variable below that level
** that is in a register and uses its register index ('ridx') plus one.
*/
-static int reglevel (FuncState *fs, int nvar) {
+static lu_byte reglevel (FuncState *fs, int nvar) {
while (nvar-- > 0) {
Vardesc *vd = getlocalvardesc(fs, nvar); /* get previous variable */
if (vd->vd.kind != RDKCTC) /* is in a register? */
- return vd->vd.ridx + 1;
+ return cast_byte(vd->vd.ridx + 1);
}
return 0; /* no variables in registers */
}
@@ -248,7 +249,7 @@ static int reglevel (FuncState *fs, int nvar) {
** Return the number of variables in the register stack for the given
** function.
*/
-int luaY_nvarstack (FuncState *fs) {
+lu_byte luaY_nvarstack (FuncState *fs) {
return reglevel(fs, fs->nactvar);
}
@@ -274,7 +275,7 @@ static LocVar *localdebuginfo (FuncState *fs, int vidx) {
static void init_var (FuncState *fs, expdesc *e, int vidx) {
e->f = e->t = NO_JUMP;
e->k = VLOCAL;
- e->u.var.vidx = vidx;
+ e->u.var.vidx = cast(unsigned short, vidx);
e->u.var.ridx = getlocalvardesc(fs, vidx)->vd.ridx;
}
@@ -323,7 +324,7 @@ static void adjustlocalvars (LexState *ls, int nvars) {
for (i = 0; i < nvars; i++) {
int vidx = fs->nactvar++;
Vardesc *var = getlocalvardesc(fs, vidx);
- var->vd.ridx = reglevel++;
+ var->vd.ridx = cast_byte(reglevel++);
var->vd.pidx = registerlocalvar(ls, fs, var->vd.name);
}
}
@@ -505,7 +506,7 @@ static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {
if (needed > 0)
luaK_reserveregs(fs, needed); /* registers for extra values */
else /* adding 'needed' is actually a subtraction */
- fs->freereg += needed; /* remove extra values */
+ fs->freereg = cast_byte(fs->freereg + needed); /* remove extra values */
}
@@ -682,7 +683,7 @@ static void leaveblock (FuncState *fs) {
BlockCnt *bl = fs->bl;
LexState *ls = fs->ls;
int hasclose = 0;
- int stklevel = reglevel(fs, bl->nactvar); /* level outside the block */
+ lu_byte stklevel = reglevel(fs, bl->nactvar); /* level outside the block */
removevars(fs, bl->nactvar); /* remove block locals */
lua_assert(bl->nactvar == fs->nactvar); /* back to level on entry */
if (bl->isloop) /* has to fix pending breaks? */
@@ -856,7 +857,7 @@ typedef struct ConsControl {
static void recfield (LexState *ls, ConsControl *cc) {
/* recfield -> (NAME | '['exp']') = exp */
FuncState *fs = ls->fs;
- int reg = ls->fs->freereg;
+ lu_byte reg = ls->fs->freereg;
expdesc tab, key, val;
if (ls->t.token == TK_NAME) {
checklimit(fs, cc->nh, INT_MAX, "items in a constructor");
@@ -939,7 +940,7 @@ static void field (LexState *ls, ConsControl *cc) {
static int maxtostore (FuncState *fs) {
int numfreeregs = MAX_FSTACK - fs->freereg;
if (numfreeregs >= 160) /* "lots" of registers? */
- return numfreeregs / 5u; /* use up to 1/5 of them */
+ return numfreeregs / 5; /* use up to 1/5 of them */
else if (numfreeregs >= 80) /* still "enough" registers? */
return 10; /* one 'SETLIST' instruction for each 10 values */
else /* save registers for potential more nesting */
@@ -1090,8 +1091,9 @@ static void funcargs (LexState *ls, expdesc *f) {
}
init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));
luaK_fixline(fs, line);
- fs->freereg = base+1; /* call removes function and arguments and leaves
- one result (unless changed later) */
+ /* call removes function and arguments and leaves one result (unless
+ changed later) */
+ fs->freereg = cast_byte(base + 1);
}
@@ -1356,7 +1358,7 @@ struct LHS_assign {
*/
static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {
FuncState *fs = ls->fs;
- int extra = fs->freereg; /* eventual position to save local variable */
+ lu_byte extra = fs->freereg; /* eventual position to save local variable */
int conflict = 0;
for (; lh; lh = lh->prev) { /* check all previous assignments */
if (vkisindexed(lh->v.k)) { /* assignment to table field? */
@@ -1723,7 +1725,7 @@ static void localfunc (LexState *ls) {
}
-static int getlocalattribute (LexState *ls) {
+static lu_byte getlocalattribute (LexState *ls) {
/* ATTRIB -> ['<' Name '>'] */
if (testnext(ls, '<')) {
TString *ts = str_checkname(ls);
@@ -1760,7 +1762,7 @@ static void localstat (LexState *ls) {
expdesc e;
do {
TString *vname = str_checkname(ls);
- int kind = getlocalattribute(ls);
+ lu_byte kind = getlocalattribute(ls);
vidx = new_localvarkind(ls, vname, kind);
if (kind == RDKTOCLOSE) { /* to-be-closed? */
if (toclose != -1) /* one already present? */
diff --git a/lparser.h b/lparser.h
@@ -163,7 +163,7 @@ typedef struct FuncState {
} FuncState;
-LUAI_FUNC int luaY_nvarstack (FuncState *fs);
+LUAI_FUNC lu_byte luaY_nvarstack (FuncState *fs);
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);
diff --git a/lstate.c b/lstate.c
@@ -190,7 +190,8 @@ static void freestack (lua_State *L) {
L->ci = &L->base_ci; /* free the entire 'ci' list */
freeCI(L);
lua_assert(L->nci == 0);
- luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
+ /* free stack */
+ luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK));
}
@@ -266,7 +267,7 @@ static void close_state (lua_State *L) {
luaC_freeallobjects(L); /* collect all objects */
luai_userstateclose(L);
}
- luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
+ luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
freestack(L);
lua_assert(g->totalbytes == sizeof(LG));
lua_assert(gettotalobjs(g) == 1);
diff --git a/lstate.h b/lstate.h
@@ -259,7 +259,7 @@ struct CallInfo {
#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7)
#define setcistrecst(ci,st) \
check_exp(((st) & 7) == (st), /* status must fit in three bits */ \
- ((ci)->callstatus = ((ci)->callstatus & ~(7 << CIST_RECST)) \
+ ((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \
| (cast(l_uint32, st) << CIST_RECST)))
diff --git a/lstring.c b/lstring.c
@@ -164,7 +164,7 @@ size_t luaS_sizelngstr (size_t len, int kind) {
/*
** creates a new string object
*/
-static TString *createstrobj (lua_State *L, size_t totalsize, int tag,
+static TString *createstrobj (lua_State *L, size_t totalsize, lu_byte tag,
unsigned h) {
TString *ts;
GCObject *o;
@@ -233,7 +233,7 @@ static TString *internshrstr (lua_State *L, const char *str, size_t l) {
list = &tb->hash[lmod(h, tb->size)]; /* rehash with new size */
}
ts = createstrobj(L, sizestrshr(l), LUA_VSHRSTR, h);
- ts->shrlen = cast_byte(l);
+ ts->shrlen = cast(ls_byte, l);
getshrstr(ts)[l] = '\0'; /* ending 0 */
memcpy(getshrstr(ts), str, l * sizeof(char));
ts->u.hnext = *list;
@@ -283,7 +283,7 @@ TString *luaS_new (lua_State *L, const char *str) {
}
-Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
+Udata *luaS_newudata (lua_State *L, size_t s, unsigned short nuvalue) {
Udata *u;
int i;
GCObject *o;
@@ -301,7 +301,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue) {
struct NewExt {
- int kind;
+ ls_byte kind;
const char *s;
size_t len;
TString *ts; /* output */
diff --git a/lstring.h b/lstring.h
@@ -61,7 +61,8 @@ LUAI_FUNC void luaS_resize (lua_State *L, int newsize);
LUAI_FUNC void luaS_clearcache (global_State *g);
LUAI_FUNC void luaS_init (lua_State *L);
LUAI_FUNC void luaS_remove (lua_State *L, TString *ts);
-LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, int nuvalue);
+LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s,
+ unsigned short nuvalue);
LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
LUAI_FUNC TString *luaS_new (lua_State *L, const char *str);
LUAI_FUNC TString *luaS_createlngstrobj (lua_State *L, size_t l);
diff --git a/lstrlib.c b/lstrlib.c
@@ -113,7 +113,7 @@ static int str_lower (lua_State *L) {
const char *s = luaL_checklstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i=0; i<l; i++)
- p[i] = tolower(cast_uchar(s[i]));
+ p[i] = cast_char(tolower(cast_uchar(s[i])));
luaL_pushresultsize(&b, l);
return 1;
}
@@ -126,7 +126,7 @@ static int str_upper (lua_State *L) {
const char *s = luaL_checklstring(L, 1, &l);
char *p = luaL_buffinitsize(L, &b, l);
for (i=0; i<l; i++)
- p[i] = toupper(cast_uchar(s[i]));
+ p[i] = cast_char(toupper(cast_uchar(s[i])));
luaL_pushresultsize(&b, l);
return 1;
}
@@ -139,7 +139,7 @@ static int str_rep (lua_State *L) {
const char *sep = luaL_optlstring(L, 3, "", &lsep);
if (n <= 0)
lua_pushliteral(L, "");
- else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / n))
+ else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / cast_sizet(n)))
return luaL_error(L, "resulting string too large");
else {
size_t totallen = ((size_t)n * (l + lsep)) - lsep;
@@ -172,7 +172,7 @@ static int str_byte (lua_State *L) {
n = (int)(pose - posi) + 1;
luaL_checkstack(L, n, "string slice too long");
for (i=0; i<n; i++)
- lua_pushinteger(L, cast_uchar(s[posi+i-1]));
+ lua_pushinteger(L, cast_uchar(s[posi + cast_uint(i) - 1]));
return n;
}
@@ -181,13 +181,13 @@ static int str_char (lua_State *L) {
int n = lua_gettop(L); /* number of arguments */
int i;
luaL_Buffer b;
- char *p = luaL_buffinitsize(L, &b, n);
+ char *p = luaL_buffinitsize(L, &b, cast_uint(n));
for (i=1; i<=n; i++) {
lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
- p[i - 1] = cast_uchar(c);
+ p[i - 1] = cast_char(cast_uchar(c));
}
- luaL_pushresultsize(&b, n);
+ luaL_pushresultsize(&b, cast_uint(n));
return 1;
}
@@ -352,10 +352,10 @@ typedef struct MatchState {
const char *p_end; /* end ('\0') of pattern */
lua_State *L;
int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
- unsigned char level; /* total number of captures (finished or unfinished) */
+ int level; /* total number of captures (finished or unfinished) */
struct {
const char *init;
- ptrdiff_t len;
+ ptrdiff_t len; /* length or special value (CAP_*) */
} capture[LUA_MAXCAPTURES];
} MatchState;
@@ -550,7 +550,7 @@ static const char *end_capture (MatchState *ms, const char *s,
static const char *match_capture (MatchState *ms, const char *s, int l) {
size_t len;
l = check_capture(ms, l);
- len = ms->capture[l].len;
+ len = cast_sizet(ms->capture[l].len);
if ((size_t)(ms->src_end-s) >= len &&
memcmp(ms->capture[l].init, s, len) == 0)
return s+len;
@@ -674,7 +674,7 @@ static const char *lmemfind (const char *s1, size_t l1,
if (memcmp(init, s2+1, l2) == 0)
return init-1;
else { /* correct 'l1' and 's1' to try again */
- l1 -= init-s1;
+ l1 -= ct_diff2sz(init - s1);
s1 = init;
}
}
@@ -690,13 +690,13 @@ static const char *lmemfind (const char *s1, size_t l1,
** its length and put its address in '*cap'. If it is an integer
** (a position), push it on the stack and return CAP_POSITION.
*/
-static size_t get_onecapture (MatchState *ms, int i, const char *s,
+static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s,
const char *e, const char **cap) {
if (i >= ms->level) {
if (l_unlikely(i != 0))
luaL_error(ms->L, "invalid capture index %%%d", i + 1);
*cap = s;
- return e - s;
+ return (e - s);
}
else {
ptrdiff_t capl = ms->capture[i].len;
@@ -718,7 +718,7 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
const char *cap;
ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
if (l != CAP_POSITION)
- lua_pushlstring(ms->L, cap, l);
+ lua_pushlstring(ms->L, cap, cast_sizet(l));
/* else position was already pushed */
}
@@ -776,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) {
const char *s2 = lmemfind(s + init, ls - init, p, lp);
if (s2) {
lua_pushinteger(L, (s2 - s) + 1);
- lua_pushinteger(L, (s2 - s) + lp);
+ lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp));
return 2;
}
}
@@ -866,23 +866,23 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
const char *news = lua_tolstring(L, 3, &l);
const char *p;
while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
- luaL_addlstring(b, news, p - news);
+ luaL_addlstring(b, news, ct_diff2sz(p - news));
p++; /* skip ESC */
if (*p == L_ESC) /* '%%' */
luaL_addchar(b, *p);
else if (*p == '0') /* '%0' */
- luaL_addlstring(b, s, e - s);
+ luaL_addlstring(b, s, ct_diff2sz(e - s));
else if (isdigit(cast_uchar(*p))) { /* '%n' */
const char *cap;
ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
if (resl == CAP_POSITION)
luaL_addvalue(b); /* add position to accumulated result */
else
- luaL_addlstring(b, cap, resl);
+ luaL_addlstring(b, cap, cast_sizet(resl));
}
else
luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
- l -= p + 1 - news;
+ l -= ct_diff2sz(p + 1 - news);
news = p + 1;
}
luaL_addlstring(b, news, l);
@@ -917,7 +917,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
}
if (!lua_toboolean(L, -1)) { /* nil or false? */
lua_pop(L, 1); /* remove value */
- luaL_addlstring(b, s, e - s); /* keep original text */
+ luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */
return 0; /* no changes */
}
else if (l_unlikely(!lua_isstring(L, -1)))
@@ -936,7 +936,8 @@ static int str_gsub (lua_State *L) {
const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
const char *lastmatch = NULL; /* end of last match */
int tr = lua_type(L, 3); /* replacement type */
- lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
+ /* max replacements */
+ lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1);
int anchor = (*p == '^');
lua_Integer n = 0; /* replacement count */
int changed = 0; /* change flag */
@@ -966,7 +967,7 @@ static int str_gsub (lua_State *L) {
if (!changed) /* no changes? */
lua_pushvalue(L, 1); /* return original string */
else { /* something changed */
- luaL_addlstring(&b, src, ms.src_end-src);
+ luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src));
luaL_pushresult(&b); /* create and return new string */
}
lua_pushinteger(L, n); /* number of substitutions */
@@ -1004,15 +1005,15 @@ static int str_gsub (lua_State *L) {
/*
** Add integer part of 'x' to buffer and return new 'x'
*/
-static lua_Number adddigit (char *buff, int n, lua_Number x) {
+static lua_Number adddigit (char *buff, unsigned n, lua_Number x) {
lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
int d = (int)dd;
- buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
+ buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
return x - dd; /* return what is left */
}
-static int num2straux (char *buff, int sz, lua_Number x) {
+static int num2straux (char *buff, unsigned sz, lua_Number x) {
/* if 'inf' or 'NaN', format it like '%g' */
if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
@@ -1023,7 +1024,7 @@ static int num2straux (char *buff, int sz, lua_Number x) {
else {
int e;
lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
- int n = 0; /* character count */
+ unsigned n = 0; /* character count */
if (m < 0) { /* is number negative? */
buff[n++] = '-'; /* add sign */
m = -m; /* make it positive */
@@ -1037,20 +1038,20 @@ static int num2straux (char *buff, int sz, lua_Number x) {
m = adddigit(buff, n++, m * 16);
} while (m > 0);
}
- n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */
+ n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */
lua_assert(n < sz);
- return n;
+ return cast_int(n);
}
}
-static int lua_number2strx (lua_State *L, char *buff, int sz,
+static int lua_number2strx (lua_State *L, char *buff, unsigned sz,
const char *fmt, lua_Number x) {
int n = num2straux(buff, sz, x);
if (fmt[SIZELENMOD] == 'A') {
int i;
for (i = 0; i < n; i++)
- buff[i] = toupper(cast_uchar(buff[i]));
+ buff[i] = cast_char(toupper(cast_uchar(buff[i])));
}
else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
@@ -1151,9 +1152,9 @@ static int quotefloat (lua_State *L, char *buff, lua_Number n) {
int nb = lua_number2strx(L, buff, MAX_ITEM,
"%" LUA_NUMBER_FRMLEN "a", n);
/* ensures that 'buff' string uses a dot as the radix character */
- if (memchr(buff, '.', nb) == NULL) { /* no dot? */
+ if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */
char point = lua_getlocaledecpoint(); /* try locale point */
- char *ppoint = (char *)memchr(buff, point, nb);
+ char *ppoint = (char *)memchr(buff, point, cast_uint(nb));
if (ppoint) *ppoint = '.'; /* change it to a dot */
}
return nb;
@@ -1183,7 +1184,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
: LUA_INTEGER_FMT; /* else use default format */
nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
}
- luaL_addsize(b, nb);
+ luaL_addsize(b, cast_uint(nb));
break;
}
case LUA_TNIL: case LUA_TBOOLEAN: {
@@ -1277,7 +1278,7 @@ static int str_format (lua_State *L) {
luaL_addchar(&b, *strfrmt++); /* %% */
else { /* format item */
char form[MAX_FORMAT]; /* to store the format ('%...') */
- int maxitem = MAX_ITEM; /* maximum length for the result */
+ unsigned maxitem = MAX_ITEM; /* maximum length for the result */
char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */
int nb = 0; /* number of bytes in result */
if (++arg > top)
@@ -1360,8 +1361,8 @@ static int str_format (lua_State *L) {
return luaL_error(L, "invalid conversion '%s' to 'format'", form);
}
}
- lua_assert(nb < maxitem);
- luaL_addsize(&b, nb);
+ lua_assert(cast_uint(nb) < maxitem);
+ luaL_addsize(&b, cast_uint(nb));
}
}
luaL_pushresult(&b);
@@ -1409,7 +1410,7 @@ static const union {
typedef struct Header {
lua_State *L;
int islittle;
- int maxalign;
+ unsigned maxalign;
} Header;
@@ -1443,7 +1444,7 @@ static size_t getnum (const char **fmt, size_t df) {
else {
size_t a = 0;
do {
- a = a*10 + (*((*fmt)++) - '0');
+ a = a*10 + cast_uint(*((*fmt)++) - '0');
} while (digit(**fmt) && a <= (MAX_SIZE - 9)/10);
return a;
}
@@ -1454,12 +1455,12 @@ static size_t getnum (const char **fmt, size_t df) {
** Read an integer numeral and raises an error if it is larger
** than the maximum size of integers.
*/
-static int getnumlimit (Header *h, const char **fmt, int df) {
+static unsigned getnumlimit (Header *h, const char **fmt, size_t df) {
size_t sz = getnum(fmt, df);
if (l_unlikely((sz - 1u) >= MAXINTSIZE))
- return luaL_error(h->L, "integral size (%d) out of limits [1,%d]",
- sz, MAXINTSIZE);
- return cast_int(sz);
+ return cast_uint(luaL_error(h->L,
+ "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE));
+ return cast_uint(sz);
}
@@ -1510,7 +1511,7 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) {
case '>': h->islittle = 0; break;
case '=': h->islittle = nativeendian.little; break;
case '!': {
- const int maxalign = offsetof(struct cD, u);
+ const size_t maxalign = offsetof(struct cD, u);
h->maxalign = getnumlimit(h, fmt, maxalign);
break;
}
@@ -1529,8 +1530,8 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) {
** the maximum alignment ('maxalign'). Kchar option needs no alignment
** despite its size.
*/
-static KOption getdetails (Header *h, size_t totalsize,
- const char **fmt, size_t *psize, int *ntoalign) {
+static KOption getdetails (Header *h, size_t totalsize, const char **fmt,
+ size_t *psize, unsigned *ntoalign) {
KOption opt = getoption(h, fmt, psize);
size_t align = *psize; /* usually, alignment follows size */
if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
@@ -1540,11 +1541,15 @@ static KOption getdetails (Header *h, size_t totalsize,
if (align <= 1 || opt == Kchar) /* need no alignment? */
*ntoalign = 0;
else {
- if (align > cast_sizet(h->maxalign)) /* enforce maximum alignment */
+ if (align > h->maxalign) /* enforce maximum alignment */
align = h->maxalign;
if (l_unlikely(!ispow2(align))) /* not a power of 2? */
luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
- *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
+ else {
+ /* 'szmoda' = totalsize % align */
+ unsigned szmoda = cast_uint(totalsize & (align - 1));
+ *ntoalign = cast_uint((align - szmoda) & (align - 1));
+ }
}
return opt;
}
@@ -1557,9 +1562,9 @@ static KOption getdetails (Header *h, size_t totalsize,
** bytes if necessary (by default they would be zeros).
*/
static void packint (luaL_Buffer *b, lua_Unsigned n,
- int islittle, int size, int neg) {
+ int islittle, unsigned size, int neg) {
char *buff = luaL_prepbuffsize(b, size);
- int i;
+ unsigned i;
buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
for (i = 1; i < size; i++) {
n >>= NB;
@@ -1578,7 +1583,7 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
** given 'islittle' is different from native endianness.
*/
static void copywithendian (char *dest, const char *src,
- int size, int islittle) {
+ unsigned size, int islittle) {
if (islittle == nativeendian.little)
memcpy(dest, src, size);
else {
@@ -1599,7 +1604,7 @@ static int str_pack (lua_State *L) {
lua_pushnil(L); /* mark to separate arguments from string buffer */
luaL_buffinit(L, &b);
while (*fmt != '\0') {
- int ntoalign;
+ unsigned ntoalign;
size_t size;
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg,
@@ -1615,7 +1620,7 @@ static int str_pack (lua_State *L) {
lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
}
- packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0));
+ packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0));
break;
}
case Kuint: { /* unsigned integers */
@@ -1623,7 +1628,7 @@ static int str_pack (lua_State *L) {
if (size < SZINT) /* need overflow check? */
luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
arg, "unsigned overflow");
- packint(&b, (lua_Unsigned)n, h.islittle, size, 0);
+ packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0);
break;
}
case Kfloat: { /* C float */
@@ -1669,7 +1674,8 @@ static int str_pack (lua_State *L) {
luaL_argcheck(L, size >= sizeof(lua_Unsigned) ||
len < ((lua_Unsigned)1 << (size * NB)),
arg, "string length does not fit in given size");
- packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */
+ /* pack length */
+ packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0);
luaL_addlstring(&b, s, len);
totalsize += len;
break;
@@ -1697,20 +1703,20 @@ static int str_pack (lua_State *L) {
static int str_packsize (lua_State *L) {
Header h;
const char *fmt = luaL_checkstring(L, 1); /* format string */
- lua_Integer totalsize = 0; /* accumulate total size of result */
+ size_t totalsize = 0; /* accumulate total size of result */
initheader(L, &h);
while (*fmt != '\0') {
- int ntoalign;
+ unsigned ntoalign;
size_t size;
KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
"variable-length format");
size += ntoalign; /* total space used by option */
- luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - cast(lua_Integer, size),
+ luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size,
1, "format result too large");
totalsize += size;
}
- lua_pushinteger(L, totalsize);
+ lua_pushinteger(L, cast_st2S(totalsize));
return 1;
}
@@ -1759,7 +1765,7 @@ static int str_unpack (lua_State *L) {
luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
initheader(L, &h);
while (*fmt != '\0') {
- int ntoalign;
+ unsigned ntoalign;
size_t size;
KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
luaL_argcheck(L, ntoalign + size <= ld - pos, 2,
@@ -1771,8 +1777,8 @@ static int str_unpack (lua_State *L) {
switch (opt) {
case Kint:
case Kuint: {
- lua_Integer res = unpackint(L, data + pos, h.islittle, size,
- (opt == Kint));
+ lua_Integer res = unpackint(L, data + pos, h.islittle,
+ cast_int(size), (opt == Kint));
lua_pushinteger(L, res);
break;
}
@@ -1800,7 +1806,7 @@ static int str_unpack (lua_State *L) {
}
case Kstring: {
lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos,
- h.islittle, size, 0);
+ h.islittle, cast_int(size), 0);
luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
lua_pushlstring(L, data + pos + size, len);
pos += len; /* skip string */
@@ -1820,7 +1826,7 @@ static int str_unpack (lua_State *L) {
}
pos += size;
}
- lua_pushinteger(L, pos + 1); /* next position */
+ lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */
return n + 1;
}
diff --git a/ltable.c b/ltable.c
@@ -109,7 +109,7 @@ typedef union {
** for other types, it is better to avoid modulo by power of 2, as
** they can have many 2 factors.
*/
-#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
+#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1u)|1u))))
#define hashstr(t,str) hashpow2(t, (str)->hash)
@@ -139,7 +139,7 @@ static const TValue absentkey = {ABSTKEYCONSTANT};
static Node *hashint (const Table *t, lua_Integer i) {
lua_Unsigned ui = l_castS2U(i);
if (ui <= cast_uint(INT_MAX))
- return hashmod(t, cast_int(ui));
+ return gnode(t, cast_int(ui) % cast_int((sizenode(t)-1) | 1));
else
return hashmod(t, ui);
}
@@ -159,7 +159,7 @@ static Node *hashint (const Table *t, lua_Integer i) {
** INT_MIN.
*/
#if !defined(l_hashfloat)
-static int l_hashfloat (lua_Number n) {
+static unsigned l_hashfloat (lua_Number n) {
int i;
lua_Integer ni;
n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
@@ -169,7 +169,7 @@ static int l_hashfloat (lua_Number n) {
}
else { /* normal case */
unsigned int u = cast_uint(i) + cast_uint(ni);
- return cast_int(u <= cast_uint(INT_MAX) ? u : ~u);
+ return (u <= cast_uint(INT_MAX) ? u : ~u);
}
}
#endif
@@ -370,7 +370,7 @@ static unsigned findindex (lua_State *L, Table *t, TValue *key,
const TValue *n = getgeneric(t, key, 1);
if (l_unlikely(isabstkey(n)))
luaG_runerror(L, "invalid key to 'next'"); /* key not found */
- i = cast_int(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
+ i = cast_uint(nodefromval(n) - gnode(t, 0)); /* key index in hash table */
/* hash elements are numbered after array ones */
return (i + 1) + asize;
}
@@ -381,14 +381,14 @@ int luaH_next (lua_State *L, Table *t, StkId key) {
unsigned int asize = luaH_realasize(t);
unsigned int i = findindex(L, t, s2v(key), asize); /* find original key */
for (; i < asize; i++) { /* try first array part */
- int tag = *getArrTag(t, i);
+ lu_byte tag = *getArrTag(t, i);
if (!tagisempty(tag)) { /* a non-empty entry? */
- setivalue(s2v(key), i + 1);
+ setivalue(s2v(key), cast_int(i) + 1);
farr2val(t, i, tag, s2v(key + 1));
return 1;
}
}
- for (i -= asize; cast_int(i) < sizenode(t); i++) { /* hash part */
+ for (i -= asize; i < sizenode(t); i++) { /* hash part */
if (!isempty(gval(gnode(t, i)))) { /* a non-empty entry? */
Node *n = gnode(t, i);
getnodekey(L, s2v(key), n);
@@ -485,7 +485,7 @@ static unsigned computesizes (unsigned nums[], unsigned *pna) {
}
-static int countint (lua_Integer key, unsigned int *nums) {
+static unsigned countint (lua_Integer key, unsigned int *nums) {
unsigned int k = arrayindex(key);
if (k != 0) { /* is 'key' an appropriate array index? */
nums[luaO_ceillog2(k)]++; /* count as such */
@@ -496,7 +496,7 @@ static int countint (lua_Integer key, unsigned int *nums) {
}
-l_sinline int arraykeyisempty (const Table *t, lua_Integer key) {
+l_sinline int arraykeyisempty (const Table *t, lua_Unsigned key) {
int tag = *getArrTag(t, key - 1);
return tagisempty(tag);
}
@@ -534,10 +534,10 @@ static unsigned numusearray (const Table *t, unsigned *nums) {
}
-static int numusehash (const Table *t, unsigned *nums, unsigned *pna) {
- int totaluse = 0; /* total number of elements */
- int ause = 0; /* elements added to 'nums' (can go to array part) */
- int i = sizenode(t);
+static unsigned numusehash (const Table *t, unsigned *nums, unsigned *pna) {
+ unsigned totaluse = 0; /* total number of elements */
+ unsigned ause = 0; /* elements added to 'nums' (can go to array part) */
+ unsigned i = sizenode(t);
while (i--) {
Node *n = &t->node[i];
if (!isempty(gval(n))) {
@@ -646,8 +646,8 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
** (Re)insert all elements from the hash part of 'ot' into table 't'.
*/
static void reinsert (lua_State *L, Table *ot, Table *t) {
- int j;
- int size = sizenode(ot);
+ unsigned j;
+ unsigned size = sizenode(ot);
for (j = 0; j < size; j++) {
Node *old = gnode(ot, j);
if (!isempty(gval(old))) {
@@ -673,10 +673,10 @@ static void exchangehashpart (Table *t1, Table *t2) {
int bitdummy1 = t1->flags & BITDUMMY;
t1->lsizenode = t2->lsizenode;
t1->node = t2->node;
- t1->flags = (t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY);
+ t1->flags = cast_byte((t1->flags & NOTBITDUMMY) | (t2->flags & BITDUMMY));
t2->lsizenode = lsizenode;
t2->node = node;
- t2->flags = (t2->flags & NOTBITDUMMY) | bitdummy1;
+ t2->flags = cast_byte((t2->flags & NOTBITDUMMY) | bitdummy1);
}
@@ -689,11 +689,12 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize,
unsigned i;
t->alimit = newasize; /* pretend array has new size... */
for (i = newasize; i < oldasize; i++) { /* traverse vanishing slice */
- int tag = *getArrTag(t, i);
+ lu_byte tag = *getArrTag(t, i);
if (!tagisempty(tag)) { /* a non-empty entry? */
TValue aux;
farr2val(t, i, tag, &aux); /* copy entry into 'aux' */
- luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */
+ /* re-insert it into the table */
+ luaH_setint(L, t, cast_int(i) + 1, &aux);
}
}
t->alimit = oldasize; /* restore current size... */
@@ -756,7 +757,7 @@ void luaH_resize (lua_State *L, Table *t, unsigned newasize,
void luaH_resizearray (lua_State *L, Table *t, unsigned int nasize) {
- int nsize = allocsizenode(t);
+ unsigned nsize = allocsizenode(t);
luaH_resize(L, t, nasize, nsize);
}
@@ -768,7 +769,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
unsigned int na; /* number of keys in the array part */
unsigned int nums[MAXABITS + 1];
int i;
- int totaluse;
+ unsigned totaluse;
for (i = 0; i <= MAXABITS; i++) nums[i] = 0; /* reset counts */
setlimittosize(t);
na = numusearray(t, nums); /* count keys in array part */
@@ -795,7 +796,7 @@ Table *luaH_new (lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_VTABLE, sizeof(Table));
Table *t = gco2t(o);
t->metatable = NULL;
- t->flags = cast_byte(maskflags); /* table has no metamethod fields */
+ t->flags = maskflags; /* table has no metamethod fields */
t->array = NULL;
t->alimit = 0;
setnodevector(L, t, 0);
@@ -825,7 +826,7 @@ static Node *getfreepos (Table *t) {
}
else { /* no 'lastfree' information */
if (!isdummy(t)) {
- int i = sizenode(t);
+ unsigned i = sizenode(t);
while (i--) { /* do a linear search */
Node *free = gnode(t, i);
if (keyisnil(free))
@@ -919,13 +920,13 @@ static const TValue *getintfromhash (Table *t, lua_Integer key) {
}
-static int hashkeyisempty (Table *t, lua_Integer key) {
- const TValue *val = getintfromhash(t, key);
+static int hashkeyisempty (Table *t, lua_Unsigned key) {
+ const TValue *val = getintfromhash(t, l_castU2S(key));
return isempty(val);
}
-static int finishnodeget (const TValue *val, TValue *res) {
+static lu_byte finishnodeget (const TValue *val, TValue *res) {
if (!ttisnil(val)) {
setobj(((lua_State*)NULL), res, val);
}
@@ -933,9 +934,9 @@ static int finishnodeget (const TValue *val, TValue *res) {
}
-int luaH_getint (Table *t, lua_Integer key, TValue *res) {
+lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res) {
if (keyinarray(t, key)) {
- int tag = *getArrTag(t, key - 1);
+ lu_byte tag = *getArrTag(t, key - 1);
if (!tagisempty(tag))
farr2val(t, key - 1, tag, res);
return tag;
@@ -964,7 +965,7 @@ const TValue *luaH_Hgetshortstr (Table *t, TString *key) {
}
-int luaH_getshortstr (Table *t, TString *key, TValue *res) {
+lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res) {
return finishnodeget(luaH_Hgetshortstr(t, key), res);
}
@@ -980,7 +981,7 @@ static const TValue *Hgetstr (Table *t, TString *key) {
}
-int luaH_getstr (Table *t, TString *key, TValue *res) {
+lu_byte luaH_getstr (Table *t, TString *key, TValue *res) {
return finishnodeget(Hgetstr(t, key), res);
}
@@ -997,7 +998,7 @@ TString *luaH_getstrkey (Table *t, TString *key) {
/*
** main search function
*/
-int luaH_get (Table *t, const TValue *key, TValue *res) {
+lu_byte luaH_get (Table *t, const TValue *key, TValue *res) {
const TValue *slot;
switch (ttypetag(key)) {
case LUA_VSHRSTR:
@@ -1259,7 +1260,7 @@ lua_Unsigned luaH_getn (Table *t) {
/* (3) 'limit' is the last element and either is zero or present in table */
lua_assert(limit == luaH_realasize(t) &&
(limit == 0 || !arraykeyisempty(t, limit)));
- if (isdummy(t) || hashkeyisempty(t, cast(lua_Integer, limit + 1)))
+ if (isdummy(t) || hashkeyisempty(t, limit + 1))
return limit; /* 'limit + 1' is absent */
else /* 'limit + 1' is also present */
return hash_search(t, limit);
diff --git a/ltable.h b/ltable.h
@@ -20,7 +20,7 @@
** may have any of these metamethods. (First access that fails after the
** clearing will set the bit again.)
*/
-#define invalidateTMcache(t) ((t)->flags &= ~maskflags)
+#define invalidateTMcache(t) ((t)->flags &= cast_byte(~maskflags))
/*
@@ -137,10 +137,10 @@
(*tag = (val)->tt_, *getArrVal(h,(k)) = (val)->value_)
-LUAI_FUNC int luaH_get (Table *t, const TValue *key, TValue *res);
-LUAI_FUNC int luaH_getshortstr (Table *t, TString *key, TValue *res);
-LUAI_FUNC int luaH_getstr (Table *t, TString *key, TValue *res);
-LUAI_FUNC int luaH_getint (Table *t, lua_Integer key, TValue *res);
+LUAI_FUNC lu_byte luaH_get (Table *t, const TValue *key, TValue *res);
+LUAI_FUNC lu_byte luaH_getshortstr (Table *t, TString *key, TValue *res);
+LUAI_FUNC lu_byte luaH_getstr (Table *t, TString *key, TValue *res);
+LUAI_FUNC lu_byte luaH_getint (Table *t, lua_Integer key, TValue *res);
/* Special get for metamethods */
LUAI_FUNC const TValue *luaH_Hgetshortstr (Table *t, TString *key);
diff --git a/ltablib.c b/ltablib.c
@@ -192,7 +192,7 @@ static int tconcat (lua_State *L) {
static int tpack (lua_State *L) {
int i;
int n = lua_gettop(L); /* number of elements to pack */
- lua_createtable(L, n, 1); /* create result table */
+ lua_createtable(L, cast_uint(n), 1); /* create result table */
lua_insert(L, 1); /* put it at index 1 */
for (i = n; i >= 1; i--) /* assign elements */
lua_seti(L, 1, i);
@@ -207,7 +207,7 @@ static int tunpack (lua_State *L) {
lua_Integer i = luaL_optinteger(L, 2, 1);
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
if (i > e) return 0; /* empty range */
- n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
+ n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */
if (l_unlikely(n >= (unsigned int)INT_MAX ||
!lua_checkstack(L, (int)(++n))))
return luaL_error(L, "too many results to unpack");
diff --git a/ltests.c b/ltests.c
@@ -217,7 +217,7 @@ void *debug_realloc (void *ud, void *b, size_t oldsize, size_t size) {
mc->memlimit = limit ? strtoul(limit, NULL, 10) : ULONG_MAX;
}
if (block == NULL) {
- type = (oldsize < LUA_NUMTYPES) ? oldsize : 0;
+ type = (oldsize < LUA_NUMTYPES) ? cast_int(oldsize) : 0;
oldsize = 0;
}
else {
@@ -567,7 +567,7 @@ static l_obj checkgraylist (global_State *g, GCObject *o) {
** Check objects in gray lists.
*/
static l_obj checkgrays (global_State *g) {
- int total = 0; /* count number of elements in all lists */
+ l_obj total = 0; /* count number of elements in all lists */
if (!keepinvariant(g)) return total;
total += checkgraylist(g, g->gray);
total += checkgraylist(g, g->grayagain);
@@ -778,7 +778,7 @@ static int listk (lua_State *L) {
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1),
1, "Lua function expected");
p = getproto(obj_at(L, 1));
- lua_createtable(L, p->sizek, 0);
+ lua_createtable(L, cast_uint(p->sizek), 0);
for (i=0; i<p->sizek; i++) {
pushobject(L, p->k+i);
lua_rawseti(L, -2, i+1);
@@ -794,7 +794,7 @@ static int listabslineinfo (lua_State *L) {
1, "Lua function expected");
p = getproto(obj_at(L, 1));
luaL_argcheck(L, p->abslineinfo != NULL, 1, "function has no debug info");
- lua_createtable(L, 2 * p->sizeabslineinfo, 0);
+ lua_createtable(L, 2u * cast_uint(p->sizeabslineinfo), 0);
for (i=0; i < p->sizeabslineinfo; i++) {
lua_pushinteger(L, p->abslineinfo[i].pc);
lua_rawseti(L, -2, 2 * i + 1);
@@ -847,9 +847,9 @@ static int get_limits (lua_State *L) {
static int mem_query (lua_State *L) {
if (lua_isnone(L, 1)) {
- lua_pushinteger(L, l_memcontrol.total);
- lua_pushinteger(L, l_memcontrol.numblocks);
- lua_pushinteger(L, l_memcontrol.maxmem);
+ lua_pushinteger(L, cast(lua_Integer, l_memcontrol.total));
+ lua_pushinteger(L, cast(lua_Integer, l_memcontrol.numblocks));
+ lua_pushinteger(L, cast(lua_Integer, l_memcontrol.maxmem));
return 3;
}
else if (lua_isnumber(L, 1)) {
@@ -863,7 +863,7 @@ static int mem_query (lua_State *L) {
int i;
for (i = LUA_NUMTYPES - 1; i >= 0; i--) {
if (strcmp(t, ttypename(i)) == 0) {
- lua_pushinteger(L, l_memcontrol.objcount[i]);
+ lua_pushinteger(L, cast(lua_Integer, l_memcontrol.objcount[i]));
return 1;
}
}
@@ -874,9 +874,9 @@ static int mem_query (lua_State *L) {
static int alloc_count (lua_State *L) {
if (lua_isnone(L, 1))
- l_memcontrol.countlimit = ~0L;
+ l_memcontrol.countlimit = cast(unsigned long, ~0L);
else
- l_memcontrol.countlimit = luaL_checkinteger(L, 1);
+ l_memcontrol.countlimit = cast(unsigned long, luaL_checkinteger(L, 1));
return 0;
}
@@ -975,26 +975,26 @@ static int gc_state (lua_State *L) {
static int hash_query (lua_State *L) {
if (lua_isnone(L, 2)) {
luaL_argcheck(L, lua_type(L, 1) == LUA_TSTRING, 1, "string expected");
- lua_pushinteger(L, tsvalue(obj_at(L, 1))->hash);
+ lua_pushinteger(L, cast_int(tsvalue(obj_at(L, 1))->hash));
}
else {
TValue *o = obj_at(L, 1);
Table *t;
luaL_checktype(L, 2, LUA_TTABLE);
t = hvalue(obj_at(L, 2));
- lua_pushinteger(L, luaH_mainposition(t, o) - t->node);
+ lua_pushinteger(L, cast(lua_Integer, luaH_mainposition(t, o) - t->node));
}
return 1;
}
static int stacklevel (lua_State *L) {
- unsigned long a = 0;
- lua_pushinteger(L, (L->top.p - L->stack.p));
+ int a = 0;
+ lua_pushinteger(L, cast(lua_Integer, L->top.p - L->stack.p));
lua_pushinteger(L, stacksize(L));
- lua_pushinteger(L, L->nCcalls);
+ lua_pushinteger(L, cast(lua_Integer, L->nCcalls));
lua_pushinteger(L, L->nci);
- lua_pushinteger(L, (unsigned long)&a);
+ lua_pushinteger(L, (lua_Integer)(size_t)&a);
return 5;
}
@@ -1007,9 +1007,9 @@ static int table_query (lua_State *L) {
t = hvalue(obj_at(L, 1));
asize = luaH_realasize(t);
if (i == -1) {
- lua_pushinteger(L, asize);
- lua_pushinteger(L, allocsizenode(t));
- lua_pushinteger(L, t->alimit);
+ lua_pushinteger(L, cast(lua_Integer, asize));
+ lua_pushinteger(L, cast(lua_Integer, allocsizenode(t)));
+ lua_pushinteger(L, cast(lua_Integer, t->alimit));
return 3;
}
else if (cast_uint(i) < asize) {
@@ -1018,7 +1018,7 @@ static int table_query (lua_State *L) {
api_incr_top(L);
lua_pushnil(L);
}
- else if ((i -= asize) < sizenode(t)) {
+ else if (cast_uint(i -= cast_int(asize)) < sizenode(t)) {
TValue k;
getnodekey(L, &k, gnode(t, i));
if (!isempty(gval(gnode(t, i))) ||
@@ -1054,7 +1054,7 @@ static int query_GCparams (lua_State *L) {
static int test_codeparam (lua_State *L) {
lua_Integer p = luaL_checkinteger(L, 1);
- lua_pushinteger(L, luaO_codeparam(p));
+ lua_pushinteger(L, luaO_codeparam(cast_uint(p)));
return 1;
}
@@ -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(p, x));
+ lua_pushinteger(L, luaO_applyparam(cast_byte(p), x));
return 1;
}
@@ -1147,7 +1147,7 @@ static int upvalue (lua_State *L) {
static int newuserdata (lua_State *L) {
size_t size = cast_sizet(luaL_optinteger(L, 1, 0));
- int nuv = luaL_optinteger(L, 2, 0);
+ int nuv = cast_int(luaL_optinteger(L, 2, 0));
char *p = cast_charp(lua_newuserdatauv(L, size, nuv));
while (size--) *p++ = '\0';
return 1;
@@ -1227,8 +1227,8 @@ static lua_State *getstate (lua_State *L) {
static int loadlib (lua_State *L) {
lua_State *L1 = getstate(L);
- int load = luaL_checkinteger(L, 2);
- int preload = luaL_checkinteger(L, 3);
+ int load = cast_int(luaL_checkinteger(L, 2));
+ int preload = cast_int(luaL_checkinteger(L, 3));
luaL_openselectedlibs(L1, load, preload);
luaL_requiref(L1, "T", luaB_opentests, 0);
lua_assert(lua_type(L1, -1) == LUA_TTABLE);
@@ -1490,13 +1490,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
}
else if EQ("append") {
int t = getindex;
- int i = lua_rawlen(L1, t);
+ int i = cast_int(lua_rawlen(L1, t));
lua_rawseti(L1, t, i + 1);
}
else if EQ("arith") {
int op;
skip(&pc);
- op = strchr(ops, *pc++) - ops;
+ op = cast_int(strchr(ops, *pc++) - ops);
lua_arith(L1, op);
}
else if EQ("call") {
@@ -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_pushnumber(L1, cast_sizet(func));
+ lua_pushinteger(L1, cast(lua_Integer, func));
}
else if EQ("getfield") {
int t = getindex;
@@ -1624,13 +1624,13 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) {
lua_pushinteger(L1, lua_resetthread(L1)); /* deprecated */
}
else if EQ("newuserdata") {
- lua_newuserdata(L1, getnum);
+ lua_newuserdata(L1, cast_sizet(getnum));
}
else if EQ("next") {
lua_next(L1, -2);
}
else if EQ("objsize") {
- lua_pushinteger(L1, lua_rawlen(L1, getindex));
+ lua_pushinteger(L1, l_castU2S(lua_rawlen(L1, getindex)));
}
else if EQ("pcall") {
int narg = getnum;
@@ -1903,7 +1903,7 @@ static int Cfunck (lua_State *L, int status, lua_KContext ctx) {
lua_setglobal(L, "status");
lua_pushinteger(L, ctx);
lua_setglobal(L, "ctx");
- return runC(L, L, lua_tostring(L, ctx));
+ return runC(L, L, lua_tostring(L, cast_int(ctx)));
}
diff --git a/ltm.c b/ltm.c
@@ -116,8 +116,8 @@ void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
}
-int luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
- const TValue *p2, StkId res) {
+lu_byte luaT_callTMres (lua_State *L, const TValue *f, const TValue *p1,
+ const TValue *p2, StkId res) {
ptrdiff_t result = savestack(L, res);
StkId func = L->top.p;
setobj2s(L, func, f); /* push function (assume EXTRA_STACK) */
diff --git a/ltm.h b/ltm.h
@@ -51,7 +51,7 @@ typedef enum {
** corresponding metamethod field. (Bit 6 of the flag indicates that
** the table is using the dummy node; bit 7 is used for 'isrealasize'.)
*/
-#define maskflags (~(~0u << (TM_EQ + 1)))
+#define maskflags cast_byte(~(~0u << (TM_EQ + 1)))
/*
@@ -81,8 +81,8 @@ LUAI_FUNC void luaT_init (lua_State *L);
LUAI_FUNC void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1,
const TValue *p2, const TValue *p3);
-LUAI_FUNC int luaT_callTMres (lua_State *L, const TValue *f,
- const TValue *p1, const TValue *p2, StkId p3);
+LUAI_FUNC lu_byte luaT_callTMres (lua_State *L, const TValue *f,
+ const TValue *p1, const TValue *p2, StkId p3);
LUAI_FUNC void luaT_trybinTM (lua_State *L, const TValue *p1, const TValue *p2,
StkId res, TMS event);
LUAI_FUNC void luaT_tryconcatTM (lua_State *L);
diff --git a/lua.c b/lua.c
@@ -185,7 +185,7 @@ static void print_version (void) {
static void createargtable (lua_State *L, char **argv, int argc, int script) {
int i, narg;
narg = argc - (script + 1); /* number of positive indices */
- lua_createtable(L, narg, script + 1);
+ lua_createtable(L, cast_uint(narg), cast_uint(script + 1));
for (i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
lua_rawseti(L, -2, i - script);
diff --git a/lundump.c b/lundump.c
@@ -36,7 +36,7 @@ typedef struct {
ZIO *Z;
const char *name;
Table *h; /* list for string reuse */
- lu_mem offset; /* current position relative to beginning of dump */
+ size_t offset; /* current position relative to beginning of dump */
lua_Integer nstr; /* number of strings in the list */
lu_byte fixed; /* dump is fixed in memory */
} LoadState;
@@ -61,8 +61,8 @@ static void loadBlock (LoadState *S, void *b, size_t size) {
}
-static void loadAlign (LoadState *S, int align) {
- int padding = align - (S->offset % align);
+static void loadAlign (LoadState *S, unsigned align) {
+ unsigned padding = align - cast_uint(S->offset % align);
if (padding < align) { /* apd == align means no padding */
lua_Integer paddingContent;
loadBlock(S, &paddingContent, padding);
@@ -113,11 +113,19 @@ static size_t loadSize (LoadState *S) {
}
+/*
+** Read an non-negative int */
+static unsigned loadUint (LoadState *S) {
+ return cast_uint(loadVarint(S, cast_sizet(INT_MAX)));
+}
+
+
static int loadInt (LoadState *S) {
return cast_int(loadVarint(S, cast_sizet(INT_MAX)));
}
+
static lua_Number loadNumber (LoadState *S) {
lua_Number x;
loadVar(S, x);
@@ -180,15 +188,15 @@ static void loadString (LoadState *S, Proto *p, TString **sl) {
static void loadCode (LoadState *S, Proto *f) {
- int n = loadInt(S);
+ unsigned n = loadUint(S);
loadAlign(S, sizeof(f->code[0]));
if (S->fixed) {
f->code = getaddr(S, n, Instruction);
- f->sizecode = n;
+ f->sizecode = cast_int(n);
}
else {
f->code = luaM_newvectorchecked(S->L, n, Instruction);
- f->sizecode = n;
+ f->sizecode = cast_int(n);
loadVector(S, f->code, n);
}
}
@@ -198,10 +206,10 @@ static void loadFunction(LoadState *S, Proto *f);
static void loadConstants (LoadState *S, Proto *f) {
- int i;
- int n = loadInt(S);
+ unsigned i;
+ unsigned n = loadUint(S);
f->k = luaM_newvectorchecked(S->L, n, TValue);
- f->sizek = n;
+ f->sizek = cast_int(n);
for (i = 0; i < n; i++)
setnilvalue(&f->k[i]);
for (i = 0; i < n; i++) {
@@ -240,10 +248,10 @@ static void loadConstants (LoadState *S, Proto *f) {
static void loadProtos (LoadState *S, Proto *f) {
- int i;
- int n = loadInt(S);
+ unsigned i;
+ unsigned n = loadUint(S);
f->p = luaM_newvectorchecked(S->L, n, Proto *);
- f->sizep = n;
+ f->sizep = cast_int(n);
for (i = 0; i < n; i++)
f->p[i] = NULL;
for (i = 0; i < n; i++) {
@@ -261,10 +269,10 @@ static void loadProtos (LoadState *S, Proto *f) {
** in that case all prototypes must be consistent for the GC.
*/
static void loadUpvalues (LoadState *S, Proto *f) {
- int i, n;
- n = loadInt(S);
+ unsigned i;
+ unsigned n = loadUint(S);
f->upvalues = luaM_newvectorchecked(S->L, n, Upvaldesc);
- f->sizeupvalues = n;
+ f->sizeupvalues = cast_int(n);
for (i = 0; i < n; i++) /* make array valid for GC */
f->upvalues[i].name = NULL;
for (i = 0; i < n; i++) { /* following calls can raise errors */
@@ -276,33 +284,33 @@ static void loadUpvalues (LoadState *S, Proto *f) {
static void loadDebug (LoadState *S, Proto *f) {
- int i, n;
- n = loadInt(S);
+ unsigned i;
+ unsigned n = loadUint(S);
if (S->fixed) {
f->lineinfo = getaddr(S, n, ls_byte);
- f->sizelineinfo = n;
+ f->sizelineinfo = cast_int(n);
}
else {
f->lineinfo = luaM_newvectorchecked(S->L, n, ls_byte);
- f->sizelineinfo = n;
+ f->sizelineinfo = cast_int(n);
loadVector(S, f->lineinfo, n);
}
- n = loadInt(S);
+ n = loadUint(S);
if (n > 0) {
loadAlign(S, sizeof(int));
if (S->fixed) {
f->abslineinfo = getaddr(S, n, AbsLineInfo);
- f->sizeabslineinfo = n;
+ f->sizeabslineinfo = cast_int(n);
}
else {
f->abslineinfo = luaM_newvectorchecked(S->L, n, AbsLineInfo);
- f->sizeabslineinfo = n;
+ f->sizeabslineinfo = cast_int(n);
loadVector(S, f->abslineinfo, n);
}
}
- n = loadInt(S);
+ n = loadUint(S);
f->locvars = luaM_newvectorchecked(S->L, n, LocVar);
- f->sizelocvars = n;
+ f->sizelocvars = cast_int(n);
for (i = 0; i < n; i++)
f->locvars[i].varname = NULL;
for (i = 0; i < n; i++) {
@@ -310,9 +318,9 @@ static void loadDebug (LoadState *S, Proto *f) {
f->locvars[i].startpc = loadInt(S);
f->locvars[i].endpc = loadInt(S);
}
- n = loadInt(S);
+ n = loadUint(S);
if (n != 0) /* does it have debug information? */
- n = f->sizeupvalues; /* must be this many */
+ n = cast_uint(f->sizeupvalues); /* must be this many */
for (i = 0; i < n; i++)
loadString(S, f, &f->upvalues[i].name);
}
@@ -384,7 +392,7 @@ LClosure *luaU_undump (lua_State *L, ZIO *Z, const char *name, int fixed) {
S.name = name;
S.L = L;
S.Z = Z;
- S.fixed = fixed;
+ S.fixed = cast_byte(fixed);
S.offset = 1; /* fist byte was already read */
checkHeader(&S);
cl = luaF_newLclosure(L, loadByte(&S));
diff --git a/lutf8lib.c b/lutf8lib.c
@@ -180,7 +180,7 @@ static int byteoffset (lua_State *L) {
size_t len;
const char *s = luaL_checklstring(L, 1, &len);
lua_Integer n = luaL_checkinteger(L, 2);
- lua_Integer posi = (n >= 0) ? 1 : len + 1;
+ lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
"position out of bounds");
@@ -239,7 +239,7 @@ static int iter_aux (lua_State *L, int strict) {
const char *next = utf8_decode(s + n, &code, strict);
if (next == NULL || iscontp(next))
return luaL_error(L, MSGInvalid);
- lua_pushinteger(L, n + 1);
+ lua_pushinteger(L, l_castU2S(n + 1));
lua_pushinteger(L, code);
return 2;
}
diff --git a/lvm.c b/lvm.c
@@ -288,8 +288,8 @@ static int floatforloop (StkId ra) {
/*
** Finish the table access 'val = t[key]' and return the tag of the result.
*/
-int luaV_finishget (lua_State *L, const TValue *t, TValue *key, StkId val,
- int tag) {
+lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key,
+ StkId val, lu_byte tag) {
int loop; /* counter to avoid infinite loops */
const TValue *tm; /* metamethod */
for (loop = 0; loop < MAXTAGLOOP; loop++) {
@@ -690,7 +690,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
Table *h = hvalue(rb);
tm = fasttm(L, h->metatable, TM_LEN);
if (tm) break; /* metamethod? break switch to call it */
- setivalue(s2v(ra), luaH_getn(h)); /* else primitive len */
+ setivalue(s2v(ra), l_castU2S(luaH_getn(h))); /* else primitive len */
return;
}
case LUA_VSHRSTR: {
@@ -698,7 +698,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
return;
}
case LUA_VLNGSTR: {
- setivalue(s2v(ra), tsvalue(rb)->u.lnglen);
+ setivalue(s2v(ra), cast_st2S(tsvalue(rb)->u.lnglen));
return;
}
default: { /* try metamethod */
@@ -1255,7 +1255,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TValue *upval = cl->upvals[GETARG_B(i)]->v.p;
TValue *rc = KC(i);
TString *key = tsvalue(rc); /* key must be a short string */
- int tag;
+ lu_byte tag;
luaV_fastget(upval, key, s2v(ra), luaH_getshortstr, tag);
if (tagisempty(tag))
Protect(luaV_finishget(L, upval, rc, ra, tag));
@@ -1265,7 +1265,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
StkId ra = RA(i);
TValue *rb = vRB(i);
TValue *rc = vRC(i);
- int tag;
+ lu_byte tag;
if (ttisinteger(rc)) { /* fast track for integers? */
luaV_fastgeti(rb, ivalue(rc), s2v(ra), tag);
}
@@ -1279,7 +1279,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
StkId ra = RA(i);
TValue *rb = vRB(i);
int c = GETARG_C(i);
- int tag;
+ lu_byte tag;
luaV_fastgeti(rb, c, s2v(ra), tag);
if (tagisempty(tag)) {
TValue key;
@@ -1293,7 +1293,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
TValue *rb = vRB(i);
TValue *rc = KC(i);
TString *key = tsvalue(rc); /* key must be a short string */
- int tag;
+ lu_byte tag;
luaV_fastget(rb, key, s2v(ra), luaH_getshortstr, tag);
if (tagisempty(tag))
Protect(luaV_finishget(L, rb, rc, ra, tag));
@@ -1359,14 +1359,15 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}
vmcase(OP_NEWTABLE) {
StkId ra = RA(i);
- int b = GETARG_vB(i); /* log2(hash size) + 1 */
- int c = GETARG_vC(i); /* array size */
+ unsigned b = cast_uint(GETARG_vB(i)); /* log2(hash size) + 1 */
+ unsigned c = cast_uint(GETARG_vC(i)); /* array size */
Table *t;
if (b > 0)
- b = 1 << (b - 1); /* hash size is 2^(b - 1) */
+ b = 1u << (b - 1); /* hash size is 2^(b - 1) */
if (TESTARG_k(i)) { /* non-zero extra argument? */
lua_assert(GETARG_Ax(*pc) != 0);
- c += GETARG_Ax(*pc) * (MAXARG_vC + 1); /* add it to array size */
+ /* add it to array size */
+ c += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
}
pc++; /* skip extra argument */
L->top.p = ra + 1; /* correct top in case of emergency GC */
@@ -1379,7 +1380,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}
vmcase(OP_SELF) {
StkId ra = RA(i);
- int tag;
+ lu_byte tag;
TValue *rb = vRB(i);
TValue *rc = RKC(i);
TString *key = tsvalue(rc); /* key must be a string */
@@ -1786,7 +1787,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
if (count > 0) { /* still more iterations? */
lua_Integer step = ivalue(s2v(ra + 1));
lua_Integer idx = ivalue(s2v(ra + 2)); /* control variable */
- chgivalue(s2v(ra), count - 1); /* update counter */
+ chgivalue(s2v(ra), l_castU2S(count - 1)); /* update counter */
idx = intop(+, idx, step); /* add step to index */
chgivalue(s2v(ra + 2), idx); /* update control variable */
pc -= GETARG_Bx(i); /* jump back */
@@ -1851,16 +1852,16 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
}}
vmcase(OP_SETLIST) {
StkId ra = RA(i);
- int n = GETARG_vB(i);
- unsigned int last = GETARG_vC(i);
+ unsigned n = cast_uint(GETARG_vB(i));
+ unsigned int last = cast_uint(GETARG_vC(i));
Table *h = hvalue(s2v(ra));
if (n == 0)
- n = cast_int(L->top.p - ra) - 1; /* get up to the top */
+ n = cast_uint(L->top.p - ra) - 1; /* get up to the top */
else
L->top.p = ci->top.p; /* correct top in case of emergency GC */
last += n;
if (TESTARG_k(i)) {
- last += GETARG_Ax(*pc) * (MAXARG_vC + 1);
+ last += cast_uint(GETARG_Ax(*pc)) * (MAXARG_vC + 1);
pc++;
}
/* when 'n' is known, table should have proper size */
diff --git a/lvm.h b/lvm.h
@@ -120,8 +120,8 @@ LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
F2Imod mode);
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
-LUAI_FUNC int luaV_finishget (lua_State *L, const TValue *t, TValue *key,
- StkId val, int tag);
+LUAI_FUNC lu_byte luaV_finishget (lua_State *L, const TValue *t, TValue *key,
+ StkId val, lu_byte tag);
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
TValue *val, int aux);
LUAI_FUNC void luaV_finishOp (lua_State *L);
diff --git a/lzio.h b/lzio.h
@@ -32,7 +32,7 @@ typedef struct Mbuffer {
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
#define luaZ_bufflen(buff) ((buff)->n)
-#define luaZ_buffremove(buff,i) ((buff)->n -= (i))
+#define luaZ_buffremove(buff,i) ((buff)->n -= cast_sizet(i))
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
diff --git a/makefile b/makefile
@@ -14,11 +14,11 @@ CWARNSCPP= \
-Wdisabled-optimization \
-Wdouble-promotion \
-Wmissing-declarations \
+ -Wconversion \
# the next warnings might be useful sometimes,
# but usually they generate too much noise
# -Werror \
# -pedantic # warns if we use jump tables \
- # -Wconversion \
# -Wsign-conversion \
# -Wstrict-overflow=2 \
# -Wformat=2 \
diff --git a/manual/manual.of b/manual/manual.of
@@ -3995,7 +3995,7 @@ The conversion specifiers can only be
@Char{%p} (inserts a pointer),
@Char{%d} (inserts an @T{int}),
@Char{%c} (inserts an @T{int} as a one-byte character), and
-@Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence).
+@Char{%U} (inserts an @T{unsigned long} as a @x{UTF-8} byte sequence).
This function may raise errors due to memory overflow
or an invalid conversion specifier.