Skip to content

Commit

Permalink
Merge pull request #39 from sveinungf/dev/buffer-bug
Browse files Browse the repository at this point in the history
Fix buffer being too small for escaped XML characters
  • Loading branch information
sveinungf authored Feb 27, 2024
2 parents d0ff36d + 60ff3e0 commit 0f1113b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
29 changes: 29 additions & 0 deletions SpreadCheetah.Test/Tests/SpreadsheetRowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,35 @@ public async Task Spreadsheet_AddRow_MultipleRows(CellType type, RowCollectionTy
Assert.Equal(values, actualValues);
}

[Theory]
[MemberData(nameof(TestData.CellTypes), MemberType = typeof(TestData))]
public async Task Spreadsheet_AddRow_MultipleRowsWithCharactersToEscape(CellType type, RowCollectionType rowType)
{
// Arrange
const string cellValue = "'This \" text & has & &&&&&&' characters < that > needs & to & be & escaped!'";
var values = Enumerable.Repeat(cellValue, 1000);
using var stream = new MemoryStream();
var options = new SpreadCheetahOptions { BufferSize = SpreadCheetahOptions.MinimumBufferSize };
await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream, options);
await spreadsheet.StartWorksheetAsync("Sheet");

// Act
foreach (var value in values)
{
await spreadsheet.AddRowAsync(CellFactory.Create(type, value), rowType);
}

await spreadsheet.FinishAsync();


// Assert
SpreadsheetAssert.Valid(stream);
using var workbook = new XLWorkbook(stream);
var worksheet = workbook.Worksheets.Single();
var actualValues = worksheet.Cells(true).Select(x => x.Value.GetText());
Assert.All(actualValues, x => Assert.Equal(cellValue, x));
}

[Theory]
[MemberData(nameof(TestData.CellAndValueTypes), MemberType = typeof(TestData))]
public async Task Spreadsheet_AddRow_ExplicitCellReferences(CellValueType valueType, bool isNull, CellType cellType, RowCollectionType rowType)
Expand Down
28 changes: 0 additions & 28 deletions SpreadCheetah/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:SpreadCheetah.SourceGeneration.WorksheetRowMetadataServices.CreateObjectInfo``1(System.Func{SpreadCheetah.Spreadsheet,``0,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask},System.Func{SpreadCheetah.Spreadsheet,System.Collections.Generic.IEnumerable{``0},System.Threading.CancellationToken,System.Threading.Tasks.ValueTask})</Target>
<Left>lib/net6.0/SpreadCheetah.dll</Left>
<Right>lib/net6.0/SpreadCheetah.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:SpreadCheetah.SourceGeneration.WorksheetRowMetadataServices.CreateObjectInfo``1(System.Func{SpreadCheetah.Spreadsheet,``0,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask},System.Func{SpreadCheetah.Spreadsheet,System.Collections.Generic.IEnumerable{``0},System.Threading.CancellationToken,System.Threading.Tasks.ValueTask})</Target>
<Left>lib/net7.0/SpreadCheetah.dll</Left>
<Right>lib/net7.0/SpreadCheetah.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:SpreadCheetah.SourceGeneration.WorksheetRowMetadataServices.CreateObjectInfo``1(System.Func{SpreadCheetah.Spreadsheet,``0,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask},System.Func{SpreadCheetah.Spreadsheet,System.Collections.Generic.IEnumerable{``0},System.Threading.CancellationToken,System.Threading.Tasks.ValueTask})</Target>
<Left>lib/net8.0/SpreadCheetah.dll</Left>
<Right>lib/net8.0/SpreadCheetah.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:SpreadCheetah.SourceGeneration.WorksheetRowMetadataServices.CreateObjectInfo``1(System.Func{SpreadCheetah.Spreadsheet,``0,System.Threading.CancellationToken,System.Threading.Tasks.ValueTask},System.Func{SpreadCheetah.Spreadsheet,System.Collections.Generic.IEnumerable{``0},System.Threading.CancellationToken,System.Threading.Tasks.ValueTask})</Target>
<Left>lib/netstandard2.0/SpreadCheetah.dll</Left>
<Right>lib/netstandard2.0/SpreadCheetah.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0008</DiagnosticId>
<Target>T:SpreadCheetah.SpreadCheetahCompressionLevel</Target>
Expand Down
3 changes: 2 additions & 1 deletion SpreadCheetah/Helpers/XmlUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private static int IndexOfCharToEscape(ReadOnlySpan<char> source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryXmlEncodeToUtf8(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten)
{
if (!Utf8Helper.DestinationCanFitTranscodedString(source, destination))
// Worst case: " becomes &quot; (1 character becomes 6 bytes)
if (destination.Length < source.Length * 6)
{
bytesWritten = 0;
return false;
Expand Down
4 changes: 2 additions & 2 deletions SpreadCheetah/SpreadCheetah.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<!-- NuGet package -->
<PackageId>SpreadCheetah</PackageId>
<Version>1.13.0</Version>
<Version>1.13.1</Version>
<Authors>sveinungf</Authors>
<Description>SpreadCheetah is a high-performance .NET library for generating spreadsheet (Microsoft Excel XLSX) files.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -34,7 +34,7 @@

<!-- Package validation -->
<EnablePackageValidation>true</EnablePackageValidation>
<PackageValidationBaselineVersion>1.12.0</PackageValidationBaselineVersion>
<PackageValidationBaselineVersion>1.13.0</PackageValidationBaselineVersion>
<EnableStrictModeForCompatibleTfms>true</EnableStrictModeForCompatibleTfms>
<EnableStrictModeForCompatibleFrameworksInPackage>true</EnableStrictModeForCompatibleFrameworksInPackage>
</PropertyGroup>
Expand Down

0 comments on commit 0f1113b

Please sign in to comment.