From 51fa1b2cb0bb54c0eb235c9d1fa68a7e1abaf464 Mon Sep 17 00:00:00 2001 From: Mary Date: Mon, 25 Oct 2021 00:13:20 +0200 Subject: hle: Improve safety (#2778) * timezone: Make timezone implementation safe * hle: Do not use TrimEnd to parse ASCII strings This adds an util that handle reading an ASCII string in a safe way. Previously it was possible to read malformed data that could cause various undefined behaviours in multiple services. * hid: Remove an useless unsafe modifier on keyboard update * Address gdkchan's comment * Address gdkchan's comment --- Ryujinx.HLE/Utilities/StringUtils.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'Ryujinx.HLE/Utilities/StringUtils.cs') diff --git a/Ryujinx.HLE/Utilities/StringUtils.cs b/Ryujinx.HLE/Utilities/StringUtils.cs index 4142ab5b..2b7cbffe 100644 --- a/Ryujinx.HLE/Utilities/StringUtils.cs +++ b/Ryujinx.HLE/Utilities/StringUtils.cs @@ -36,6 +36,15 @@ namespace Ryujinx.HLE.Utilities return output; } + public static string ReadInlinedAsciiString(BinaryReader reader, int maxSize) + { + byte[] data = reader.ReadBytes(maxSize); + + int stringSize = Array.IndexOf(data, 0); + + return Encoding.ASCII.GetString(data, 0, stringSize < 0 ? maxSize : stringSize); + } + public static byte[] HexToBytes(string hexString) { // Ignore last character if HexLength % 2 != 0. @@ -107,7 +116,7 @@ namespace Ryujinx.HLE.Utilities } } - public static unsafe int CompareCStr(char* s1, char* s2) + public static int CompareCStr(ReadOnlySpan s1, ReadOnlySpan s2) { int s1Index = 0; int s2Index = 0; @@ -121,7 +130,7 @@ namespace Ryujinx.HLE.Utilities return s2[s2Index] - s1[s1Index]; } - public static unsafe int LengthCstr(char* s) + public static int LengthCstr(ReadOnlySpan s) { int i = 0; -- cgit v1.2.3