From 94b503d95ef00f1e38b58b024ef45bf8973a8746 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 10 Jun 2024 12:09:35 -0300 Subject: Encoding of table indices (hres) must use C indices As the encoding of array indices is (~index), 0 is encoded as -1 and INT_MAX is encoded as INT_MIN. --- ltable.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index e969adef..40a4683f 100644 --- a/ltable.c +++ b/ltable.c @@ -384,7 +384,7 @@ int luaH_next (lua_State *L, Table *t, StkId key) { int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ setivalue(s2v(key), i + 1); - farr2val(t, i + 1, tag, s2v(key + 1)); + farr2val(t, i, tag, s2v(key + 1)); return 1; } } @@ -692,7 +692,7 @@ static void reinsertOldSlice (lua_State *L, Table *t, unsigned oldasize, int tag = *getArrTag(t, i); if (!tagisempty(tag)) { /* a non-empty entry? */ TValue aux; - farr2val(t, i + 1, tag, &aux); /* copy entry into 'aux' */ + farr2val(t, i, tag, &aux); /* copy entry into 'aux' */ luaH_setint(L, t, i + 1, &aux); /* re-insert it into the table */ } } @@ -937,7 +937,7 @@ int luaH_getint (Table *t, lua_Integer key, TValue *res) { if (keyinarray(t, key)) { int tag = *getArrTag(t, key - 1); if (!tagisempty(tag)) - farr2val(t, key, tag, res); + farr2val(t, key - 1, tag, res); return tag; } else @@ -1048,11 +1048,11 @@ int luaH_psetint (Table *t, lua_Integer key, TValue *val) { if (keyinarray(t, key)) { lu_byte *tag = getArrTag(t, key - 1); if (!tagisempty(*tag) || checknoTM(t->metatable, TM_NEWINDEX)) { - fval2arr(t, key, tag, val); + fval2arr(t, key - 1, tag, val); return HOK; /* success */ } else - return ~cast_int(key); /* empty slot in the array part */ + return ~cast_int(key - 1); /* empty slot in the array part */ } else return finishnodeset(t, getintfromhash(t, key), val); @@ -1126,7 +1126,7 @@ void luaH_set (lua_State *L, Table *t, const TValue *key, TValue *value) { */ void luaH_setint (lua_State *L, Table *t, lua_Integer key, TValue *value) { if (keyinarray(t, key)) - obj2arr(t, key, value); + obj2arr(t, key - 1, value); else { int ok = rawfinishnodeset(getintfromhash(t, key), value); if (!ok) { -- cgit v1.2.3-55-g6feb