Fixed small bugs/issues

- In 'readutf8esc' (llex.c), the overflow check must be done before
shifting the accumulator. It was working because tests were using
64-bit longs. Failed with 32-bit longs.

- In OP_FORPREP (lvm.c), avoid negating an unsigned value. Visual
Studio gives a warning for that operation, despite being well
defined in ISO C.

- In 'luaV_execute' (lvm.c), 'cond' can be defined only when needed,
like all other variables.
This commit is contained in:
Roberto Ierusalimschy
2019-03-25 10:38:56 -03:00
parent 23e6bac8a0
commit 7ceb2154ed
2 changed files with 9 additions and 5 deletions

2
llex.c
View File

@@ -334,8 +334,8 @@ static unsigned long readutf8esc (LexState *ls) {
r = gethexa(ls); /* must have at least one digit */ r = gethexa(ls); /* must have at least one digit */
while ((save_and_next(ls), lisxdigit(ls->current))) { while ((save_and_next(ls), lisxdigit(ls->current))) {
i++; i++;
esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
r = (r << 4) + luaO_hexavalue(ls->current); r = (r << 4) + luaO_hexavalue(ls->current);
esccheck(ls, r <= 0x7FFFFFFFu, "UTF-8 value too large");
} }
esccheck(ls, ls->current == '}', "missing '}'"); esccheck(ls, ls->current == '}', "missing '}'");
next(ls); /* skip '}' */ next(ls); /* skip '}' */

12
lvm.c
View File

@@ -925,6 +925,7 @@ void luaV_finishOp (lua_State *L) {
** Order operations with register operands. ** Order operations with register operands.
*/ */
#define op_order(L,opi,opf,other) { \ #define op_order(L,opi,opf,other) { \
int cond; \
TValue *rb = vRB(i); \ TValue *rb = vRB(i); \
if (ttisinteger(s2v(ra)) && ttisinteger(rb)) \ if (ttisinteger(s2v(ra)) && ttisinteger(rb)) \
cond = opi(ivalue(s2v(ra)), ivalue(rb)); \ cond = opi(ivalue(s2v(ra)), ivalue(rb)); \
@@ -939,6 +940,7 @@ void luaV_finishOp (lua_State *L) {
** Order operations with immediate operand. ** Order operations with immediate operand.
*/ */
#define op_orderI(L,opi,opf,inv,tm) { \ #define op_orderI(L,opi,opf,inv,tm) { \
int cond; \
int im = GETARG_sB(i); \ int im = GETARG_sB(i); \
if (ttisinteger(s2v(ra))) \ if (ttisinteger(s2v(ra))) \
cond = opi(ivalue(s2v(ra)), im); \ cond = opi(ivalue(s2v(ra)), im); \
@@ -1076,7 +1078,6 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
base = ci->func + 1; base = ci->func + 1;
/* main loop of interpreter */ /* main loop of interpreter */
for (;;) { for (;;) {
int cond; /* flag for conditional jumps */
Instruction i; /* instruction being executed */ Instruction i; /* instruction being executed */
StkId ra; /* instruction's A register */ StkId ra; /* instruction's A register */
vmfetch(); vmfetch();
@@ -1475,6 +1476,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
vmbreak; vmbreak;
} }
vmcase(OP_EQ) { vmcase(OP_EQ) {
int cond;
TValue *rb = vRB(i); TValue *rb = vRB(i);
Protect(cond = luaV_equalobj(L, s2v(ra), rb)); Protect(cond = luaV_equalobj(L, s2v(ra), rb));
docondjump(); docondjump();
@@ -1491,11 +1493,12 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
vmcase(OP_EQK) { vmcase(OP_EQK) {
TValue *rb = KB(i); TValue *rb = KB(i);
/* basic types do not use '__eq'; we can use raw equality */ /* basic types do not use '__eq'; we can use raw equality */
cond = luaV_equalobj(NULL, s2v(ra), rb); int cond = luaV_equalobj(NULL, s2v(ra), rb);
docondjump(); docondjump();
vmbreak; vmbreak;
} }
vmcase(OP_EQI) { vmcase(OP_EQI) {
int cond;
int im = GETARG_sB(i); int im = GETARG_sB(i);
if (ttisinteger(s2v(ra))) if (ttisinteger(s2v(ra)))
cond = (ivalue(s2v(ra)) == im); cond = (ivalue(s2v(ra)) == im);
@@ -1523,7 +1526,7 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
vmbreak; vmbreak;
} }
vmcase(OP_TEST) { vmcase(OP_TEST) {
cond = !l_isfalse(s2v(ra)); int cond = !l_isfalse(s2v(ra));
docondjump(); docondjump();
vmbreak; vmbreak;
} }
@@ -1679,7 +1682,8 @@ void luaV_execute (lua_State *L, CallInfo *ci) {
} }
else { /* step < 0; descending loop */ else { /* step < 0; descending loop */
count = l_castS2U(init) - l_castS2U(limit); count = l_castS2U(init) - l_castS2U(limit);
count /= -l_castS2U(step); /* 'step+1' avoids negating 'mininteger' */
count /= l_castS2U(-(step + 1)) + 1u;
} }
/* store the counter in place of the limit (which won't be /* store the counter in place of the limit (which won't be
needed anymore */ needed anymore */