From 6efb5975f3c15295fcb05896a74279e80146242b Mon Sep 17 00:00:00 2001 From: Rex Henderson Date: Tue, 28 Jul 2020 12:25:00 -0500 Subject: [PATCH] EchoAsync fix for proper decoding UTF-8 buffers --- src/SyslogProxy/Proxy.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/SyslogProxy/Proxy.cs b/src/SyslogProxy/Proxy.cs index 80870cb..b385ba3 100644 --- a/src/SyslogProxy/Proxy.cs +++ b/src/SyslogProxy/Proxy.cs @@ -43,6 +43,8 @@ private async Task EchoAsync(TcpClient client) Logger.Information("New client connected from IP: [{0}].", ipAddress); using (client) { + var decoder = Encoding.UTF8.GetDecoder(); + char[] decoded = new char[BufferSize]; var stream = client.GetStream(); var buf = new byte[BufferSize]; var accumulator = new StringBuilder(); @@ -63,7 +65,10 @@ private async Task EchoAsync(TcpClient client) { break; } - accumulator.Append(Encoding.UTF8.GetString(buf).TrimEnd('\0')); + + var countDecoded = decoder.GetChars(buf, 0, amountReadTask.Result, decoded, 0); + accumulator.Append(decoded, 0, countDecoded); + if (accumulator.ToString().Contains("\n")) { var splitMessage = accumulator.ToString().Split('\n').ToList();