Skip to content

Fix NullReferenceException when binding common parameters of type ActionPreference - #12124

Merged
Ilya (iSazonov) merged 1 commit into
PowerShell:masterfrom
daxian-dbw:binder
Mar 18, 2020
Merged

Ilya (iSazonov) merged 1 commit into
PowerShell:masterfrom
daxian-dbw:binder

Conversation

@daxian-dbw

@daxian-dbw Dongbo Wang (daxian-dbw) commented Mar 13, 2020 •

Copy link
Copy Markdown
Member

PR Summary

Fix #11692

Check for null value argument when assigning to the Enum type ActionPreference for common parameters, just like what we do for non-optimized setters from the below code:

Expression expr = Expression.Assign(propertyExpr, Expression.Convert(value, propertyExpr.Type));
if (propertyExpr.Type.IsValueType && Nullable.GetUnderlyingType(propertyExpr.Type) == null)
{
var throwInvalidCastExceptionExpr =
Expression.Call(Language.CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException,
Language.ExpressionCache.NullConstant,
Expression.Constant(propertyExpr.Type, typeof(Type)));
// The return type of 'ThrowInvalidCastException' is System.Object, but the method actually always
// throws 'PSInvalidCastException' when it's executed. So converting 'throwInvalidCastExceptionExpr'
// to 'propertyExpr.Type' is fine, because the conversion will never be hit.
expr = Expression.Condition(Expression.Equal(value, Language.ExpressionCache.NullConstant),
Expression.Convert(throwInvalidCastExceptionExpr, propertyExpr.Type),
expr);
}
return Expression.Lambda<Action<object, object>>(expr, new[] { target, value }).Compile();

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 16, 2020
@iSazonov
Ilya (iSazonov) merged commit c737f31 into PowerShell:master Mar 18, 2020
@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.1 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ErrorAction for Test-Path throw an NullReferenceException

3 participants