Fixed ChunkEnumerator.Current NRE (#24076)
authorBenjamin Bartels <bartels.benjamin@tutanota.de>
Thu, 18 Apr 2019 12:53:22 +0000 (13:53 +0100)
committerStephen Toub <stoub@microsoft.com>
Thu, 18 Apr 2019 12:53:22 +0000 (08:53 -0400)
* Fixed ChunkEnumerator.Current NRE

* Added Nullable Flow analysis annotation comment

* Changed TODO-NULLABLE comment to link to appropriate issue

src/System.Private.CoreLib/shared/System/Text/StringBuilder.cs

index b694b7d..eee36e5 100644 (file)
@@ -629,7 +629,18 @@ namespace System.Text
             /// <summary>
             /// Implements the IEnumerator pattern.
             /// </summary>
-            public ReadOnlyMemory<char> Current => new ReadOnlyMemory<char>(_currentChunk!.m_ChunkChars, 0, _currentChunk.m_ChunkLength); // TODO-NULLABLE: NullReferenceException if called before calling MoveNext
+            public ReadOnlyMemory<char> Current
+            {
+                get
+                {
+                    if (_currentChunk == null)
+                    {
+                        ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen();
+                    }
+
+                    return new ReadOnlyMemory<char>(_currentChunk!.m_ChunkChars, 0, _currentChunk.m_ChunkLength); // TODO-NULLABLE: https://github.com/dotnet/csharplang#538
+                }
+            }
 
             #region private
             internal ChunkEnumerator(StringBuilder stringBuilder)