Skip to content

fix null ref in SqlDataReader#4159

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

fix null ref in SqlDataReader#4159
paulmedynski merged 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).

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.20%. Comparing base (52c7149) to head (37ffbf4).
⚠️ Report is 19 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (52c7149) and HEAD (37ffbf4). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (52c7149) HEAD (37ffbf4)
CI-SqlClient 1 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4159       +/-   ##
===========================================
- Coverage   74.27%   64.20%   -10.07%     
===========================================
  Files         279      272        -7     
  Lines       42980    65763    +22783     
===========================================
+ Hits        31922    42222    +10300     
- Misses      11058    23541    +12483     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.20% <100.00%> (?)

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

☔ View full report in Codecov by Sentry.
📢 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.

@mdaigle mdaigle added Hotfix 7.0.2 PRs targeting main whose changes should be backported to release/7.0 for the 7.0.2 release. Hotfix 6.1.6 and removed Hotfix Candidate 🚑 Issues/PRs that are candidate for backporting to earlier supported versions. labels Apr 17, 2026
@paulmedynski paulmedynski merged commit e0176e1 into dotnet:main Apr 17, 2026
306 of 307 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board Apr 17, 2026
github-actions Bot pushed a commit that referenced this pull request Apr 17, 2026
github-actions Bot pushed a commit that referenced this pull request Apr 17, 2026
@SimonCropp SimonCropp deleted the fix-null-ref-in-SqlDataReader.GetChars branch April 18, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hotfix 6.1.6 Hotfix 7.0.2 PRs targeting main whose changes should be backported to release/7.0 for the 7.0.2 release.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants