Skip to content

Fix NullReferenceExceptions in expressions and connection strings#127750

Open
pumpkin-bit wants to merge 4 commits intodotnet:mainfrom
pumpkin-bit:fix/system-data-null-evals
Open

Fix NullReferenceExceptions in expressions and connection strings#127750
pumpkin-bit wants to merge 4 commits intodotnet:mainfrom
pumpkin-bit:fix/system-data-null-evals

Conversation

@pumpkin-bit
Copy link
Copy Markdown
Contributor

Problem/Bug

Following an audit of the System.Data.Common library's code, comments marked “TODO: Possible bug” were discovered, indicating unhandled edge cases specifically working with empty or null values in expressions could result in exceptions NullReferenceException and ArgumentNullException.


Solution

DataTable.cs: A check for dc.DataExpression != null has been added to the EvaluateDependentExpressions method. This prevents a NullReferenceException from occurring if the column expression has been deleted or cleared.

DataExpression.cs: Fixed a conversion error in the Evaluate method we now check that _dataType != null before passing data to SqlConvert.ChangeType2 which prevents an ArgumentNullException from being thrown when evaluating untyped expressions

DbConnectionOptions.Common.cs: Fixed parsing of connection strings previously, an empty value for a Boolean flag caused a NullReferenceException due to a call to .Trim() on a null string this case is now safely handled and throws a standard ArgumentException

Fixed:

TODO: Possible bug: dc.DataExpression may be null
TODO: _dataType can be null this is likely a bug
TODO: Is it possible for _parsetable to contain a null value here? If so there's a bug here, investigate.

Testing

I've also attached tests to this PR

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label May 4, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @SamMonoRT, @dotnet/efteam
See info in area-owners.md if you want to be subscribed.

@pumpkin-bit pumpkin-bit changed the title Fix system data null evals Fix NullReferenceExceptions in expressions and connection strings May 4, 2026
@AndriySvyryd AndriySvyryd requested a review from Copilot May 4, 2026 16:36
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

This PR addresses reported NullReferenceException / ArgumentNullException edge cases in System.Data.Common by hardening expression evaluation and connection-string boolean parsing when internal state contains null values.

Changes:

  • DataTable.EvaluateDependentExpressions: skip evaluation for dependent columns whose DataExpression is null.
  • DataExpression.Evaluate: avoid calling SqlConvert.ChangeType2 when _dataType is null (and use a safe format provider when _table is null).
  • DbConnectionOptions: treat null boolean option values as invalid and throw ArgumentException (via existing ADP.InvalidConnectionOptionValue) rather than throwing NullReferenceException.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/libraries/System.Data.Common/tests/System/Data/DataTableTest.cs Adds regression tests for null expressions / null _dataType scenarios (currently contains reflection issues that can break/skip coverage).
src/libraries/System.Data.Common/tests/System/Data/Common/DbConnectionStringBuilderTest.cs Adds a regression test for empty boolean option values (currently can silently pass without executing).
src/libraries/System.Data.Common/src/System/Data/Filter/DataExpression.cs Avoids ChangeType2 when _dataType is null and uses a safe format provider when _table is null.
src/libraries/System.Data.Common/src/System/Data/DataTable.cs Avoids NRE by skipping dependent-expression evaluation when dc.DataExpression is null.
src/libraries/Common/src/System/Data/Common/DbConnectionOptions.Common.cs Allows null values through boolean conversion logic and throws standard invalid-option exceptions instead of NRE.

Comment thread src/libraries/System.Data.Common/tests/System/Data/DataTableTest.cs Outdated
public void DbConnectionOptions_EmptyBooleanValue_ThrowsArgumentException()
{
Type type = typeof(System.Data.Common.DbConnectionStringBuilder).Assembly.GetType("System.Data.Common.DbConnectionOptions");
if (type == null) return; // if internal type is not accessible or renamed, just return
Comment on lines +3755 to +3756

System.Reflection.MethodInfo evaluateMethod = expressionType.GetMethod("Evaluate", new Type[] { typeof(DataRow), typeof(DataRowVersion) });
pumpkin-bit and others added 2 commits May 5, 2026 08:09
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Data community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants