Skip to content

Return the field length from GetChars when the buffer is null - #1067

Merged
michelebastione merged 1 commit into
mini-software:masterfrom
vtxe02023274:fix/getchars-null-buffer
Nov 23, 2026
Merged

michelebastione merged 1 commit into
mini-software:masterfrom
vtxe02023274:fix/getchars-null-buffer

Conversation

@vtxe02023274

@vtxe02023274 vtxe02023274 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

GetChars(i, 0, null, 0, 0) returns 0 instead of the field length.

IDataRecord.GetChars says a null buffer returns the length of the field in characters, and callers use it to size the buffer before the real read:

var length = reader.GetChars(0, 0, null, 0, 0); // 0, expected 4 for "John"
var buffer = new char[length];
reader.GetChars(0, 0, buffer, 0, buffer.Length); // reads nothing

The base reader took Math.Min(length, ...) with length 0, so it always returned 0 for a null buffer. Now a null buffer returns the field length; reads into a buffer are unchanged.

Added a test to the CSV and the OpenXml data reader tests. Both fail before the change (expected 4, actual 0) and pass after. MiniExcel.Csv.Tests (91) and MiniExcel.OpenXml.Tests (564) pass on net10.0.

Summary by CodeRabbit

  • Bug Fixes
    • CSV and Excel data readers now correctly return a text field’s length when queried without a character buffer, and copy characters into the buffer when one is provided.
  • Tests
    • Added coverage for character-length reporting and copying in both data reader formats.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GetChars now returns the field length when called with a null buffer. Tests cover this behavior and verify character copying for CSV and OpenXML data readers.

Changes: GetChars behavior

Layer / File(s) Summary
Handle null buffers and verify character copying
src/MiniExcel.Core/MiniExcelDataReaderBase.cs, tests/MiniExcel.Csv.Tests/DataReader/CsvDataReaderTests.cs, tests/MiniExcel.OpenXml.Tests/DataReader/OpenXmlDataReaderTests.cs
GetChars returns the field length when buffer is null. Tests verify the returned length and copied characters for CSV and OpenXML data readers.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix

Suggested reviewers: michelebastione

Merge Risk: Minimal · up to 7f1a5

GetChars now returns the field length when the buffer is null, so callers can size a buffer before reading. Reads into a non-null buffer are described as unchanged, and tests cover both cases. The change is low-risk and ready to merge; the failing CI job is a workflow configuration issue unrelated to this change.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the primary change: GetChars returns the field length when the buffer is null. The raw summary says GetChars returns the string length when buffer is null, and the objectives describe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the primary change: GetChars returns the field length when the buffer is null. The raw summary says GetChars returns the string length when buffer is null, and the objectives describe the same behavior change. The title matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ff3cfeaf-26bf-4313-b7ec-bca17450ec75
Commits

Reviewing files that changed from the base of the PR and between 574d751 and 7f1a58b.

Files selected for processing: 3

  • src/MiniExcel.Core/MiniExcelDataReaderBase.cs
  • tests/MiniExcel.Csv.Tests/DataReader/CsvDataReaderTests.cs
  • tests/MiniExcel.OpenXml.Tests/DataReader/OpenXmlDataReaderTests.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@michelebastione
michelebastione merged commit 07d487c into mini-software:master Nov 23, 2026
8 of 10 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.

2 participants