From e57d7d1205c2e6914b352ddc451167138d4c3dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Mon, 18 Sep 2023 11:50:36 +0200 Subject: [PATCH] Fix `MemoryStreamArchiveReader.GetStream()` failing in some cases `MemoryStreamArchiveReader` introduced in 0657b551964986fc5504a202eaa5e699d1c72f00 would previously use `MemoryStream.GetBuffer()` to retrieve the underlying byte buffer with stream data. However, this is not generally the method you would want, for two reasons: 1. It can fail if the stream wasn't created in the way that supports it. 2. As per https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.getbuffer?view=net-7.0#system-io-memorystream-getbuffer, it will return the _raw_ contents of the buffer, including potentially unused bytes. To fix, use `MemoryStream.ToArray()` instead, which avoids both pitfalls. Notably, `ToArray()` always returns the full contents of the buffer, regardless of `Position`, as documented in: https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.toarray?view=net-7.0#system-io-memorystream-toarray --- osu.Game/IO/Archives/MemoryStreamArchiveReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs b/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs index 37ce1e508e..d8e1199e93 100644 --- a/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs +++ b/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs @@ -19,7 +19,7 @@ namespace osu.Game.IO.Archives this.stream = stream; } - public override Stream GetStream(string name) => new MemoryStream(stream.GetBuffer(), 0, (int)stream.Length); + public override Stream GetStream(string name) => new MemoryStream(stream.ToArray(), 0, (int)stream.Length); public override void Dispose() {