From 034d887abd4e53dd745dcb9cafc961acc6b72291 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Wed, 31 Jul 2019 20:15:29 -0500 Subject: [PATCH] modify how reading from file works to prevent accidental overreads --- Application/IO/GameLogEventDetection.cs | 2 +- Application/IO/GameLogReader.cs | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Application/IO/GameLogEventDetection.cs b/Application/IO/GameLogEventDetection.cs index f137ffe8c..c869f57dc 100644 --- a/Application/IO/GameLogEventDetection.cs +++ b/Application/IO/GameLogEventDetection.cs @@ -65,7 +65,7 @@ namespace IW4MAdmin.Application.IO previousFileSize = fileSize; - var events = await _reader.ReadEventsFromLog(_server, fileDiff, 0); + var events = await _reader.ReadEventsFromLog(_server, fileDiff, fileSize - fileDiff); foreach (var ev in events) { diff --git a/Application/IO/GameLogReader.cs b/Application/IO/GameLogReader.cs index 41026938c..a26fd154f 100644 --- a/Application/IO/GameLogReader.cs +++ b/Application/IO/GameLogReader.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Threading.Tasks; namespace IW4MAdmin.Application.IO @@ -37,13 +38,28 @@ namespace IW4MAdmin.Application.IO // open the file as a stream using (var rd = new StreamReader(new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Utilities.EncodingType)) { - // take the old start position and go back the number of new characters + char[] buff = new char[fileSizeDiff]; rd.BaseStream.Seek(-fileSizeDiff, SeekOrigin.End); + await rd.ReadAsync(buff, 0, (int)fileSizeDiff); - string newLine; - while (!string.IsNullOrEmpty(newLine = await rd.ReadLineAsync())) + var stringBuilder = new StringBuilder(); + foreach (char c in buff) { - logLines.Add(newLine); + if (c == '\n') + { + logLines.Add(stringBuilder.ToString()); + stringBuilder = new StringBuilder(); + } + + else if (c != '\r') + { + stringBuilder.Append(c); + } + } + + if (stringBuilder.Length > 0) + { + logLines.Add(stringBuilder.ToString()); } }