fix null ref in SqlDataReader by SimonCropp · Pull Request #4159 · dotnet/SqlClient · GitHub
Skip to content

fix null ref in SqlDataReader#4159

Merged
paulmedynski merged 4 commits into
dotnet:mainfrom
SimonCropp:fix-null-ref-in-SqlDataReader.GetChars
Apr 17, 2026
Merged

fix null ref in SqlDataReader#4159
paulmedynski merged 4 commits into
dotnet: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

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

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.

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

@paulmedynski paulmedynski 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.

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

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.

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

This was referenced Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hotfix 6.1.7 PRs targeting main that should be backported to release/6.1 branch for future hotfix Hotfix 7.0.3 PRs targeting main that should be backported to release/7.0 branch for next release.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants