Skip to content

Bug-hunt fixes: bounded OLE chains, XLSB/XLSX writer output Excel opens cleanly, CSV date mapping, native close crash - #186

Merged
GabrielMarquezMatte merged 26 commits into
masterfrom
develop
Nov 17, 2026
Merged

GabrielMarquezMatte merged 26 commits into
masterfrom
develop

Conversation

@GabrielMarquezMatte

Copy link
Copy Markdown
Owner

Summary

develop is 21 commits ahead of master. All of them come from a bug hunt across the reader, the parser layer, the writer and the native ABI. Each fix has a regression test that failed before the fix.

Reader

  • OLE/CFB chains are bounded by the file. ChainByteLength now checks every sector against the container's sector count and sums lengths as a long, before anything is allocated. Previously a crafted FAT made a 1 MB file allocate about 135 MB, and a 17 MB file made DetectFileFormat throw OverflowException instead of returning Unknown.
  • XLSX and CSV honor cancellation on every MoveNext/MoveNextAsync. Rows already buffered used to keep coming after cancel, and a small sheet never threw. XLS and XLSB already did this.

Parser, generator, CLI

  • BuildWithAttributeFallback reads CSV with the CSV type map. Text dates such as 01/15/2024 came back as 0001-01-01 with no error.
  • The generator handles init-only properties inherited from a closed generic base (class M : Base<int>). It now emits a generic UnsafeAccessor holder. Before, every row threw MissingMethodException.
  • Parallel CSV keeps the BOM when DetectEncodingFromByteOrderMark = false, matching the sequential parser.
  • Two PropertyAt bindings on the same column now throw at build time. Before, one binding was dropped silently, and CSV and the generic path dropped different ones.
  • CLI convert writes the serial number for a date-formatted cell outside the DateTime range, as preview already did. Before, it aborted the whole conversion.

Writer (each fix checked by opening the output in Excel 16)

  • XLSB: -0.0 is written as a real cell. Its sign-bit-only RK made Excel refuse to open the whole workbook. That RK comes from Math.Round(-0.004, 2), -0.0m and similar values.
  • XLSB: sheets no longer open with the top row frozen. A copied BrtPane blob was written on every sheet.
  • XLSX, XLSB, XLS: a column with a style but no width gets Excel's default width. XLSX used to hide it (missing width reads as 0), and XLSB and XLS made it narrower than default.
  • XLSX, XLSB: a hidden first sheet stays hidden. The workbook view now points activeTab/firstSheet at the first visible sheet, as the XLS writer already did. It is written only when that sheet isn't sheet 0, so ordinary output is unchanged.
  • CSV writer: a delimiter or quote byte ≥ 0x80 is rejected. It used to collide with UTF-8 continuation bytes.
  • Sheet names, number formats and font names: an unpaired surrogate is rejected. The XLSX output used to turn it into U+FFFD.

Native ABI

  • xl_close_write_handle no longer crashes the host process when the commit move and the temp-file cleanup both fail. Cleanup in StagedFile is now best-effort, and close still returns XL_ERROR.
  • xl_open_source / xl_open_stream call release exactly once whenever the struct is readable, failures included. Before, it depended on which validation failed. The header now documents this.
  • xl_write_handle_bytes reports a failed buffer copy as XL_ERROR instead of letting the exception escape the export.

Evidence

Repro tests, before → after:

CfbContainerTests.Should_Throw_Without_Allocating_The_Chain_When_The_Directory_Chain_Runs_Past_The_File
  before: 135,540,120 bytes allocated for a 1,057,280-byte file, then InvalidDataException
  after:  InvalidDataException within a 16x-file-size allocation budget
FluentMappingTests.WithAttributeFallbackReadsCsvDateTextLikeFromAttributes
  before: When == 0001-01-01   after: 2024-01-15
XlsbWriterTests (-0.0)
  before: BrtCellRk with rk == 0x80000000; Excel fails to open the file
  after:  BrtCellReal; file opens
NativeWriterHandleTests.CloseWriteHandle_Should_Return_An_Error_When_Neither_The_Commit_Nor_The_Cleanup_Can_Touch_The_Folder
  before: UnauthorizedAccessException escapes the export; process exits 0xC0000409
  after:  XL_ERROR with a message
  • dotnet build ExcelReader.slnx: 0 warnings, 0 errors
  • dotnet test tests/ExcelReader.Tests: 4484 passed, 0 failed
  • On the native branch before merge: Python 269 passed (plus the existing xfail), cargo test --all-features green, native namespace 557 passed

