Skip to content

Commit

Permalink
VersionUtil: fix fd leak in .versionFor()
Browse files Browse the repository at this point in the history
If the resource was not null, the opened InputStreamReader was never closed.
Fix.

Create a separate private doReadVersion() method to avoid deeply nested
try/catch/finally blocks.
  • Loading branch information
Francis Galiegue committed Nov 13, 2012
1 parent 806f418 commit 6ebc759
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions src/main/java/com/fasterxml/jackson/core/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,58 @@ protected VersionUtil()
*/
public static Version versionFor(Class<?> cls)
{
InputStream in;
Version version = null;
final InputStream in = cls.getResourceAsStream(VERSION_FILE);

if (in == null)
return Version.unknownVersion();

try {
in = cls.getResourceAsStream(VERSION_FILE);
if (in != null) {
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
try {
return doReadVersion(reader);
} finally {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String groupStr = null, artifactStr = null;
String versionStr = br.readLine();
if (versionStr != null) {
groupStr = br.readLine();
if (groupStr != null) {
groupStr = groupStr.trim();
artifactStr = br.readLine();
if (artifactStr != null) {
artifactStr = artifactStr.trim();
}
}
}
version = parseVersion(versionStr, groupStr, artifactStr);
} finally {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
reader.close();
} catch (IOException ignored) {
}
}
} catch (IOException e) { }
return (version == null) ? Version.unknownVersion() : version;
} catch (UnsupportedEncodingException e) {
return Version.unknownVersion();
} finally {
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

private static Version doReadVersion(final Reader reader)
{
String version = null, group = null, artifact = null;

final BufferedReader br = new BufferedReader(reader);
try {
version = br.readLine();
if (version != null) {
group = br.readLine();
if (group != null)
artifact = br.readLine();
}
} catch (IOException ignored) {
} finally {
try {
br.close();
} catch (IOException ignored) {
}
}

// We don't trim() version: parseVersion() takes care ot that
if (group != null)
group = group.trim();
if (artifact != null)
artifact = artifact.trim();
return parseVersion(version, group, artifact);
}

/**
Expand Down

0 comments on commit 6ebc759

Please sign in to comment.