GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] CloseResource: false positive with reassignment detection · Issue #2977 · pmd/pmd

[java] CloseResource: false positive with reassignment detection #2977

New issue
Copy link
New issue
Copy link
Closed
#3147
#3061
Closed
[java] CloseResource: false positive with reassignment detection#2977
#3147
#3061
Copy link
Assignees
adangel
Labels
a:false-positivePMD flags a piece of code that is not problematic
Milestone
6.33.0

Description

@djh82
djh82
opened on Dec 15, 2020
Issue body actions

Rule: CloseResource

Upgraded to 6.30.0, having previously been using 6.28.0. Now seeing false positives with the CloseResource rule, with this contrived example:

import java.io.RandomAccessFile;

public class FalsePositive {
    private boolean isHundredBytes = false;

    public void bar() throws Exception {
        RandomAccessFile file = null;
        try {
            file = new RandomAccessFile("name", "r");
            this.isHundredBytes = file.length() == 100;  /* violation here... */
        } finally {
            file.close();
        }
    }
}

Activity

Next
  1. djh82
    added
    a:bugPMD crashes or fails to analyse a file.
    on Dec 15, 2020
  2. djh82 commented on Dec 15, 2020

    @djh82
    djh82
    on Dec 15, 2020
    Author
    More actions

    Assume it was introduced by #2811

  3. dhalperi commented on Jan 7, 2021

    @dhalperi
    dhalperi
    on Jan 7, 2021 · Hidden as off-topic
    show commentMore actions
  4. oowekyala
    added a commit that references this issue on Jan 16, 2021

    Test for pmd#2977

    c1dca31
  5. oowekyala
    added this to the 6.31.0 milestone on Jan 16, 2021
  6. oowekyala commented on Jan 16, 2021

    @oowekyala
    oowekyala
    on Jan 16, 2021 · Hidden as off-topic
    show commentMore actions
  7. oowekyala
    self-assigned this
    on Jan 16, 2021
  8. oowekyala
    added
    a:false-positivePMD flags a piece of code that is not problematic
    and removed
    a:bugPMD crashes or fails to analyse a file.
    on Jan 16, 2021
  9. oowekyala
    added a commit that references this issue on Jan 16, 2021

    Fix CloseResource reassigned stream

    b7ff1aa
  10. adangel
    added a commit that references this issue on Jan 21, 2021

    [doc] Update release notes

    1050bce
  11. adangel
    closed this as completedin #3061on Jan 21, 2021
  12. djh82 commented on Feb 1, 2021

    @djh82
    djh82
    on Feb 1, 2021
    Author
    More actions

    I'm not sure this has been fixed; still seeing issues with 6.31.0

  13. Kittera commented on Feb 18, 2021

    @Kittera
    Kittera
    on Feb 18, 2021
    More actions

    I'm not sure this has been fixed; still seeing issues with 6.31.0

    I'm seeing this myself on 6.31.0, just updated in order to test. Still flagging it even if I immediately close the Scanner.
    image
    image

  14. oowekyala commented on Feb 18, 2021

    @oowekyala
    oowekyala
    on Feb 18, 2021
    Member
    More actions

    Thanks for your feedback I'll try and see what went wrong

  15. 11 remaining items

  16. adangel commented on Feb 26, 2021

    @adangel
    adangel
    on Feb 26, 2021
    Member
    More actions

    @djh82 Thanks, I can reproduce it now. It has something to do with declaring the variable and re-assigning it.

    One possible workaround could be (depends on your actual code) to use two try-blocks:

        public void bar3() throws Exception {
            try {
                RandomAccessFile file = new RandomAccessFile("name", "r");
                try {
                    boolean isHundredBytes = file.length() == 100;
                } catch (IOException e) {
                    // no-op
                } finally {
                    file.close();
                }
            } catch (IOException e) {
                // ignored
            }
        }
  17. djh82 commented on Feb 26, 2021

    @djh82
    djh82
    on Feb 26, 2021
    Author
    More actions

    @adangel excellent. Yes, while digging out the code and writing that reproducer, I did fix the error by doing exactly that. Though I think the pattern is the usual paradigm when try-wth-resources is not available (usually with a null-guard and try/catch in the finally too).

  18. adangel
    assigned
    adangel
    and unassigned
    oowekyala
    on Feb 26, 2021
  19. adangel commented on Feb 26, 2021

    @adangel
    adangel
    on Feb 26, 2021
    Member
    More actions

    @djh82 Unfortunately I looked at the wrong file, I still had the ScannerFalsePositive sample in my test project which triggered a violation, but your file runs fine.
    So I still can't reproduce the violation. The CloseResource rule behaves correctly. I tested versions 6.29.0, 6.30.0, 6.31.0, with and without type resolution.
    I can provoke a violation, if I set the rule property "closeAsDefaultTarget" to false - then "file.close()" is ignored and the rule rightly says, the file is not closed.

    I've attached my sample project: issue-2977-sample.zip Could you please make it fail?

  20. adangel
    added
    was:unreproducibleA bug report that cannot be reproduced - reporter should provide a MWE
    on Feb 26, 2021
  21. adangel
    added a commit that references this issue on Feb 26, 2021

    [java] Update test for #2977

    1f73ebc
  22. adangel
    modified the milestones: 6.32.0, 6.33.0 on Feb 26, 2021
  23. djh82 commented on Mar 1, 2021

    @djh82
    djh82
    on Mar 1, 2021
    Author
    More actions

    @adangel OK, I've tested with 6.32.0, and I still get the same issue. I think this should fail:

    public class FalsePositive {
        private boolean isHundredBytes = false;
    
        public void bar() throws Exception {
            RandomAccessFile file = null;
            try {
                file = new RandomAccessFile("name", "r");
                this.isHundredBytes = file.length() == 100;
            } finally {
                file.close();
            }
        }
    }
  24. adangel commented on Mar 1, 2021

    @adangel
    adangel
    on Mar 1, 2021
    Member
    More actions

    @djh82 Thanks for the updates sample, now we are getting somewhere: Updated test project:
    issue-2977-sample.zip

    The reported violation:

    [INFO] PMD Failure: AnotherFalsePositive:10 Rule:CloseResource Priority:3 'file' is reassigned, but the original instance is not closed.
    

    It's present since 6.32.0, 6.31.0, 6.30.0, 6.29.0. It's not reproducible with 6.28.0.

    The line, where we display the violation, is actually this one:

    this.isHundredBytes = file.length() == 100;
    

    which doesn't make much sense - neither file is declared here nor it is assigned here....

    Reacted by David
  25. adangel
    removed
    was:unreproducibleA bug report that cannot be reproduced - reporter should provide a MWE
    on Mar 1, 2021
  26. adangel
    changed the title [-][java] 6.30.0 introduces new false positive in CloseResource rule?[/-] [+][java] CloseResource: false positive with reassignment detection[/+] on Mar 1, 2021
  27. adangel
    added a commit that references this issue on Mar 1, 2021

    [java] CloseResource: false positive with reassignment detection

    This issue will close once commit b942c50 is merged into the 'main' branch.
    b942c50
  28. adangel
    closed this as completedin b942c50on Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @adangel
    adangel

Labels

a:false-positivePMD flags a piece of code that is not problematic

Type

No type

Projects

No projects

    Milestone

    • 6.33.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.