Fix NullReferenceExceptions in expressions and connection strings#127750
Open
pumpkin-bit wants to merge 4 commits intodotnet:mainfrom
Open
Fix NullReferenceExceptions in expressions and connection strings#127750pumpkin-bit wants to merge 4 commits intodotnet:mainfrom
pumpkin-bit wants to merge 4 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @SamMonoRT, @dotnet/efteam |
Contributor
There was a problem hiding this comment.
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 whoseDataExpressionisnull.DataExpression.Evaluate: avoid callingSqlConvert.ChangeType2when_dataTypeisnull(and use a safe format provider when_tableisnull).DbConnectionOptions: treatnullboolean option values as invalid and throwArgumentException(via existingADP.InvalidConnectionOptionValue) rather than throwingNullReferenceException.
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. |
| 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) }); |
This was referenced May 4, 2026
Open
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem/Bug
Following an audit of the
System.Data.Commonlibrary'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 exceptionsNullReferenceExceptionandArgumentNullException.Solution
DataTable.cs: A check for
dc.DataExpression != nullhas been added to theEvaluateDependentExpressionsmethod. This prevents aNullReferenceExceptionfrom 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 != nullbefore passing data toSqlConvert.ChangeType2which prevents anArgumentNullExceptionfrom being thrown when evaluating untyped expressionsDbConnectionOptions.Common.cs: Fixed parsing of connection strings previously, an empty value for a Boolean flag caused a
NullReferenceExceptiondue to a call to.Trim()on anull stringthis case is now safely handled and throws a standardArgumentExceptionFixed:
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