Skip to content

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

Draft
Copilot wants to merge 1 commit intomainfrom
copilot/review-comments-and-add-null-checks
Draft

Fix null-forgiving operator misuse in System.Diagnostics.EventLog#126639
Copilot wants to merge 1 commit intomainfrom
copilot/review-comments-and-add-null-checks

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

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

…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 <[email protected]>
@krwq
Copy link
Copy Markdown
Member

krwq commented Apr 8, 2026

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.

private void StartRaisingEvents(string currentMachineName, string currentLogName)
{
if (!boolFlags[Flag_initializing] && !boolFlags[Flag_monitoring] && !parent!.ComponentDesignMode)
if (!boolFlags[Flag_initializing] && !boolFlags[Flag_monitoring] && parent?.ComponentDesignMode != true)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should it be parent?.ComponentDesignMode == false? if parent is null, what parent?.ComponentDesignMode != true will result?

private void StopRaisingEvents(/*string currentMachineName,*/ string currentLogName)
{
if (!boolFlags[Flag_initializing] && boolFlags[Flag_monitoring] && !parent!.ComponentDesignMode)
if (!boolFlags[Flag_initializing] && boolFlags[Flag_monitoring] && parent?.ComponentDesignMode != true)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll not comment more on similar issue

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.

3 participants