From 8dfb7bcf67f8d9707573604785321a687e650d42 Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Wed, 16 Feb 2022 08:42:39 +0100
Subject: [PATCH] fix two potential crashes

---
 src/map/map.cpp        | 9 ++++-----
 src/map/script_map.cpp | 5 +++++
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/map/map.cpp b/src/map/map.cpp
index 3494247cf..5812f9a88 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -255,8 +255,10 @@ bool UnitTypeCanBeAt(const CUnitType &type, const Vec2i &pos)
 
 	for (int addy = 0; addy < type.TileHeight; ++addy) {
 		for (int addx = 0; addx < type.TileWidth; ++addx) {
-			if (Map.Info.IsPointOnMap(pos.x + addx, pos.y + addy) == false
-				|| Map.Field(pos.x + addx + index)->CheckMask(mask) == true) {
+			if (!Map.Info.IsPointOnMap(pos.x + addx, pos.y + addy)) {
+				return false;
+			}
+			if (!type.BoolFlag[NONSOLID_INDEX].value && Map.Field(pos.x + addx + index)->CheckMask(mask)) {
 				return false;
 			}
 		}
@@ -276,9 +278,6 @@ bool UnitTypeCanBeAt(const CUnitType &type, const Vec2i &pos)
 bool UnitCanBeAt(const CUnit &unit, const Vec2i &pos)
 {
 	Assert(unit.Type);
-	if (unit.Type->BoolFlag[NONSOLID_INDEX].value) {
-		return true;
-	}
 	return UnitTypeCanBeAt(*unit.Type, pos);
 }
 
diff --git a/src/map/script_map.cpp b/src/map/script_map.cpp
index a1d175f97..3d013a35d 100644
--- a/src/map/script_map.cpp
+++ b/src/map/script_map.cpp
@@ -925,6 +925,11 @@ static int CclGetTileTerrainHasFlag(lua_State *l)
 	LuaCheckArgs(l, 3);
 
 	const Vec2i pos(LuaToNumber(l, 1), LuaToNumber(l, 2));
+	if (pos.x >= Map.Info.MapWidth || pos.y >= Map.Info.MapHeight || pos.x < 0 || pos.y < 0) {
+		// out of bounds, doesn't have it
+		lua_pushboolean(l, 0);
+		return 1;
+	}
 
 	unsigned short flag = 0;
 	const char *flag_name = LuaToString(l, 3);