Skip to content

Conversation

@iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Feb 5, 2020

PR Summary

Fix #11692

Add ValidateNotNull attribute to ErrorAction, WarningAction and InformationAction common parameters.

PR Context

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Feb 5, 2020
@iSazonov iSazonov added this to the 7.1.0-preview.1 milestone Feb 5, 2020
@ghost ghost assigned adityapatwardhan Feb 5, 2020
Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks right to me. I can't think of any negative consequences in making this change, since it currently throws NRE.

@iSazonov iSazonov force-pushed the fix-nre-common-parameters branch from edf1f50 to 6abd5c4 Compare March 9, 2020 17:34
@daxian-dbw
Copy link
Member

I think the right fix should be in the parameter binding code, where the NullReferenceException is thrown.
This could happen to any parameter of a enum type, not just the ActionPreference ones.

@iSazonov
Copy link
Collaborator Author

@daxian-dbw My initial thoughts was the same. But after I look the binding code I think the code is too sensitive (we can break something) and the simple fix is more reliable.

The code is in ReflectionParameterBinder()

s_setterMethods.TryAdd(Tuple.Create(typeof(CommonParameters), "ErrorAction"), (o, v) => ((CommonParameters)o).ErrorAction = (ActionPreference)v);

Can we safely replace (ActionPreference)v with v as ActionPreference?

@daxian-dbw
Copy link
Member

daxian-dbw commented Mar 13, 2020

The fix should be doing the same as in here:

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();

I will submit a PR shortly. See #12124. I think it can supersede this PR. @iSazonov please let me know if you have any concerns. I assigned the new PR to you for review.

@iSazonov
Copy link
Collaborator Author

@daxian-dbw #12124 returns better error message so I close current PR.

@iSazonov iSazonov closed this Mar 16, 2020
@iSazonov iSazonov deleted the fix-nre-common-parameters branch March 16, 2020 12:52
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

5 participants