Skip to content

Conversation

@mertemr
Copy link

@mertemr mertemr commented Oct 3, 2025

Description

This PR adds null checks for $binaryReader and $fileStream in core.ps1 to prevent InvalidOperation exceptions when running scoop update.

Motivation and Context

Running scoop update in PowerShell 7 consistently threw InvalidOperation errors because Close() was called on null objects.
This change ensures the objects are checked before attempting to close them.
Fixes #6506

How Has This Been Tested?

  • Tested on PowerShell 7.4.1 (Windows 11 x64)
  • Verified that scoop update -a no longer throws InvalidOperation exceptions.

Checklist:

  • I have read the Contributing Guide.
  • I have ensured that I am targeting the develop branch.
  • I have updated the documentation accordingly.
  • I have updated the tests accordingly.
  • I have added an entry in the CHANGELOG.

Summary by CodeRabbit

  • Bug Fixes
    • Added safeguards during cleanup to prevent rare null-reference errors when closing file resources.
    • Eliminates occasional crashes when analyzing binaries, improving overall stability.
    • No changes to user-facing behavior or results; reliability is improved in edge cases.

Copilot AI review requested due to automatic review settings October 3, 2025 07:29
@coderabbitai
Copy link

coderabbitai bot commented Oct 3, 2025

Walkthrough

Adds null checks in Get-PESubsystem’s finally block within lib/core.ps1 to verify $binaryReader and $fileStream are non-null before calling Close(). No other logic or control flow changes.

Changes

Cohort / File(s) Summary of Changes
Resource cleanup safeguards
lib/core.ps1
Added conditional null checks for $binaryReader and $fileStream before .Close() in the finally block of Get-PESubsystem to avoid null-reference errors.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my whiskers at a tidy new rule,
Close only when not null—how cool!
Streams and readers, safe and sound,
No more errors hopping around.
With gentle paws I press “update,”
A clean run now—oh, what a fate! 🐇✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly and concisely states that null checks are being added to the core module before closing BinaryReader and FileStream, which matches the primary change in the PR and uses a standard commit style prefix.
Linked Issues Check ✅ Passed The implementation adds the required null checks before calling .Close() on $binaryReader and $fileStream, directly addressing the InvalidOperation errors described in issue #6506 without altering other functionality.
Out of Scope Changes Check ✅ Passed The changes are confined to adding null checks in the finally block of Get-PESubsystem and do not include any unrelated modifications or additional feature work.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca0506c and 7d7af80.

📒 Files selected for processing (1)
  • lib/core.ps1 (1 hunks)
🔇 Additional comments (1)
lib/core.ps1 (1)

19-20: LGTM! Null checks prevent InvalidOperation exceptions.

The defensive null checks before calling Close() correctly prevent the InvalidOperation exceptions reported in issue #6506. If an exception occurs during object instantiation (lines 3-4), these guards ensure that only successfully created objects are closed.


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 and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes InvalidOperation exceptions that occur when running scoop update in PowerShell 7 by adding null checks before closing BinaryReader and FileStream objects in the Get-PESubsystem function.

  • Added null checks for $binaryReader and $fileStream variables before calling their Close() methods
  • Prevents exceptions when these objects are null during the cleanup process in the finally block

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@z-Fng
Copy link
Member

z-Fng commented Nov 17, 2025

Please merge the develop branch into your branch.
Also, update the title and the changelog entry so they match each other, and make sure the first letter is capitalized.

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.

2 participants