Skip to content

Commit 24c7ba4

Browse files
authored
Fix potential crash in TelemetryFilter when retrieving value from opt… (#50565)
1 parent f80d3ac commit 24c7ba4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Cli/dotnet/Extensions/ParseResultExtensions.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,20 @@ public static void HandleDebugSwitch(this ParseResult parseResult)
258258
}
259259
public static T? SafelyGetValueForOption<T>(this ParseResult parseResult, string name)
260260
{
261-
if (parseResult.GetResult(name) is OptionResult optionResult &&
262-
!parseResult.Errors.Any(e => e.SymbolResult == optionResult))
261+
if (parseResult.GetResult(name) is OptionResult optionResult && // only return a value if there _is_ a value - default or otherwise
262+
!parseResult.Errors.Any(e => e.SymbolResult == optionResult) // only return a value if this isn't a parsing error
263+
&& optionResult.Option.ValueType.IsAssignableTo(typeof(T))) // only return a value if coercing the type won't error
263264
{
264-
return optionResult.GetValue<T>(name);
265+
// shouldn't happen because of the above checks, but we can be safe since this is only used in telemetry, and should
266+
// be resistant to errors
267+
try
268+
{
269+
return optionResult.GetValue<T>(name);
270+
}
271+
catch
272+
{
273+
return default;
274+
}
265275
}
266276
else
267277
{

0 commit comments

Comments
 (0)