fix: #3144 Reader/Writer Texture2D null support & test to guarantee it never happens again

This commit is contained in:
vis2k 2022-04-29 09:59:52 +08:00
parent b709453057
commit ae1c7c5fe4
3 changed files with 28 additions and 1 deletions

View File

@ -274,8 +274,12 @@ public static Texture2D ReadTexture2D(this NetworkReader reader)
// TODO allocation protection when sending textures to server.
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
// read width & height
// support 'null' textures for [SyncVar]s etc.
// https://github.com/vis2k/Mirror/issues/3144
short width = reader.ReadShort();
if (width == -1) return null;
// read height
short height = reader.ReadShort();
Texture2D texture2D = new Texture2D(width, height);

View File

@ -294,6 +294,15 @@ public static void WriteTexture2D(this NetworkWriter writer, Texture2D texture2D
// TODO allocation protection when sending textures to server.
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
// support 'null' textures for [SyncVar]s etc.
// https://github.com/vis2k/Mirror/issues/3144
// simply send -1 for width.
if (texture2D == null)
{
writer.WriteShort(-1);
return;
}
// write dimensions first so reader can create the texture with size
// 32k x 32k short is more than enough
writer.WriteShort((short)texture2D.width);

View File

@ -1484,5 +1484,19 @@ public void WriteTexture2D_normal()
Assert.That(texture.height, Is.EqualTo(Texture2D.normalTexture.height));
Assert.That(texture.GetPixels32().SequenceEqual(Texture2D.normalTexture.GetPixels32()));
}
// test to prevent https://github.com/vis2k/Mirror/issues/3144
[Test]
public void WriteTexture2D_Null()
{
// write
NetworkWriter writer = new NetworkWriter();
writer.WriteTexture2D(null);
// read
NetworkReader reader = new NetworkReader(writer.ToArray());
Texture2D texture = reader.ReadTexture2D();
Assert.That(texture, Is.Null);
}
}
}