Skip to content

Commit 1e3e4fb

Browse files
rubenwardynerzhul
authored andcommittedMar 12, 2019
HPChange Reason: Fix push after free, and type being overwritten (#8359)
* HPChange Reason: Fix push after free, and type being overwritten Fixes #8227 and #8344
1 parent 3b25b80 commit 1e3e4fb

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed
 

‎src/content_sao.h

+5
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ struct PlayerHPChangeReason {
405405
bool from_mod = false;
406406
int lua_reference = -1;
407407

408+
inline bool hasLuaReference() const
409+
{
410+
return lua_reference >= 0;
411+
}
412+
408413
bool setTypeFromString(const std::string &typestr)
409414
{
410415
if (typestr == "set_hp")

‎src/script/cpp_api/s_base.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,18 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
384384

385385
void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason)
386386
{
387-
if (reason.lua_reference >= 0) {
387+
if (reason.hasLuaReference())
388388
lua_rawgeti(L, LUA_REGISTRYINDEX, reason.lua_reference);
389-
luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
390-
} else
389+
else
391390
lua_newtable(L);
392391

393-
lua_pushstring(L, reason.getTypeAsString().c_str());
394-
lua_setfield(L, -2, "type");
392+
lua_getfield(L, -1, "type");
393+
bool has_type = (bool)lua_isstring(L, -1);
394+
lua_pop(L, 1);
395+
if (!has_type) {
396+
lua_pushstring(L, reason.getTypeAsString().c_str());
397+
lua_setfield(L, -2, "type");
398+
}
395399

396400
lua_pushstring(L, reason.from_mod ? "mod" : "engine");
397401
lua_setfield(L, -2, "from");

‎src/script/lua_api/l_object.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ int ObjectRef::l_set_hp(lua_State *L)
257257
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER)
258258
getServer(L)->SendPlayerHPOrDie((PlayerSAO *)co, reason);
259259

260+
if (reason.hasLuaReference())
261+
luaL_unref(L, LUA_REGISTRYINDEX, reason.lua_reference);
262+
260263
// Return
261264
return 0;
262265
}

0 commit comments

Comments
 (0)
Please sign in to comment.