From a707842e14dde468781270198ae63757ca3c2716 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Thu, 16 Feb 2023 11:16:31 -0300
Subject: [PATCH] Validate dimensions before creating texture (#4430)

---
 Ryujinx.Graphics.Gpu/Image/TextureCache.cs | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/Ryujinx.Graphics.Gpu/Image/TextureCache.cs b/Ryujinx.Graphics.Gpu/Image/TextureCache.cs
index f18de60751..261d060382 100644
--- a/Ryujinx.Graphics.Gpu/Image/TextureCache.cs
+++ b/Ryujinx.Graphics.Gpu/Image/TextureCache.cs
@@ -477,7 +477,23 @@ namespace Ryujinx.Graphics.Gpu.Image
                 // If the start address is unmapped, let's try to find a page of memory that is mapped.
                 if (address == MemoryManager.PteUnmapped)
                 {
-                    address = memoryManager.TranslateFirstMapped(info.GpuAddress, (ulong)info.CalculateSizeInfo(layerSize).TotalSize);
+                    // Make sure that the dimensions are valid before calculating the texture size.
+                    if (info.Width < 1 || info.Height < 1 || info.Levels < 1)
+                    {
+                        return null;
+                    }
+
+                    if ((info.Target == Target.Texture3D ||
+                         info.Target == Target.Texture2DArray ||
+                         info.Target == Target.Texture2DMultisampleArray ||
+                         info.Target == Target.CubemapArray) && info.DepthOrLayers < 1)
+                    {
+                        return null;
+                    }
+
+                    ulong dataSize = (ulong)info.CalculateSizeInfo(layerSize).TotalSize;
+
+                    address = memoryManager.TranslateFirstMapped(info.GpuAddress, dataSize);
                 }
 
                 // If address is still invalid, the texture is fully unmapped, so it has no data, just return null.