Skip to content

fix null ref in SqlDataReader#4159

Open
SimonCropp wants to merge 4 commits intodotnet:mainfrom
SimonCropp:fix-null-ref-in-SqlDataReader.GetChars
Open

fix null ref in SqlDataReader#4159
SimonCropp wants to merge 4 commits intodotnet:mainfrom
SimonCropp:fix-null-ref-in-SqlDataReader.GetChars

Conversation

@SimonCropp
Copy link
Copy Markdown
Contributor

The bug: At SqlDataReader.cs:2140, when buffer is null and bufferIndex < 0, the condition (bufferIndex < 0) is true, so execution enters the block. But buffer.Length in the exception constructor throws NullReferenceException since buffer is null.

This only affects the PLP + SequentialAccess path in GetChars. The other GetChars/GetBytes paths at lines 1760 and 1891 don't have the buffer != null && guard, so they'd NRE on the condition itself (different issue, but buffer is expected non-null there).

The fix (SqlDataReader.cs:2140): Changed buffer.Length to buffer?.Length ?? 0 so it reports size 0 when buffer is null.

The test (DataReaderTest.cs): Added GetCharsSequentialAccess_NullBufferNegativeBufferIndex_ThrowsArgumentOutOfRange which calls reader.GetChars(0, 0, null, -1, 0) with CommandBehavior.SequentialAccess on an NVARCHAR(MAX) column and asserts it throws ArgumentOutOfRangeException with paramName: "bufferIndex" — without the fix this throws NullReferenceException instead.

The bug: At SqlDataReader.cs:2140, when buffer is null and bufferIndex < 0, the condition (bufferIndex < 0) is true, so execution enters the block. But buffer.Length in the exception constructor throws NullReferenceException since buffer is null.

This only affects the PLP + SequentialAccess path in GetChars. The other GetChars/GetBytes paths at lines 1760 and 1891 don't have the buffer != null && guard, so they'd NRE on the condition itself (different issue, but buffer is expected non-null there).

The fix (SqlDataReader.cs:2140): Changed buffer.Length to buffer?.Length ?? 0 so it reports size 0 when buffer is null.

The test (DataReaderTest.cs): Added GetCharsSequentialAccess_NullBufferNegativeBufferIndex_ThrowsArgumentOutOfRange which calls reader.GetChars(0, 0, null, -1, 0) with CommandBehavior.SequentialAccess on an NVARCHAR(MAX) column and asserts it throws ArgumentOutOfRangeException with paramName: "bufferIndex" — without the fix this throws NullReferenceException instead.
Copilot AI review requested due to automatic review settings April 8, 2026 03:57
@SimonCropp SimonCropp requested a review from a team as a code owner April 8, 2026 03:57
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Apr 8, 2026
@apoorvdeshmukh apoorvdeshmukh self-assigned this Apr 8, 2026
@apoorvdeshmukh apoorvdeshmukh added this to the 7.1.0 milestone Apr 8, 2026
@apoorvdeshmukh
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a NullReferenceException in SqlDataReader.GetChars when called in the PLP + CommandBehavior.SequentialAccess path with buffer == null and a negative bufferIndex, ensuring the intended ArgumentOutOfRangeException is thrown instead.

Changes:

  • Update SqlDataReader.GetChars (PLP + SequentialAccess) to avoid dereferencing buffer.Length when buffer is null while constructing the argument exception.
  • Add a regression test covering GetChars with SequentialAccess + PLP, buffer == null, and negative bufferIndex.
  • Add additional tests for non-sequential GetChars and GetBytes null-buffer + negative bufferIndex behavior (currently inconsistent with implementation).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs Prevents NRE when building the invalid bufferIndex exception in the PLP + SequentialAccess GetChars path; also adds some redundant null-guards in exception-path validation.
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderTest.cs Adds a regression test for the sequential PLP scenario and two additional tests that currently don’t match product behavior when buffer == null.

@paulmedynski paulmedynski self-assigned this Apr 10, 2026
@paulmedynski paulmedynski modified the milestones: 7.1.0, 7.1.0-preview1 Apr 10, 2026
@paulmedynski paulmedynski added the Hotfix Candidate 🚑 Issues/PRs that are candidate for backporting to earlier supported versions. label Apr 10, 2026
Copy link
Copy Markdown
Contributor

@paulmedynski paulmedynski left a comment

Choose a reason for hiding this comment

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

Please address the Copilot comments.

@github-project-automation github-project-automation bot moved this from To triage to In progress in SqlClient Board Apr 10, 2026
@SimonCropp SimonCropp marked this pull request as draft April 10, 2026 12:18
Copilot AI review requested due to automatic review settings April 12, 2026 00:40
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@paulmedynski paulmedynski marked this pull request as ready for review April 14, 2026 16:37
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board Apr 14, 2026
@paulmedynski paulmedynski enabled auto-merge (squash) April 14, 2026 16:38
@paulmedynski
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hotfix Candidate 🚑 Issues/PRs that are candidate for backporting to earlier supported versions.

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

4 participants