From add2a9d15120185166adc6d2431ffee7d8a8b26b Mon Sep 17 00:00:00 2001 From: gdkchan Date: Thu, 20 Apr 2023 12:10:17 -0300 Subject: [PATCH] Avoid LM service crashes by not reading more than the buffer size (#4701) --- Ryujinx.Common/Memory/SpanReader.cs | 5 +++++ Ryujinx.Horizon/LogManager/Ipc/LmLogger.cs | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Ryujinx.Common/Memory/SpanReader.cs b/Ryujinx.Common/Memory/SpanReader.cs index e46649e1..673932d0 100644 --- a/Ryujinx.Common/Memory/SpanReader.cs +++ b/Ryujinx.Common/Memory/SpanReader.cs @@ -33,6 +33,11 @@ namespace Ryujinx.Common.Memory return data; } + public ReadOnlySpan GetSpanSafe(int size) + { + return GetSpan((int)Math.Min((uint)_input.Length, (uint)size)); + } + public T ReadAt(int offset) where T : unmanaged { return MemoryMarshal.Cast(_input.Slice(offset))[0]; diff --git a/Ryujinx.Horizon/LogManager/Ipc/LmLogger.cs b/Ryujinx.Horizon/LogManager/Ipc/LmLogger.cs index 8b3acb10..e930bdd7 100644 --- a/Ryujinx.Horizon/LogManager/Ipc/LmLogger.cs +++ b/Ryujinx.Horizon/LogManager/Ipc/LmLogger.cs @@ -113,7 +113,7 @@ namespace Ryujinx.Horizon.LogManager.Ipc } else if (key == LogDataChunkKey.Message) { - string text = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + string text = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); if (isHeadPacket && isTailPacket) { @@ -131,23 +131,23 @@ namespace Ryujinx.Horizon.LogManager.Ipc } else if (key == LogDataChunkKey.Filename) { - _logPacket.Filename = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + _logPacket.Filename = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); } else if (key == LogDataChunkKey.Function) { - _logPacket.Function = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + _logPacket.Function = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); } else if (key == LogDataChunkKey.Module) { - _logPacket.Module = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + _logPacket.Module = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); } else if (key == LogDataChunkKey.Thread) { - _logPacket.Thread = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + _logPacket.Thread = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); } else if (key == LogDataChunkKey.ProgramName) { - _logPacket.ProgramName = Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd(); + _logPacket.ProgramName = Encoding.UTF8.GetString(reader.GetSpanSafe(size)).TrimEnd(); } }