From dacac76deaf84afeac69764a4c0c038762f70212 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Sat, 17 Aug 2019 20:52:27 -0400 Subject: [PATCH] Fix DecoderExceptionFallbackBuffer message (dotnet/coreclr#26211) When the number of unknown bytes is exactly 20, the message includes a misleading " ..." at the end. Commit migrated from https://github.com/dotnet/coreclr/commit/b291538ed18d906fc4be39bc385068633eccbbfe --- .../src/System/Text/DecoderExceptionFallback.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderExceptionFallback.cs b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderExceptionFallback.cs index 90a216a..4ea00ef 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/DecoderExceptionFallback.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/DecoderExceptionFallback.cs @@ -45,10 +45,10 @@ namespace System.Text bytesUnknown ??= Array.Empty(); // Create a string representation of our bytes. - StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 3); + StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 4); - int i; - for (i = 0; i < bytesUnknown.Length && i < 20; i++) + const int MaxLength = 20; + for (int i = 0; i < bytesUnknown.Length && i < MaxLength; i++) { strBytes.Append('['); strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture)); @@ -56,8 +56,10 @@ namespace System.Text } // In case the string's really long - if (i == 20) + if (bytesUnknown.Length > MaxLength) + { strBytes.Append(" ..."); + } // Known index throw new DecoderFallbackException( -- 2.7.4