Skip to content

Commit 9252a1d

Browse files
committed
Dispose will reset the state to not lead to undefined behavior
1 parent b002d69 commit 9252a1d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Changed
10+
- `Dispose` resets the `ValueStringBuilder` to the initial state, so it doesn't lead to undefined behavior when used again
11+
912
## [1.18.5] - 2023-10-19
1013

1114
### Changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,14 @@ public readonly int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
286286
/// Disposes the instance and returns rented buffer from an array pool if needed.
287287
/// </summary>
288288
[MethodImpl(MethodImplOptions.AggressiveInlining)]
289-
public readonly void Dispose()
289+
public void Dispose()
290290
{
291291
if (arrayFromPool != null)
292292
{
293293
ArrayPool<char>.Shared.Return(arrayFromPool);
294294
}
295+
296+
this = default;
295297
}
296298

297299
/// <summary>

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,14 @@ public void GivenAString_WhenEnumerating_ThenShouldReturnCharacters()
503503

504504
output.Should().Be("Hello World");
505505
}
506+
507+
[Fact]
508+
public void GivenStringBuilder_WhenDisposed_ThenEmtpyStringReturned()
509+
{
510+
var builder = new ValueStringBuilder("Hello World");
511+
512+
builder.Dispose();
513+
514+
builder.ToString().Should().Be(string.Empty);
515+
}
506516
}

0 commit comments

Comments
 (0)