From cfd5be1bc59fd5fe1b30f29efb442ca1c0c26e57 Mon Sep 17 00:00:00 2001 From: Misa Date: Wed, 24 Mar 2021 11:51:39 -0700 Subject: [PATCH] Add bounds checks to tile setter and getters This removes all traces of Undefined Behavior from getting and placing tiles. This mimics the previous behavior (2.2 and below) as reasonably as possible. `vmult` was previously a vector, there was a bunch of unused space directly after the end of the usable space of the vector, which was all filled with zeroes. The same goes for `contents`, having previously been a vector, and so having a bunch of zeroes immediately following the end of the in-bounds space. That's why both are 0 if you index them out of bounds. --- desktop_version/src/editor.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index 1ca46303..1536be52 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -951,7 +951,14 @@ int editorclass::gettileidx( int mult; int idx; - mult = vmult[yoff]; + if (INBOUNDS_ARR(yoff, vmult)) + { + mult = vmult[yoff]; + } + else + { + mult = 0; + } idx = x + rx*40 + mult; @@ -967,6 +974,11 @@ void editorclass::settile( ) { const int idx = gettileidx(rx, ry, x, y); + if (!INBOUNDS_ARR(idx, contents)) + { + return; + } + contents[idx] = t; } @@ -978,6 +990,11 @@ int editorclass::gettile( ) { const int idx = gettileidx(rx, ry, x, y); + if (!INBOUNDS_ARR(idx, contents)) + { + return 0; + } + return contents[idx]; } @@ -986,10 +1003,22 @@ int editorclass::getabstile(const int x, const int y) int idx; int yoff; - yoff = vmult[y]; + if (INBOUNDS_ARR(y, vmult)) + { + yoff = vmult[y]; + } + else + { + yoff = 0; + } idx = x + yoff; + if (!INBOUNDS_ARR(idx, contents)) + { + return 0; + } + return contents[idx]; }