From 8a8436f73d4c4edd10b43313b724f32ccb7b957e Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Wed, 20 Jun 2012 13:56:57 -0400 Subject: Fixes for Coverity issues of type Resource Leaks - Remaining --- .../cms/publish/publishers/FileBasedPublisher.java | 18 ++++++----- .../src/com/netscape/cmscore/cert/CertUtils.java | 35 ++++++++++++++-------- .../cmscore/ldapconn/LdapJssSSLSocketFactory.java | 7 +++++ .../com/netscape/cmscore/security/PWsdrCache.java | 23 ++++++++++++-- .../com/netscape/cmscore/util/FileAsString.java | 23 +++++++------- 5 files changed, 74 insertions(+), 32 deletions(-) (limited to 'base/common') diff --git a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java index c41025b54..59effbe81 100644 --- a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java +++ b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java @@ -317,7 +317,7 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { String baseName = mDir + File.separator + namePrefix[0]; String tempFile = baseName + ".temp"; FileOutputStream fos; - ZipOutputStream zos; + ZipOutputStream zos = null; byte[] encodedArray = null; File destFile = null; String destName = null; @@ -329,12 +329,16 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { fos.write(encodedArray); fos.close(); if (mZipCRL) { - zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); - zos.setLevel(mZipLevel); - zos.putNextEntry(new ZipEntry(baseName + ".der")); - zos.write(encodedArray, 0, encodedArray.length); - zos.closeEntry(); - zos.close(); + try { + zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); + zos.setLevel(mZipLevel); + zos.putNextEntry(new ZipEntry(baseName + ".der")); + zos.write(encodedArray, 0, encodedArray.length); + zos.closeEntry(); + } finally { + if (zos != null) + zos.close(); + } } destName = baseName + ".der"; destFile = new File(destName); diff --git a/base/common/src/com/netscape/cmscore/cert/CertUtils.java b/base/common/src/com/netscape/cmscore/cert/CertUtils.java index d443781ae..ee1e1568c 100644 --- a/base/common/src/com/netscape/cmscore/cert/CertUtils.java +++ b/base/common/src/com/netscape/cmscore/cert/CertUtils.java @@ -525,23 +525,34 @@ public class CertUtils { public static byte[] readFromFile(String fileName) throws IOException { - FileInputStream fin = new FileInputStream(fileName); - int available = fin.available(); - byte[] ba = new byte[available]; - int nRead = fin.read(ba); - - if (nRead != available) - throw new IOException("Error reading data from file: " + fileName); - fin.close(); - return ba; + FileInputStream fin = null; + try { + fin = new FileInputStream(fileName); + int available = fin.available(); + byte[] ba = new byte[available]; + int nRead = fin.read(ba); + + if (nRead != available) + throw new IOException("Error reading data from file: " + fileName); + + return ba; + } finally { + if (fin != null) + fin.close(); + } } public static void storeInFile(String fileName, byte[] ba) throws IOException { - FileOutputStream fout = new FileOutputStream(fileName); + FileOutputStream fout = null; + try { + fout = new FileOutputStream(fileName); - fout.write(ba); - fout.close(); + fout.write(ba); + } finally { + if (fout != null) + fout.close(); + } } public static String toMIME64(X509CertImpl cert) { diff --git a/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java b/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java index bad9e3b23..38651d0ba 100644 --- a/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java +++ b/base/common/src/com/netscape/cmscore/ldapconn/LdapJssSSLSocketFactory.java @@ -78,6 +78,13 @@ public class LdapJssSSLSocketFactory implements LDAPSSLSocketFactoryExt { throw new LDAPException( "Cannot Create JSS SSL Socket - Unknown host"); } catch (IOException e) { + if (s != null) { + try { + s.close(); + } catch (IOException e1) { + e1.printStackTrace(); + } + } log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_LDAPCONN_IO_ERROR", e.toString())); throw new LDAPException("IO Error creating JSS SSL Socket"); } diff --git a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java index 17cc9f0a7..b0445bd6d 100644 --- a/base/common/src/com/netscape/cmscore/security/PWsdrCache.java +++ b/base/common/src/com/netscape/cmscore/security/PWsdrCache.java @@ -325,13 +325,21 @@ public class PWsdrCache { bos.write(readbuf, 0, numRead); totalRead += numRead; } - inputs.close(); + } catch (FileNotFoundException e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); } catch (IOException e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); + } finally { + if (inputs != null) { + try { + inputs.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } if (totalRead > 0) { @@ -354,6 +362,7 @@ public class PWsdrCache { * encrypts and writes the whole String buf into pwcache.db */ public void writePWcache(String bufs) throws EBaseException { + FileOutputStream outstream = null; try { Encryptor sdr = new Encryptor(mToken, mKeyID, Encryptor.DEFAULT_ENCRYPTION_ALG); @@ -376,10 +385,10 @@ public class PWsdrCache { tmpPWcache.delete(); tmpPWcache = new File(mPWcachedb + ".tmp"); } - FileOutputStream outstream = new FileOutputStream(mPWcachedb + ".tmp"); + outstream = new FileOutputStream(mPWcachedb + ".tmp"); outstream.write(writebuf); - outstream.close(); + File origFile = new File(mPWcachedb); @@ -427,6 +436,14 @@ public class PWsdrCache { } catch (Exception e) { log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_SECURITY_PW_FILE", mPWcachedb, e.toString())); throw new EBaseException(e.toString() + ": " + mPWcachedb); + } finally { + if (outstream != null) { + try { + outstream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } } diff --git a/base/common/src/com/netscape/cmscore/util/FileAsString.java b/base/common/src/com/netscape/cmscore/util/FileAsString.java index 7853346f5..ea8fb796d 100644 --- a/base/common/src/com/netscape/cmscore/util/FileAsString.java +++ b/base/common/src/com/netscape/cmscore/util/FileAsString.java @@ -52,16 +52,19 @@ public class FileAsString { BufferedReader br = createBufferedReader(mFilename); StringBuffer buf = new StringBuffer(); int bytesread = 0; - - do { - char cbuf[] = new char[16]; - - bytesread = br.read(cbuf, 0, cbuf.length); - if (bytesread > 0) { - buf.append(cbuf, 0, bytesread); - } - } while (bytesread != -1); - br.close(); + try { + do { + char cbuf[] = new char[16]; + + bytesread = br.read(cbuf, 0, cbuf.length); + if (bytesread > 0) { + buf.append(cbuf, 0, bytesread); + } + } while (bytesread != -1); + } finally { + if (br != null) + br.close(); + } fileContents = new String(buf); } -- cgit