Fix null-forgiving operator misuse in System.Diagnostics.EventLog - #126639
Merged
Merged
Conversation
Member
|
cc: @RenderMichael - note this PR is auto-generated |
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing |
tarekgh
reviewed
Apr 8, 2026
tarekgh
reviewed
Apr 8, 2026
krwq
force-pushed
the
copilot/review-comments-and-add-null-checks
branch
from
September 17, 2026 11:34
dda36cf to
aca3df6
Compare
…LogInternal - Replace EventLog! with proper null guards in all TraceEvent/TraceData methods - Replace parent! with parent?. null-conditional in EnableRaisingEvents, SynchronizingObject, StartRaisingEvents, and StopRaisingEvents - Capture onEntryWrittenHandler to local variable in CompletionCallback to prevent potential TOCTOU race condition - Replace strings[i]! with local variable after null-coalescing assignment Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/98985e7e-eb8b-4ef4-8b52-7b43d0c32b35 Co-authored-by: krwq <660048+krwq@users.noreply.github.com>
Remove remaining parent null suppressions, stabilize callback state, and add regression coverage for nullable trace listeners. Copilot-Session-Id: bd9868f4-0a45-4919-a545-e10034ae7a13 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
krwq
force-pushed
the
copilot/review-comments-and-add-null-checks
branch
from
September 17, 2026 11:37
aca3df6 to
361889f
Compare
tarekgh
approved these changes
Sep 17, 2026
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
One or more issues must be addressed before approval.
Pull request overview
This PR removes unsafe null-forgiving usage in System.Diagnostics.EventLog and adds null-listener coverage.
Changes:
- Adds null-safe
EventLogTraceListenerbehavior. - Makes
EventLogInternalparent and callback handling null/race-safe. - Removes remaining null-forgiving operators in write paths.
File summaries
| File | Description |
|---|---|
| src/libraries/System.Diagnostics.EventLog/tests/EventLogTraceListenerTests.cs | Updated as part of this pull request. |
| src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLogTraceListener.cs | Updated as part of this pull request. |
| src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLogInternal.cs | Updated as part of this pull request. |
| src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLog.cs | Updated as part of this pull request. |
Review details
Suppressed comments (1)
src/libraries/System.Diagnostics.EventLog/src/System/Diagnostics/EventLogInternal.cs:566
- This change fixes a callback TOCTOU race, but the added tests do not exercise it: the existing event tests never configure
SynchronizingObjector unsubscribe the handler between the null check and invocation. Please add a deterministic regression test (for example, anISynchronizeInvoketest double whoseInvokeRequiredremoves the subscription) so the oldthis.onEntryWrittenHandleraccess would fail and this local capture is verified.
[!NOTE] This review comment was created by GitHub Copilot.
EntryWrittenEventHandler? handler = onEntryWrittenHandler;
if (handler is not null)
{
ISynchronizeInvoke? synchronizingObject = SynchronizingObject;
if (synchronizingObject is not null && synchronizingObject.InvokeRequired)
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Lite
This was referenced Sep 18, 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.
Description
Follow-up to #119891 (NRT annotations for
System.Diagnostics.EventLog). Addresses unresolved review comments by replacing unsafe null-forgiving operator (!) usage with proper null checks where the nullable type is genuine.EventLogproperty isEventLog?but allTraceEvent/TraceDatamethods usedEventLog!.WriteEvent(...). Replaced with local capture + early return on null (6 sites).parent!:parentfield isEventLog?and genuinely null in static write paths and listener infrastructure. Replacedparent!.ComponentDesignModewithparent?.ComponentDesignMode == true(4 sites).onEntryWrittenHandlerfield was null-checked then accessed viathis.onEntryWrittenHandler— racy if unsubscribed between check and invocation. Captured to local.strings[i]!.Lengthafterstrings[i] ??= string.Empty— replaced withstring s = strings[i] ??= string.Empty; if (s.Length > ...)to eliminate the suppression.Suggested further improvements (separate PRs)
IEnumerable<string?>inner nullability in Reader APIs may be overly broad —EvtVarTypeNullmay be the only null variantEventRecord.FormatDescription(IEnumerable<object>? values)accepts null but a parameterless overload existsEventRecordWrittenEventArgsconstructors could unify null handlingLogListeningInfofields could bereadonly