Fix Shift+F1 from Space Station tileset not switching to Ship

So... it looks like being able to switch through tilesets backwards has
been in 2.3 for a while, guess no one just uses 2.3 or the level editor
that much. It seems like it's always been broken, too.

If you were on the Space Station tileset (tileset 0), pressing Shift+F1
would keep you on the Space Station tileset instead of switching to the
Ship (tileset 4).

It looks like the problem here was mixing size_t and int together - so
the modulus operation promoted the left-hand side to size_t, which is
unsigned, so the -1 turned into SIZE_MAX, which is 18446744073709551615
on my system. You'll note that that ends in a 5, so the number is
divisible by 5, meaning taking it modulo 5 leads to 0. So the tileset
would be kept at 0.

At least unsigned integer underflow/overflow is properly defined, so
there's no UB here. Just careless type mixing going on.

The solution is to make the modulus an int instead of a size_t. This
introduces an implicit conversion, but I don't care because my compiler
doesn't warn about it, and implicit conversion warnings ought to be
disabled on MSVC anyway.
This commit is contained in:
Misa 2021-01-08 15:02:17 -08:00 committed by Ethan Lee
parent 3dd1dcc131
commit 952a130595
1 changed files with 1 additions and 1 deletions

View File

@ -1508,7 +1508,7 @@ void editorclass::switch_tileset(const bool reversed /*= false*/)
tiles++;
}
const size_t modulus = SDL_arraysize(tilesets);
const int modulus = SDL_arraysize(tilesets);
tiles = (tiles % modulus + modulus) % modulus;
room.tileset = tiles;