Skip to content

Fix null-forgiving operator misuse in System.Diagnostics.EventLog - #126639

Merged
tarekgh merged 2 commits into
mainfrom
copilot/review-comments-and-add-null-checks
Sep 17, 2026
Merged

tarekgh merged 2 commits into
mainfrom
copilot/review-comments-and-add-null-checks

Conversation

Copilot AI commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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.

  • EventLogTraceListener: EventLog property is EventLog? but all TraceEvent/TraceData methods used EventLog!.WriteEvent(...). Replaced with local capture + early return on null (6 sites).
  • EventLogInternal parent!: parent field is EventLog? and genuinely null in static write paths and listener infrastructure. Replaced parent!.ComponentDesignMode with parent?.ComponentDesignMode == true (4 sites).
  • CompletionCallback TOCTOU: onEntryWrittenHandler field was null-checked then accessed via this.onEntryWrittenHandler — racy if unsubscribed between check and invocation. Captured to local.
  • InternalWriteEvent: strings[i]!.Length after strings[i] ??= string.Empty — replaced with string s = strings[i] ??= string.Empty; if (s.Length > ...) to eliminate the suppression.

Suggested further improvements (separate PRs)

  1. IEnumerable<string?> inner nullability in Reader APIs may be overly broad — EvtVarTypeNull may be the only null variant
  2. EventRecord.FormatDescription(IEnumerable<object>? values) accepts null but a parameterless overload exists
  3. EventRecordWrittenEventArgs constructors could unify null handling
  4. LogListeningInfo fields could be readonly
  5. ref/impl nullable annotation mismatches need API review process

@krwq

krwq commented Apr 8, 2026

Copy link
Copy Markdown
Member

cc: @RenderMichael - note this PR is auto-generated

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing
See info in area-owners.md if you want to be subscribed.

@krwq
krwq force-pushed the copilot/review-comments-and-add-null-checks branch from dda36cf to aca3df6 Compare September 17, 2026 11:34
Copilot AI and others added 2 commits September 17, 2026 13:35
…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
krwq force-pushed the copilot/review-comments-and-add-null-checks branch from aca3df6 to 361889f Compare September 17, 2026 11:37
@tarekgh
tarekgh marked this pull request as ready for review September 17, 2026 17:43
Copilot AI lite review requested due to automatic review settings September 17, 2026 17:43
@azure-pipelines

Copy link
Copy Markdown
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.

@tarekgh tarekgh added this to the 12.0.0 milestone Sep 17, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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 EventLogTraceListener behavior.
  • Makes EventLogInternal parent 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 SynchronizingObject or unsubscribe the handler between the null check and invocation. Please add a deterministic regression test (for example, an ISynchronizeInvoke test double whose InvokeRequired removes the subscription) so the old this.onEntryWrittenHandler access 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

@tarekgh
tarekgh merged commit 86b2c6b into main Sep 17, 2026
79 of 82 checks passed
@tarekgh
tarekgh deleted the copilot/review-comments-and-add-null-checks branch September 17, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants