Fix Rethrowing exception variable dereferenced variable may be null#328
Open
odaysec wants to merge 2 commits intomicrosoft:mainfrom
Open
Fix Rethrowing exception variable dereferenced variable may be null#328odaysec wants to merge 2 commits intomicrosoft:mainfrom
odaysec wants to merge 2 commits intomicrosoft:mainfrom
Conversation
odaysec
commented
Feb 26, 2026
kochrac
approved these changes
Feb 26, 2026
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.
fix keep the existing exception handling behavior but change the rethrow to preserve the original stack trace. In C#, that means using a bare
throw;instead ofthrow ex;inside acatchblock. We should not change the control flow or the cancellation logic, only the way the exception is rethrown. No new methods or imports are needed.Concretely, in
samples/electron-winml/winMlAddon/chatClient.cs, locate thecatch (Exception ex)block around lines 72–77. Replacethrow ex;on line 76 withthrow;. Everything else in the method remains unchanged; this preserves the original exception (including stack) while still propagating it when the token has not been cancelled.Rethrowing an exception variable will lose the stack trace in the original exception, and replace it with the stack trace from the
throwstatement. This will make debugging the root cause of the exception more difficult, for example if the stack trace is written to a log file. This shows an exception handler which sets the status toUnexpectedExceptionif an exception is thrown. However it throws ex which discards the original stack trace containing the source of the error.The
fixis to replace the throw statement as follows:Since the variable
exis no longer needed, the catch variable has been removed.References
MSDN Creating and Throwing Exceptions (C# Programming Guide)
MSDN throw (C# Reference)