Bug 383965 - [compiler][resource] Error/Warning "Potential resource leak" triggered for methods which return InputStream
Summary: [compiler][resource] Error/Warning "Potential resource leak" triggered for me...
Status: REOPENED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks: 386665
  Show dependency tree
 
Reported: 2012-06-30 12:58 EDT by Scott Pedigo CLA
Modified: 2022-07-03 14:24 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Scott Pedigo CLA 2012-06-30 12:58:54 EDT
When under Preferences -> Java -> Compiler -> Errors/Warning -> Potential programming problems, the option Potential resource leak is set to Warning or Error, then a warning or error is generated for methods which return an InputStream. Obviously, a method returning an InputStream is not going to want to close the InputStream. In the following sample, the error or warning is reported for the line "return is;" The method _using_ the InputStream is the one where a warning needs to be displayed.

-- Sample Code --

 	/**
	 * Returns an input stream from the specified properties file.
	 * 
	 * @param source String the name of the properties file.
	 * @return InputStream the input stream, or null.
	 */
	private static InputStream getInputStream(String source) {
		boolean logDebugEnabled = LOG.isTraceEnabled();
		if (logDebugEnabled) {
			Object[][] params = {{"source", source}};
			LOG.entering(PropertyUtils.class, "getInputStream", params);
		}
		InputStream is = null;
		try {
			URL url = null;

			// Try retrieving with the class loader.
			if (is == null) {
				try {
					url = PropertyUtils.class.getResource("//" + source);
					if (url != null) {
						is = url.openStream();
					}
				}
				catch (Exception e) {
				}
			}

			// Try retrieving with the system classloader.
			if (is == null) {
				url = ClassLoader.getSystemClassLoader().getResource(source);
				if (url != null) {
					try {
						is = url.openStream();
					}
					catch (IOException e) {
					}
				}
			}

			// Try looking in the file system.
			if (is == null) {
				try {
					is = new FileInputStream(source);
				}
				catch (IOException e) {
				}
			}

			// Return the input stream, if found, otherwise null.
			return is;
		}
		finally {
			if (logDebugEnabled) {
				LOG.exiting(is != null ? "OK" : "not found");
			}
		}
	}
 
-- Configuration Details --
Product: Eclipse 1.5.0.20120131-1544 (org.eclipse.epp.package.jee.product)
Installed Features:
 org.eclipse.jdt 3.8.0.v20120525-1249-8-8nFqlFNOfwKDRGz-pXLdGxEM83
Comment 1 Stephan Herrmann CLA 2012-06-30 17:04:11 EDT
Thanks for the report. This is indeed a bug in the implementation.

What I can see from quick tests:

The warning goes away if you comment one of the lines "is = url.openStream();"

The bug is probably related to the fact that the compiler sees multiple
assignments to "is" and fails to see that at most one of these assignments
binds an actual non-null value.

A workaround would be to change the second (or all) "if (is == null) {"
into:
  if (is != null) return is;

This has the same behavior and makes the bogus warning go away.
Comment 2 Eclipse Genie CLA 2020-03-26 08:06:34 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 3 Stephan Herrmann CLA 2020-03-26 12:50:06 EDT
Still relevant, let's see if we can fix this for 4.16 (time permitting).
Comment 4 Stephan Herrmann CLA 2020-06-10 03:33:59 EDT
.
Comment 5 Eclipse Genie CLA 2022-07-03 14:24:43 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.