Skip to content

Commit 4d924b6

Browse files
committed
Updated for Factorio version 1.1.104.
1 parent aa2e1f5 commit 4d924b6

File tree

11 files changed

+122
-74
lines changed

11 files changed

+122
-74
lines changed

src/LuaCPPUtilities.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ inline const char* lua_pushstring(lua_State* L, const std::string& s)
88
inline const char* lua_pushstring(lua_State* L, std::string_view s)
99
{ return lua_pushlstring(L, s.data(), s.size()); }
1010

11-
inline void lua_getfield(lua_State* L, int idx, const std::string& k)
11+
inline void lua_getfield(lua_State* L, int idx, const std::string& k)
1212
{ lua_getlfield(L, idx, k.c_str(), k.size()); }
1313

14-
inline void lua_setfield(lua_State* L, int idx, const std::string& k)
14+
inline void lua_setfield(lua_State* L, int idx, const std::string& k)
1515
{ lua_setlfield(L, idx, k.c_str(), k.size()); }
16+
17+
template<class T>
18+
inline std::enable_if_t<!std::is_same_v<T, lua_Number>> lua_pushnumber(lua_State* L, T value)
19+
{ lua_pushnumber(L, lua_Number(value)); }

src/lbaselib.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,27 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
309309
}
310310

311311

312+
/*
313+
** Loads a chunk and pushes an executer function to the stack. If the loading
314+
** resulted in an error, pushes nil and an error string. Standard Lua 5.2 allows
315+
** loading strings containing either Lua code or precompiled bytecode, but
316+
** bytecode loading has been removed due to it being the entry point for many
317+
** security vulnerabilities.
318+
*/
312319
static int luaB_load (lua_State *L) {
313320
int status;
314321
size_t l;
315322
const char *s = lua_tolstring(L, 1, &l);
316-
const char *mode = luaL_optstring(L, 3, "bt");
317323
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
318324
if (s != NULL) { /* loading a string? */
319325
const char *chunkname = luaL_optstring(L, 2, s);
320-
status = luaL_loadbufferx(L, s, l, chunkname, mode);
326+
status = luaL_loadbufferx(L, s, l, chunkname, "t"); /* always text mode */
321327
}
322328
else { /* loading from a reader function */
323329
const char *chunkname = luaL_optstring(L, 2, "=(load)");
324330
luaL_checktype(L, 1, LUA_TFUNCTION);
325331
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
326-
status = lua_load(L, generic_reader, NULL, chunkname, mode);
332+
status = lua_load(L, generic_reader, NULL, chunkname, "t"); /* always text mode */
327333
}
328334
return load_aux(L, status, env);
329335
}

src/ldebug.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,21 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n,
119119
StkId *pos) {
120120
const char *name = NULL;
121121
StkId base;
122+
StkId limit = (ci == L->ci) ? L->top : ci->next->func;
122123
if (isLua(ci)) {
123124
if (n < 0) /* access to vararg values? */
124125
return findvararg(ci, -n, pos);
125126
else {
126127
base = ci->u.l.base;
127-
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
128+
if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
129+
name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
130+
else
131+
return NULL; /* no name */
128132
}
129133
}
130134
else
131135
base = ci->func + 1;
132136
if (name == NULL) { /* no 'standard' name? */
133-
StkId limit = (ci == L->ci) ? L->top : ci->next->func;
134137
if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
135138
name = "(*temporary)"; /* generic name for any valid slot */
136139
else

src/ldo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ int luaD_precall (lua_State *L, StkId func, int nresults) {
328328
CallInfo *ci;
329329
int n; /* number of arguments (Lua) or returns (C) */
330330
ptrdiff_t funcr = savestack(L, func);
331+
if (luaF_hasopenupval_at_or_above(L, func))
332+
luaG_runerror(L, "bytecode error, cannot call stack level with open upval at that level or above");
331333
switch (ttype(func)) {
332334
case LUA_TLCF: /* light C function */
333335
f = fvalue(func);

src/lfunc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ UpVal *luaF_newupval (lua_State *L) {
4444
}
4545

4646

47+
bool luaF_hasopenupval_at_or_above(lua_State* L, StkId level) {
48+
return L->openupval != NULL && gco2uv(L->openupval)->v >= level;
49+
}
50+
4751
UpVal *luaF_findupval (lua_State *L, StkId level) {
4852
global_State *g = G(L);
4953
GCObject **pp = &L->openupval;

src/lfunc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LUAI_FUNC Proto *luaF_newproto (lua_State *L);
2222
LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems);
2323
LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems);
2424
LUAI_FUNC UpVal *luaF_newupval (lua_State *L);
25+
LUAI_FUNC bool luaF_hasopenupval_at_or_above(lua_State* L, StkId level);
2526
LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);
2627
LUAI_FUNC void luaF_close (lua_State *L, StkId level);
2728
LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);

src/lmathlib.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@ static double custom_atan(double x)
8484
{
8585
// Taken from http://golang.org/src/pkg/math/atan.go
8686

87-
if (x == 0)
88-
return x;
89-
else if (x > 0)
87+
if (x > 0)
9088
return custom_satan(x);
91-
else
89+
if (x < 0)
9290
return -custom_satan(-x);
91+
return x;
9392
}
9493

9594
static double custom_atan2(double y, double x)
9695
{
96+
if (isnan(y) || isnan(x))
97+
return NAN;
9798
if (x > 0)
9899
return custom_atan(y / x);
99100
else if (x < 0)

src/lstate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ static void preinit_state (lua_State *L, global_State *g) {
218218
L->nny = 1;
219219
L->status = LUA_OK;
220220
L->errfunc = 0;
221-
L->userData = 0;
221+
L->userData1 = 0;
222+
L->userData2 = 0;
222223
}
223224

224225

src/lstate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ struct lua_State {
174174
struct lua_longjmp *errorJmp; /* current error recover point */
175175
ptrdiff_t errfunc; /* current error handling function (stack index) */
176176
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
177-
void* userData; /* custom user-data pointer */
177+
void* userData1; /* custom user-data pointer */
178+
void* userData2; /* custom user-data pointer */
178179
};
179180

180181

0 commit comments

Comments
 (0)