From 952a130595945c118cba49843381e9ac8afdd99f Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 8 Jan 2021 15:02:17 -0800 Subject: [PATCH] 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. --- desktop_version/src/editor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index b131b657..15775222 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -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;