From 4111fae1ce185de69d1bf532503b5d369f2a5c53 Mon Sep 17 00:00:00 2001 From: Benjamin Bartels Date: Thu, 18 Apr 2019 13:53:22 +0100 Subject: [PATCH] Fixed ChunkEnumerator.Current NRE (#24076) * Fixed ChunkEnumerator.Current NRE * Added Nullable Flow analysis annotation comment * Changed TODO-NULLABLE comment to link to appropriate issue --- .../shared/System/Text/StringBuilder.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs b/src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs index b694b7d..eee36e5 100644 --- a/src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs +++ b/src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs @@ -629,7 +629,18 @@ namespace System.Text /// /// Implements the IEnumerator pattern. /// - public ReadOnlyMemory Current => new ReadOnlyMemory(_currentChunk!.m_ChunkChars, 0, _currentChunk.m_ChunkLength); // TODO-NULLABLE: NullReferenceException if called before calling MoveNext + public ReadOnlyMemory Current + { + get + { + if (_currentChunk == null) + { + ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen(); + } + + return new ReadOnlyMemory(_currentChunk!.m_ChunkChars, 0, _currentChunk.m_ChunkLength); // TODO-NULLABLE: https://github.com/dotnet/csharplang#538 + } + } #region private internal ChunkEnumerator(StringBuilder stringBuilder) -- 2.7.4