fix(core): add null checks before closing BinaryReader/FileStream in update process
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?
- [x] Tested on PowerShell 7.4.1 (Windows 11 x64)
- [x] Verified that
scoop update -ano longer throws InvalidOperation exceptions.
Checklist:
- [x] I have read the Contributing Guide.
- [x] I have ensured that I am targeting the
developbranch. - [ ] 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.
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 safeguardslib/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 ca0506cf3e26d6b1038270dc32bc4f4804dc9a01 and 7d7af80385f9f7eecac2f80bd9258c285d849158.
📒 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.
Comment @coderabbitai help to get the list of available commands and usage tips.
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.