Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buffer being too small for escaped XML characters #39

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading