From a38faad15604b8d9bcb7b95a0880ebe150f1c80d Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 25 Sep 2020 14:08:34 -0700 Subject: [PATCH] Add bounds checks to indexing of global "temporary" variable `k` For some reason, the variable `k` is on entityclass and gets mutated in createentity() and createblock(). Then updateentities() uses it without checking if it's valid, because either `k` or the size of `entities` could have changed in the meantime. To fix any potential undefined behavior, these bounds checks should be added. --- desktop_version/src/Entity.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index 3f9d29ab..94c17487 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -2736,8 +2736,8 @@ bool entityclass::updateentities( int i ) if (entities[i].state == 1) { //happy! - if (entities[k].rule == 6) entities[k].tile = 0; - if (entities[k].rule == 7) entities[k].tile = 6; + if (INBOUNDS_VEC(k, entities) && entities[k].rule == 6) entities[k].tile = 0; + if (INBOUNDS_VEC(k, entities) && entities[k].rule == 7) entities[k].tile = 6; //Stay close to the hero! int j = getplayer(); if (INBOUNDS_VEC(j, entities) && entities[j].xp > entities[i].xp + 5)