Bug 561259 - [resource] closing inner resource of wrapper chain causes warning out outer resource
Summary: [resource] closing inner resource of wrapper chain causes warning out outer r...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.15   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.16 M1   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords: regression
Depends on:
Blocks:
 
Reported: 2020-03-19 10:32 EDT by Stephan Herrmann CLA
Modified: 2020-04-07 08:03 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stephan Herrmann CLA 2020-03-19 10:32:54 EDT
Example from https://www.eclipse.org/forums/index.php?t=msg&th=1102916&goto=1823096&#msg_1823096

  protected void replace(IFile file, String lineSeparator) throws IOException, CoreException
  {
    InputStream contents = file.getContents();
    String charset = file.getCharset();
    BufferedReader reader = new BufferedReader(new InputStreamReader(contents, charset));
    CharArrayWriter writer = new CharArrayWriter();
    int c;
    while ((c = reader.read()) != -1)
    {
      writer.write(c);
    }
    contents.close();
    String string = writer.toString();
    String newContents = LINE_DELIMITER_PATTERN.matcher(string).replaceAll(lineSeparator);
    byte[] bytes = newContents.getBytes(charset);
    file.setContents(new ByteArrayInputStream(bytes), IResource.FORCE, null);
  }

Wrongly complains that 'reader' is never closed.
Comment 1 Stephan Herrmann CLA 2020-03-19 10:35:18 EDT
While the change is triggered by one of the [resource] related fixes in 4.15M1 (not sure which one exactly), the bug appears to be in method FakedTrackingVariable.pickMoreUnsafe(..) which has not been touched in that time frame.
Comment 2 Ed Merks CLA 2020-03-19 10:39:08 EDT
Sorry, I should up updated my other development environments more often. :-(

For what it's worth, I have a similar problem in an Oomph method:

  public static String getContents(IFile file) throws CoreException, IOException
  {
    InputStream contents = null;

    try
    {
      contents = file.getContents();
      BufferedReader reader = new BufferedReader(new InputStreamReader(contents, file.getCharset()));
      CharArrayWriter caw = new CharArrayWriter();

      int c;
      while ((c = reader.read()) != -1)
      {
        caw.write(c);
      }

      return caw.toString();
    }
    finally
    {
      IOUtil.closeSilent(contents);
    }
  }
Comment 3 Eclipse Genie CLA 2020-03-19 13:55:50 EDT
New Gerrit change created: https://git.eclipse.org/r/159725
Comment 4 Stephan Herrmann CLA 2020-03-19 14:17:54 EDT
(In reply to Eclipse Genie from comment #3)
> New Gerrit change created: https://git.eclipse.org/r/159725

Test & fix. The problem is triggered by code added via bug 519740, the buggy code location, however, is from bug 411098 (Jan 2014).
Comment 6 Stephan Herrmann CLA 2020-03-19 15:36:39 EDT
(In reply to Eclipse Genie from comment #5)
> Gerrit change https://git.eclipse.org/r/159725 was merged to [master].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/
> ?id=07329519f5ac8fd8379eab265d25b2c09a24fd96

Released to master for 4.16 M1

Thanks, Ed.

(Yes, comment 2 is essentially the same pattern, except that IOUtil.closeSilent() is not among the well-known ways of invoking close() - I don't even know which library provides it).
Comment 7 Manoj N Palat CLA 2020-04-07 08:03:44 EDT
Verified for Eclipse Version: 2020-06 (4.16) M1 with Build id: I20200406-0600