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
32 changes: 18 additions & 14 deletions src/core/ldebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "lprefix.h"


#include <algorithm>
#include <cstdarg>
#include <cstddef>
#include <cstring>
Expand Down Expand Up @@ -66,14 +67,18 @@ static int getbaseline (const Proto *f, int pc, int *basepc) {
return f->getLineDefined();
}
else {
int i = pc / MAXIWTHABS - 1; /* get an estimate */
/* estimate must be a lower bound of the correct base */
lua_assert(i < 0 ||
(i < f->getAbsLineInfoSize() && f->getAbsLineInfo()[i].getPC() <= pc));
while (i + 1 < f->getAbsLineInfoSize() && pc >= f->getAbsLineInfo()[i + 1].getPC())
i++; /* low estimate; adjust it */
*basepc = f->getAbsLineInfo()[i].getPC();
return f->getAbsLineInfo()[i].getLine();
/* Binary search for the last AbsLineInfo with PC <= pc */
const AbsLineInfo* absLineInfo = f->getAbsLineInfo();
int size = f->getAbsLineInfoSize();
/* std::upper_bound finds first element with PC > pc, so we go back one */
auto it = std::upper_bound(absLineInfo, absLineInfo + size, pc,
[](int target_pc, const AbsLineInfo& info) {
return target_pc < info.getPC();
});
lua_assert(it != absLineInfo); /* we know there's at least one element with PC <= pc */
--it; /* go back to last element with PC <= pc */
*basepc = it->getPC();
return it->getLine();
}
}

Expand Down Expand Up @@ -690,13 +695,12 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci,
** region boundaries (undefined behavior in ISO C).
*/
static int instack (CallInfo *ci, const TValue *o) {
int pos;
StkId base = ci->funcRef().p + 1;
for (pos = 0; base + pos < ci->topRef().p; pos++) {
if (o == s2v(base + pos))
return pos;
}
return -1; /* not found */
StkId end = ci->topRef().p;
auto it = std::find_if(base, end, [o](const StackValue& sv) {
return o == s2v(&sv);
});
return (it != end) ? static_cast<int>(it - base) : -1; /* return position or -1 if not found */
}


Expand Down
5 changes: 3 additions & 2 deletions src/objects/lstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ unsigned luaS_hashlongstr (TString *ts) {

static void tablerehash (TString **vect, unsigned int osize, unsigned int nsize) {
unsigned int i;
for (i = osize; i < nsize; i++) /* clear new elements */
vect[i] = NULL;
/* clear new elements (only when growing) */
if (nsize > osize)
std::fill_n(vect + osize, nsize - osize, nullptr);
for (i = 0; i < osize; i++) { /* rehash old part of the array */
TString *p = vect[i];
vect[i] = NULL;
Expand Down
3 changes: 1 addition & 2 deletions src/objects/ltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
t->setDummy(); /* signal that it is using dummy node */
}
else {
unsigned int i;
unsigned int lsize = luaO_ceillog2(size);
if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE)
luaG_runerror(L, "table overflow");
Expand All @@ -736,7 +735,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) {
t->setNodeArray(nodes);
t->setLsizenode(cast_byte(lsize));
t->setNoDummy();
for (i = 0; i < size; i++) {
for (unsigned int i = 0; i < size; i++) {
Node *n = gnode(t, i);
gnext(n) = 0;
n->setKeyNil();
Expand Down
Loading