Behavior changes

  • New rejections: duplicate PropertyAt column indexes, CSV writer delimiter or quote bytes ≥ 0x80, and unpaired surrogates in sheet names, number formats and font names. Each is an ArgumentException, except the duplicate PropertyAt columns, which throw InvalidOperationException.
  • Cancellation: XLSX and CSV enumerators throw OperationCanceledException on the next MoveNext after cancel, even when rows are buffered.
  • Writer output bytes change: style-only columns now carry a width, XLSB sheets have no BrtPane, and a workbook view is written when the first sheet is hidden. The native typed-write golden digests were updated for these.
  • C ABI: no signature or struct changes and XL_ABI_VERSION is unchanged. Raw C callers that freed user_data themselves after a failed xl_open_source/xl_open_stream with a NULL out_handle, NULL read/read_at or negative length must stop doing so. The bundled C++, Rust, Python and .NET Framework bindings never did.
  • No public API changes.

Merge Danger

Door: two-way

Every fix is a local change with its own commit and can be reverted on its own. No persisted data or file format version is involved.

Blast Radius: writer output

The widest effect is byte-level changes to XLSX, XLSB and XLS files the writer produces. That breaks anyone who pins output hashes, and it changes how Excel displays styled columns and hidden first sheets. Readers only change on hostile CFB input and on cancellation timing.

A chain walking FAT entries past the end of the file sized its buffer
before any sector was checked, allocating about 128x the file and, past
about 4M sectors, throwing OverflowException instead of InvalidDataException.
The token only reached the refill, so rows already buffered kept coming
after cancellation and a small sheet never observed it.
Rows parsed from the buffer, batched or not, returned without checking
the token, so cancellation was only seen at the next refill.
The merged map was built from the serial-date type map and reused for CSV,
so attribute-fallback DateTime/DateOnly/TimeOnly properties lost the text
readers FromAttributes uses: culture-formatted CSV dates stayed default.
A non-generic UnsafeAccessor cannot bind a member of a constructed generic
type, so every row threw MissingMethodException. Declare the accessor in a
generic holder that mirrors the open type's parameters and constraints.
The first chunk forced DetectEncodingFromByteOrderMark on, so an index-based
map stripped a BOM the sequential parser keeps.
One of them was silently dropped, and CSV kept the last while the other
formats kept the first. Throw like the header-name ambiguity check does.
convert aborted on a date-formatted cell whose serial has no DateTime
(Excel shows ####); write the number instead, as preview shows the text.
…tches

Parallel CSV against sequential on stray quotes, text after a closing quote
and mixed terminators; the failure row across quoted newlines at every chunk
size; header-only and all-blank text columns through ToArrowRecordBatch.
…cess

xl_close_write_handle disposed the handle in a finally outside any catch, and
StagedFile.Dispose's temp-file delete could throw. When the commit's move and
that delete both failed, the exception left the [UnmanagedCallersOnly] export
and the runtime failed fast. The delete is now best effort and the dispose
cannot throw, so close reports XL_ERROR for the failed commit.
xl_open_source and xl_open_stream called release when the options or format
were invalid, but not when the struct named a NULL callback, a negative length
or a NULL out_handle, so a caller could not tell from XL_INVALID_ARGUMENT
whether it still owned user_data. Once the struct has the right struct_size,
the library now always owns it; the header says so.
…ashing

PublishBuffer allocated and copied the finished workbook outside a try, so an
OutOfMemoryException left the export and failed the process.
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.52381% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.30%. Comparing base (cc98179) to head (a74fa4c).

Files with missing lines Patch % Lines
src/ExcelReader.Generator/ExcelRowMapGenerator.cs 0.00% 30 Missing and 1 partial ⚠️
src/ExcelReader.Core/Writer/Internal/StagedFile.cs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #186      +/-   ##
==========================================
- Coverage   92.38%   92.30%   -0.08%     
==========================================
  Files         302      302              
  Lines       25526    25602      +76     
  Branches     4194     4219      +25     
==========================================
+ Hits        23583    23633      +50     
- Misses       1319     1347      +28     
+ Partials      624      622       -2     
Flag Coverage Δ
cpp 88.77% <ø> (ø)
csharp 92.18% <69.52%> (-0.11%) ⬇️
python 96.26% <ø> (ø)
rust 92.61% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@GabrielMarquezMatte
GabrielMarquezMatte merged commit 04b3c25 into master Nov 17, 2026
46 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant