From f0ad730a3798bbe60c2afa1b3274963adf085425 Mon Sep 17 00:00:00 2001 From: lethosor Date: Sun, 26 Jul 2020 02:11:20 -0400 Subject: [PATCH] Lua: Fix off-by-one preventing accessing last df-other-vectors item by ID, add tests --- library/LuaTypes.cpp | 2 +- test/structures/other_vectors.lua | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/structures/other_vectors.lua diff --git a/library/LuaTypes.cpp b/library/LuaTypes.cpp index d496ba56f..740b3f689 100644 --- a/library/LuaTypes.cpp +++ b/library/LuaTypes.cpp @@ -1451,7 +1451,7 @@ void other_vectors_identity::build_metatable(lua_State *state) auto keys = &index_enum->getKeys()[-index_enum->getFirstItem()]; - for (int64_t i = 0; i < index_enum->getLastItem(); i++) + for (int64_t i = 0; i <= index_enum->getLastItem(); i++) { lua_getfield(state, base+2, keys[i]); lua_rawseti(state, base+2, int(i)); diff --git a/test/structures/other_vectors.lua b/test/structures/other_vectors.lua new file mode 100644 index 000000000..43a50dc2a --- /dev/null +++ b/test/structures/other_vectors.lua @@ -0,0 +1,27 @@ +function test.index_name() + for _, k in ipairs(df.units_other_id) do + expect.eq(df.global.world.units.other[k]._kind, 'container') + end +end + +function test.index_name_bad() + expect.error_match(function() + expect.eq(df.global.world.units.other.SOME_FAKE_NAME, 'container') + end, 'not found.$') +end + +function test.index_id() + for i in ipairs(df.units_other_id) do + expect.eq(df.global.world.units.other[i]._kind, 'container') + end +end + +function test.index_id_bad() + expect.error_match(function() + expect.eq(df.global.world.units.other[df.units_other_id._first_item - 1], 'container') + end, 'Cannot read field') + expect.error_match(function() + expect.eq(df.global.world.units.other[df.units_other_id._last_item + 1], 'container') + end, 'Cannot read field') +end +