This issue is copy of #14514 which was closed by https://github.com/marek-safar.
So I guess that nobody want to understand problem.
Code here
class Program
{
static TimeSpan _from, _to;
static bool _isBegin;
static void Main(string[] args)
{
_from = Convert.ToDateTime("2019-05-13 09:00").TimeOfDay;
_to = Convert.ToDateTime("2019-05-13 11:00").TimeOfDay;
string text = File.ReadAllText("test.log", Encoding.GetEncoding(1251));
StringBuilder data = new StringBuilder(100000);
bool end = false;
foreach(string line in text.Split('\n'))
{
int result = NeedAppendString(line);
switch (result)
{
case 0:
break;
case 1:
data.Append(line + '\n');
break;
case 2:
end = true;
break;
}
if (end)
{
break;
}
}
Console.WriteLine(data.ToString());
}
static int NeedAppendString(string str)
{
try
{
bool flag;
if (str != null && (!str.Contains("[") || !str.Contains(']')) && _isBegin) return 1;
StringBuilder builder = new StringBuilder();
char[] array = str.ToCharArray();
for (byte i = 1; i<array.Length && array[i] != ']'; i++)
{
if (i == array.Length-1)
{
return 1;
}
builder.Append(array[i]);
}
TimeSpan time;
string timeS = builder.ToString();
if (timeS.Length != 8)
{
return _isBegin ? 1 : 0;
}
flag = TimeSpan.TryParse(timeS, out time);
if (flag)
{
if ((time >= _from) && (time <= _to))
{
_isBegin = true;
return 1;
}
if (_isBegin)
{
Console.WriteLine("Logs end");
return 2;
}
}
if (_isBegin) return 1;
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return 0;
}
}
}
Memory Leak due to code
builder.Append(array[i]);
Good work without memory leak
[?] macOS
[] Linux
[] Windows
Version Used:
Mono JIT compiler version 5.20.1.19 (tarball Thu Apr 11 09:02:17 UTC 2019)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS: __thread
SIGSEGV: altstack
Notifications: epoll
Architecture: amd64
Disabled: none
Misc: softdebug
Interpreter: yes
LLVM: yes(600)
Suspend: hybrid
GC: sgen (concurrent by default)
But this bug is on all mono version and even microsoft .net framework
test.log
Your code at some point simply ends up with an endless loop due tobyte type for the i:
for (byte i = 1; i < array.Length && array[i] != ']'; i++)
{
if (i == array.Length)
{
return 1;
}
builder.Append(array[i]);
}
Most helpful comment
Your code at some point simply ends up with an endless loop due to
bytetype for thei: