Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CID-156932] - Fix the imaginary resource leak in NullOutputStream #172

Merged
merged 2 commits into from
Jun 30, 2017

Conversation

oleg-nenashev
Copy link
Member

@oleg-nenashev oleg-nenashev commented Jun 25, 2017

DigestOutputStream may change in the future, hence it worth to just refactor the code. I have not found annotations which would indicate that no closing it required for NullOutputStream. There are such warnings in the core as well OTOH

@reviewbybees

DigestOutputStream may change in the future, hence it worth to just refactor the code
@ghost
Copy link

ghost commented Jun 25, 2017

This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation.

@@ -74,8 +74,10 @@ static Checksum forFile(File file) throws IOException {
static Checksum forURL(URL url) throws IOException {
try {
MessageDigest md = MessageDigest.getInstance(JarLoaderImpl.DIGEST_ALGORITHM);
Util.copy(url.openStream(), new DigestOutputStream(new NullOutputStream(), md));
return new Checksum(md.digest(), md.getDigestLength() / 8);
try(OutputStream ostream = new DigestOutputStream(new NullOutputStream(), md)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you actually were concerned about NullOutputStream leaking something (impossible), you would use

try (NullOutputStream nos = new NullOutputStream();
     OutputStream ostream = new DigestOutputStream(nos)) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really required, because DigestOutputStream closes the nested one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless DigestOutputStream.<init> throws an exception. That is why as a rule you should declare every Closeable object you are constructing as a separate variable in the block.

Util.copy(url.openStream(), new DigestOutputStream(new NullOutputStream(), md));
return new Checksum(md.digest(), md.getDigestLength() / 8);
try(OutputStream ostream = new DigestOutputStream(new NullOutputStream(), md)) {
Util.copy(url.openStream(), ostream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But here you are failing to provably close the one stream here which might actually hold open a socket or file handle! So you need to include this InputStream amongst the closeables being bound.

@oleg-nenashev oleg-nenashev merged commit 78ca6c2 into jenkinsci:master Jun 30, 2017
@oleg-nenashev oleg-nenashev deleted the coverity/CID-156932 branch July 4, 2017 16:41
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.

3 participants