Skip to content

fix: guard null stream/reader/writer in PE subsys functions - #6711

Open
somls wants to merge 1 commit into
ScoopInstaller:masterfrom
somls:fix/pesubsystem-nullcheck
Open

fix: guard null stream/reader/writer in PE subsys functions#6711
somls wants to merge 1 commit into
ScoopInstaller:masterfrom
somls:fix/pesubsystem-nullcheck

Conversation

@somls

@somls somls commented Aug 10, 2026

Copy link
Copy Markdown

Fix null-reference errors when creating shims during install.

Get-PESubsystem and Set-PESubsystem unconditionally called .Close() on stream/reader/writer objects in their finally blocks. If the constructor threw (e.g. file missing or unreadable), those variables were still $null, causing:

You cannot call a method on a null-valued expression.

This commit adds null checks and also closes $binaryWriter in Set-PESubsystem, which was previously leaked.

Get-PESubsystem/Set-PESubsystem could error when the constructor threw,
because the finally block unconditionally called Close() on $null.
Add null checks, and also close $binaryWriter in Set-PESubsystem.
Fixes install shim errors like:

  You cannot call a method on a null-valued expression.
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

Summary by CodeRabbit

  • Bug Fixes
    • Improved subsystem resource cleanup to prevent errors when stream, reader, or writer resources were not initialized.
    • Ensured binary writer resources are properly closed during cleanup.

Walkthrough

Changes

PE subsystem resource cleanup

Layer / File(s) Summary
Guard resource cleanup
lib/core.ps1
Get-PESubsystem and Set-PESubsystem close initialized readers and streams conditionally. Set-PESubsystem also closes the binary writer.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the null-resource cleanup fix in the PE subsystem functions.
Description check ✅ Passed The description directly explains the null-reference fix and the binary writer cleanup change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
lib/core.ps1 (1)

1-1: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add the BOM required by PSScriptAnalyzer.

PSScriptAnalyzer reports PSUseBOMForUnicodeEncodedFile for lib/core.ps1. Save the file with a BOM to clear the warning and preserve its Unicode encoding.

Source: Linters/SAST tools

🧹 Nitpick comments (1)
lib/core.ps1 (1)

43-45: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Close the shared stream in ownership order.

$binaryReader and $binaryWriter both wrap $fileStream, and each Close() closes its underlying stream. Close $binaryWriter first, then $binaryReader, and $fileStream last. Alternatively, construct the wrappers with leaveOpen = $true and close $fileStream once. (learn.microsoft.com)

Proposed cleanup order
 finally {
+    if ($binaryWriter) { $binaryWriter.Close() }
     if ($binaryReader) { $binaryReader.Close() }
     if ($fileStream) { $fileStream.Close() }
-    if ($binaryWriter) { $binaryWriter.Close() }
 }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: fc40f167-bfeb-48d1-8f7d-92a6648eeeb7

📥 Commits

Reviewing files that changed from the base of the PR and between b588a06 and f63c481.

📒 Files selected for processing (1)
  • lib/core.ps1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant