1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-26 08:02:55 +08:00

Fix MemoryStreamArchiveReader.GetStream() failing in some cases

`MemoryStreamArchiveReader` introduced in
0657b55196 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
This commit is contained in:
Bartłomiej Dach 2023-09-18 11:50:36 +02:00
parent 0985bb0577
commit e57d7d1205
No known key found for this signature in database

View File

@ -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()
{