Skip to content

Copy only what fits in SqliteDataReader.GetValues - #39354

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
pengdows:sqlite-getvalues-short-array
Oct 22, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
pengdows:sqlite-getvalues-short-array

Conversation

@alaricd

@alaricd alaricd commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #39352

SqliteDataReader.GetValues throws for an array shorter than the row; it now copies Math.Min(values.Length, FieldCount) values and returns that count.

What the documentation says

IDataRecord.GetValues, Remarks:

You can pass an Object array that contains fewer than the number of columns that are contained in the resulting row. Only the amount of data the Object array holds is copied to the array.

Why this is a bug

The contract explicitly allows a short array and says what to do with it; throwing IndexOutOfRangeException breaks code that reads the leading columns of a row into a fixed-size buffer.

Tests

  • GetValues_throws_when_too_narrow is replaced by GetValues_copies_only_what_fits_when_too_narrow: a 2-element array for a 3-column row copies 1, 2 and returns 2; an empty array returns 0.
  • Microsoft.Data.Sqlite.Tests: 784 passed, 0 failed (7 skipped).

This is the GetValues half of #39344, which I'm closing in favour of one PR per change.

Checklist

  • I've read the guidelines for contributing
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team (Microsoft.Data.Sqlite: GetValues rejects an array shorter than the row, unlike the IDataRecord contract #39352; opened alongside so the change can be reviewed concretely; happy to wait or adjust)
  • The code builds and tests pass locally
  • Commit messages follow the format
  • Tests for the changes have been added
  • Code follows the same patterns and style as existing code in this repo

Found with https://github.com/pengdows/pengdows.AdoNetApiTest.conformance

🤖 Generated with Claude Code

GetValues threw IndexOutOfRangeException for an array shorter than the row.
IDataRecord.GetValues: "You can pass an Object array that contains fewer than
the number of columns that are contained in the resulting row. Only the amount
of data the Object array holds is copied to the array." It now copies
Math.Min(values.Length, FieldCount) values and returns that count.

Fixes dotnet#39352

Found with https://github.com/pengdows/pengdows.AdoNetApiTest.conformance

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Empty destination arrays bypass the existing no-current-row validation before the first read and after reader exhaustion.

1 open issue
What changed in this PR

This pull request updates SqliteDataReader.GetValues to copy only the values that fit in the supplied destination array, matching the IDataRecord contract.

Changes:

  • Bounds copying by Math.Min(values.Length, FieldCount) and returns the number copied.
  • Replaces the narrow-array exception test with coverage for partial and empty arrays.
  • Introduces a zero-length path that bypasses current-row validation.
File Summary
test/​Microsoft.Data.Sqlite.Tests/​SqliteDataReaderTest.cs Tests partial copying and empty destination arrays.
src/​Microsoft.Data.Sqlite.Core/​SqliteValueReader.cs Limits value copying to the destination array length.

Review effort: Lite

With the copy bounded by values.Length, an empty array skipped GetValue
and so the current-row check: GetValues(Array.Empty<object>()) returned 0
before the first Read() and after Read() returned false. SqliteDataRecord
now checks for a current row in GetValues, as it already does in GetValue.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved blocking issues were identified, and all approval-readiness assessments indicate readiness.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit c1cd96e into dotnet:main Oct 22, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Microsoft.Data.Sqlite: GetValues rejects an array shorter than the row, unlike the IDataRecord contract

3 participants