Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/lcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,8 @@ int FuncState::constfolding(int op, expdesc *e1, const expdesc *e2) {
TValue v1, v2, res;
if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2) || !validop(op, &v1, &v2))
return 0; /* non-numeric operands or not safe to fold */
luaO_rawarith(getLexState()->getLuaState(), op, &v1, &v2, &res); /* does operation */
if (!luaO_rawarith(getLexState()->getLuaState(), op, &v1, &v2, &res))
return 0; /* operation failed */
if (ttisinteger(&res)) {
e1->setKind(VKINT);
e1->setIntValue(ivalue(&res));
Expand Down
21 changes: 19 additions & 2 deletions src/core/lstack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,25 @@ int LuaStack::grow(lua_State* L, int n, int raiseerror) {
return 0; /* if not 'raiseerror', just signal it */
}
else if (n < MAXSTACK) { /* avoids arithmetic overflows */
int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */
int needed = cast_int(top.p - stack.p) + n;
int newsize;
/* Check for overflow in size * 1.5 calculation */
if (size > INT_MAX / 3 * 2) {
/* size + (size >> 1) could overflow, use MAXSTACK */
newsize = MAXSTACK;
} else {
newsize = size + (size >> 1); /* tentative new size (size * 1.5) */
}

/* Safe calculation of needed space */
ptrdiff_t stack_used = top.p - stack.p;
lua_assert(stack_used >= 0 && stack_used <= INT_MAX);
int needed;
if (stack_used > INT_MAX - n) {
/* needed calculation would overflow, use MAXSTACK */
needed = MAXSTACK;
} else {
needed = cast_int(stack_used) + n;
}

if (newsize > MAXSTACK) /* cannot cross the limit */
newsize = MAXSTACK;
Expand Down
2 changes: 2 additions & 0 deletions src/objects/lobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
/*
** Computes ceil(log2(x)), which is the smallest integer n such that
** x <= (1 << n).
** Precondition: x > 0 (ceil(log2(0)) is undefined)
*/
lu_byte luaO_ceillog2 (unsigned int x) {
lua_assert(x > 0); /* ceil(log2(0)) is undefined */
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,
Expand Down
26 changes: 13 additions & 13 deletions src/objects/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1795,18 +1795,18 @@ inline constexpr int UTF8BUFFSZ = 8;
if (msg == nullptr) (L)->doThrow(LUA_ERRMEM); /* only after 'va_end' */ }


LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x);
LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x);
LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);
LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x);
[[nodiscard]] LUAI_FUNC int luaO_utf8esc (char *buff, l_uint32 x);
[[nodiscard]] LUAI_FUNC lu_byte luaO_ceillog2 (unsigned int x);
[[nodiscard]] LUAI_FUNC lu_byte luaO_codeparam (unsigned int p);
[[nodiscard]] LUAI_FUNC l_mem luaO_applyparam (lu_byte p, l_mem x);

LUAI_FUNC int luaO_rawarith (lua_State *L, int op, const TValue *p1,
[[nodiscard]] 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 unsigned luaO_tostringbuff (const TValue *obj, char *buff);
LUAI_FUNC lu_byte luaO_hexavalue (int c);
[[nodiscard]] LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
[[nodiscard]] LUAI_FUNC unsigned luaO_tostringbuff (const TValue *obj, char *buff);
[[nodiscard]] 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);
Expand Down Expand Up @@ -1844,11 +1844,11 @@ LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);

/* Forward declarations for comparison helpers (defined in lvm.cpp and lstring.h) */
/* These handle mixed int/float comparisons correctly */
LUAI_FUNC int LTintfloat (lua_Integer i, lua_Number f);
LUAI_FUNC int LEintfloat (lua_Integer i, lua_Number f);
LUAI_FUNC int LTfloatint (lua_Number f, lua_Integer i);
LUAI_FUNC int LEfloatint (lua_Number f, lua_Integer i);
LUAI_FUNC int l_strcmp (const TString* ts1, const TString* ts2);
[[nodiscard]] LUAI_FUNC int LTintfloat (lua_Integer i, lua_Number f);
[[nodiscard]] LUAI_FUNC int LEintfloat (lua_Integer i, lua_Number f);
[[nodiscard]] LUAI_FUNC int LTfloatint (lua_Number f, lua_Integer i);
[[nodiscard]] LUAI_FUNC int LEfloatint (lua_Number f, lua_Integer i);
[[nodiscard]] LUAI_FUNC int l_strcmp (const TString* ts1, const TString* ts2);
/* luaS_eqstr declared in lstring.h */

