Skip to content

Fix resource tracking after PrintWriter.append - #4288

Merged
hazendaz merged 1 commit into
spotbugs:masterfrom
imgusguo:fix/4274-printwriter-append-resource
Sep 6, 2026
Merged

hazendaz merged 1 commit into
spotbugs:masterfrom
imgusguo:fix/4274-printwriter-append-resource

Conversation

@imgusguo

@imgusguo imgusguo commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Fixes #4274.

Assigning the result of PrintWriter.append() back to a local variable loses the writer's resource identity, so FindOpenStream reports a leak even when the writer is closed in finally.

Preserve the receiver's resource value for the three PrintWriter.append overloads. Limit this behavior to resources created as PrintWriter, since subclasses can override append to return a different writer, including when called through a PrintWriter variable. Other calls and resource-escape handling remain unchanged.

Add regression coverage for all three overloads, chaining, closing through an alias, missing closes, closing a different writer, and subclass overrides with both subclass and superclass variable types.

Validation on JDK 21:

  • Before the fix: 5 closed-writer cases reproduce the false positive; 4 genuine-leak cases pass.

  • After the fix: all 9 regression cases pass.

  • Complete :spotbugs-tests:test suite: 1,149 passed, 11 skipped, no failures or errors.

  • ./gradlew spotlessApply build smoketest --no-daemon --no-scan --max-workers=4: BUILD SUCCESSFUL.

  • git diff --check passes.

  • Added an entry into CHANGELOG.md.

@hazendaz hazendaz self-assigned this Sep 6, 2026
@hazendaz hazendaz added this to the Spotbugs 4.10.5 milestone Sep 6, 2026
@hazendaz
hazendaz merged commit ab3db02 into spotbugs:master Sep 6, 2026
3 checks passed
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.

FALSE POSITIVE: OS_OPEN_STREAM reports after assigning PrintWriter.append() back to the same variable

2 participants