/* String comparison helpers (defined in lstring.h) */
Expand Down
10 changes: 9 additions & 1 deletion src/objects/ltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ static int equalkey (const TValue *k1, const Node *n2, int deadok) {
*/
static TValue *getgeneric (Table *t, const TValue *key, int deadok) {
Node *n = mainpositionTV(t, key);
const Node *base = gnode(t, 0);
const Node *limit = base + t->nodeSize();
for (;;) { /* check whether 'key' is somewhere in the chain */
if (equalkey(key, n, deadok))
return gval(n); /* that's it */
Expand All @@ -420,6 +422,7 @@ static TValue *getgeneric (Table *t, const TValue *key, int deadok) {
if (nx == 0)
return &absentkey; /* not found */
n += nx;
lua_assert(n >= base && n < limit); /* ensure we stay in bounds */
}
}
}
Expand Down Expand Up @@ -477,7 +480,12 @@ 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_uint(reinterpret_cast<const Node*>(n) - gnode(t, 0)); /* key index in hash table */
/* Calculate index in hash table with bounds checking */
const Node* node_ptr = reinterpret_cast<const Node*>(n);
const Node* base = gnode(t, 0);
ptrdiff_t diff = node_ptr - base;
lua_assert(diff >= 0 && static_cast<size_t>(diff) < t->nodeSize());
i = cast_uint(diff);
/* hash elements are numbered after array ones */
return (i + 1) + asize;
}
Expand Down
14 changes: 7 additions & 7 deletions src/vm/lvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ inline bool tointegerns(const TValue* o, lua_Integer* i) noexcept {
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))

/* Forward declaration for luaV_equalobj (defined in lvm.cpp) */
LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
[[nodiscard]] LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);

inline int luaV_rawequalobj(const TValue* t1, const TValue* t2) noexcept {
return *t1 == *t2; /* Use operator== for raw equality */
Expand Down Expand Up @@ -224,16 +224,16 @@ inline void luaV_finishfastset(lua_State* L, const TValue* t, const TValue* v) n
** Shift right is the same as shift left with a negative 'y'
*/
/* Forward declaration for luaV_shiftl (full declaration below) */
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
[[nodiscard]] LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);

inline lua_Integer luaV_shiftr(lua_Integer x, lua_Integer y) noexcept {
return luaV_shiftl(x, intop(-, 0, y));
}



LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
[[nodiscard]] LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
[[nodiscard]] LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
#ifndef luaV_flttointeger_declared
#define luaV_flttointeger_declared
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
Expand All @@ -245,9 +245,9 @@ LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
LUAI_FUNC void luaV_finishOp (lua_State *L);
LUAI_FUNC void luaV_execute (lua_State *L, CallInfo *ci);
LUAI_FUNC void luaV_concat (lua_State *L, int total);
LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y);
[[nodiscard]] LUAI_FUNC lua_Integer luaV_idiv (lua_State *L, lua_Integer x, lua_Integer y);
[[nodiscard]] LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
[[nodiscard]] LUAI_FUNC lua_Number luaV_modf (lua_State *L, lua_Number x, lua_Number y);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);

#endif
10 changes: 5 additions & 5 deletions src/vm/lvm_comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
** of the strings. Note that segments can compare equal but still
** have different lengths.
*/
int l_strcmp (const TString *ts1, const TString *ts2) {
[[nodiscard]] int l_strcmp (const TString *ts1, const TString *ts2) {
size_t rl1; /* real length */
const char *s1 = getlstr(ts1, rl1);
size_t rl2;
Expand Down Expand Up @@ -75,7 +75,7 @@ int l_strcmp (const TString *ts1, const TString *ts2) {
** potentially giving incorrect results. Instead, we compute ceil(f) as an
** integer and compare in the integer domain where no precision is lost.
*/
int LTintfloat (lua_Integer i, lua_Number f) {
[[nodiscard]] int LTintfloat (lua_Integer i, lua_Number f) {
if (l_intfitsf(i))
return luai_numlt(cast_num(i), f); /* compare them as floats */
else { /* i < f <=> i < ceil(f) */
Expand All @@ -92,7 +92,7 @@ int LTintfloat (lua_Integer i, lua_Number f) {
** Check whether integer 'i' is less than or equal to float 'f'.
** See comments on previous function.
*/
int LEintfloat (lua_Integer i, lua_Number f) {
[[nodiscard]] int LEintfloat (lua_Integer i, lua_Number f) {
if (l_intfitsf(i))
return luai_numle(cast_num(i), f); /* compare them as floats */
else { /* i <= f <=> i <= floor(f) */
Expand All @@ -109,7 +109,7 @@ int LEintfloat (lua_Integer i, lua_Number f) {
** Check whether float 'f' is less than integer 'i'.
** See comments on previous function.
*/
int LTfloatint (lua_Number f, lua_Integer i) {
[[nodiscard]] int LTfloatint (lua_Number f, lua_Integer i) {
if (l_intfitsf(i))
return luai_numlt(f, cast_num(i)); /* compare them as floats */
else { /* f < i <=> floor(f) < i */
Expand All @@ -126,7 +126,7 @@ int LTfloatint (lua_Number f, lua_Integer i) {
** Check whether float 'f' is less than or equal to integer 'i'.
** See comments on previous function.
*/
int LEfloatint (lua_Number f, lua_Integer i) {
[[nodiscard]] int LEfloatint (lua_Number f, lua_Integer i) {
if (l_intfitsf(i))
return luai_numle(f, cast_num(i)); /* compare them as floats */
else { /* f <= i <=> ceil(f) <= i */
Expand Down
Loading