>From ce95f60290f1f0bfec8ef0e02c115e0c6d1f2aaa Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Tue, 26 Jun 2012 15:37:15 -0400
Subject: [PATCH] LeftOver Cases in Resource Leaks and NULL_RETURNS

---
 base/ca/src/com/netscape/ca/CAService.java         |   11 +++---
 .../src/com/netscape/ca/CertificateAuthority.java  |    4 +-
 .../cms/publish/publishers/FileBasedPublisher.java |   39 ++++++++++++++------
 .../com/netscape/cmscore/dbs/KeyRepository.java    |    5 ++-
 .../com/netscape/cmscore/logging/LogSubsystem.java |    7 ++--
 .../src/com/netscape/cmscore/logging/Logger.java   |    8 +++-
 .../src/com/netscape/cmstools/PKCS12Export.java    |   14 +++++--
 base/kra/src/com/netscape/kra/StorageKeyUnit.java  |   15 +++++---
 .../com/netscape/pkisilent/http/HTTPClient.java    |   11 ++----
 .../security/x509/CRLDistributionPoint.java        |   11 +++++-
 10 files changed, 82 insertions(+), 43 deletions(-)

diff --git a/base/ca/src/com/netscape/ca/CAService.java b/base/ca/src/com/netscape/ca/CAService.java
index 12011ced4f85507dd769d71ae411db556dd6f515..19778505e8ecf2503eb3d798a7ddfc249a36ded8 100644
--- a/base/ca/src/com/netscape/ca/CAService.java
+++ b/base/ca/src/com/netscape/ca/CAService.java
@@ -1927,15 +1927,14 @@ class serviceGetRevocationInfo implements IServant {
 
         while (enum1.hasMoreElements()) {
             String name = enum1.nextElement();
-
+            RevocationInfo info = null;
             if (name.equals(IRequest.ISSUED_CERTS)) {
                 X509CertImpl certsToCheck[] =
                         request.getExtDataInCertArray(IRequest.ISSUED_CERTS);
-
-                CertificateRepository certDB = (CertificateRepository) mCA.getCertificateRepository();
-                RevocationInfo info =
-                        certDB.isCertificateRevoked(certsToCheck[0]);
-
+                if (certsToCheck != null) {
+                    CertificateRepository certDB = (CertificateRepository) mCA.getCertificateRepository();
+                    info = certDB.isCertificateRevoked(certsToCheck[0]);
+                }
                 if (info != null) {
                     RevokedCertImpl revokedCerts[] = new RevokedCertImpl[1];
                     RevokedCertImpl revokedCert = new RevokedCertImpl(
diff --git a/base/ca/src/com/netscape/ca/CertificateAuthority.java b/base/ca/src/com/netscape/ca/CertificateAuthority.java
index feecec6a87a3aeffb7dda871648f336fa1b7a37a..962378ce195afdeb8f86a261e0346fb5ec6a9386 100644
--- a/base/ca/src/com/netscape/ca/CertificateAuthority.java
+++ b/base/ca/src/com/netscape/ca/CertificateAuthority.java
@@ -1896,7 +1896,7 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             return response;
         } catch (Exception e) {
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_CA_CA_OCSP_REQUEST", e.toString()));
-            return null;
+            throw new EBaseException(CMS.getLogMessage("CMSCORE_CA_CA_OCSP_REQUEST"));
         }
     }
 
@@ -1940,7 +1940,7 @@ public class CertificateAuthority implements ICertificateAuthority, ICertAuthori
             e.printStackTrace();
             // error e
             log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_CA_CA_OCSP_SIGN", e.toString()));
-            return null;
+            throw new EBaseException(CMS.getLogMessage("CMSCORE_CA_CA_OCSP_SIGN", e.toString()));
         }
     }
 
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 59effbe81698c3732ce734791764b2ce235d3c01..535cab26d596d991457f914b43c4087143ba3545 100644
--- a/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java
+++ b/base/common/src/com/netscape/cms/publish/publishers/FileBasedPublisher.java
@@ -287,6 +287,7 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo {
     public void publish(LDAPConnection conn, String dn, Object object)
             throws ELdapException {
         CMS.debug("FileBasedPublisher: publish");
+        FileOutputStream fos = null;
         try {
             if (object instanceof X509Certificate) {
                 X509Certificate cert = (X509Certificate) object;
@@ -296,27 +297,36 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo {
                         sno.toString();
                 if (mDerAttr) {
                     String fileName = name + ".der";
-                    FileOutputStream fos = new FileOutputStream(fileName);
+                    fos = new FileOutputStream(fileName);
                     fos.write(cert.getEncoded());
                     fos.close();
                 }
                 if (mB64Attr) {
                     String fileName = name + ".b64";
-                    FileOutputStream fos = new FileOutputStream(fileName);
-                    ByteArrayOutputStream output = new ByteArrayOutputStream();
-                    Base64OutputStream b64 =
-                            new Base64OutputStream(new PrintStream(new FilterOutputStream(output)));
-                    b64.write(cert.getEncoded());
-                    b64.flush();
-                    (new PrintStream(fos)).print(output.toString("8859_1"));
-                    fos.close();
+                    PrintStream ps = null;
+                    Base64OutputStream b64 = null;
+                    try {
+                        fos = new FileOutputStream(fileName);
+                        ByteArrayOutputStream output = new ByteArrayOutputStream();
+                        b64 = new Base64OutputStream(new PrintStream(new FilterOutputStream(output)));
+                        b64.write(cert.getEncoded());
+                        b64.flush();
+                        ps = new PrintStream(fos);
+                        ps.print(output.toString("8859_1"));
+                    } finally {
+                        if (ps != null) {
+                            ps.close();
+                        }
+                        if (b64 != null) {
+                            b64.close();
+                        }
+                    }
                 }
             } else if (object instanceof X509CRL) {
                 X509CRL crl = (X509CRL) object;
                 String[] namePrefix = getCrlNamePrefix(crl, mTimeStamp.equals("GMT"));
                 String baseName = mDir + File.separator + namePrefix[0];
                 String tempFile = baseName + ".temp";
-                FileOutputStream fos;
                 ZipOutputStream zos = null;
                 byte[] encodedArray = null;
                 File destFile = null;
@@ -371,7 +381,6 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo {
 
                     fos = new FileOutputStream(tempFile);
                     fos.write(Utils.base64encode(encodedArray).getBytes());
-                    fos.close();
                     destName = baseName + ".b64";
                     destFile = new File(destName);
 
@@ -390,6 +399,14 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo {
         } catch (CRLException e) {
             mLogger.log(ILogger.EV_SYSTEM, ILogger.S_OTHER,
                     ILogger.LL_FAILURE, CMS.getLogMessage("PUBLISH_FILE_PUBLISHER_ERROR", e.toString()));
+        } finally {
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
diff --git a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java
index 0fbff688a7608daa16d6c373047f1460bdafc537..df7520d6c227bcb28cdbcf6cbc4ed45e904acbb2 100644
--- a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java
+++ b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java
@@ -514,7 +514,10 @@ public class KeyRepository extends Repository implements IKeyRepository {
                 (KeyRecordList) findKeyRecordsInList(ldapfilter, attrs, serial_upper_bound.toString(10), "serialno",
                         5 * -1);
 
-        int size = recList.getSize();
+        int size = -1;
+        if (recList != null) {
+            size = recList.getSize();
+        }
 
         CMS.debug("KeyRepository: getLastSerialNumberInRange: recList size " + size);
 
diff --git a/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
index 82b515846d68a98edc01730f41fe0de29d695a7f..aa57146685535df9afc573fb499a1c6f56a0fd36 100644
--- a/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/logging/LogSubsystem.java
@@ -200,15 +200,14 @@ public class LogSubsystem implements ILogSubsystem {
 
     public String getLogPluginName(ILogEventListener log) {
         IConfigStore cs = log.getConfigStore();
-
+        if (cs == null) {
+            return "";
+        }
         try {
             return cs.getString("pluginName", "");
         } catch (EBaseException e) {
             e.printStackTrace();
             return "";
-        } catch (NullPointerException e) {
-            e.printStackTrace();
-            return "";
         }
     }
 
diff --git a/base/common/src/com/netscape/cmscore/logging/Logger.java b/base/common/src/com/netscape/cmscore/logging/Logger.java
index b27147f209a18a8bf801783bb38321c0baaa13f8..aec4caec7d089765326604432972c6b84784dc86 100644
--- a/base/common/src/com/netscape/cmscore/logging/Logger.java
+++ b/base/common/src/com/netscape/cmscore/logging/Logger.java
@@ -203,7 +203,9 @@ public class Logger implements ILogger {
      */
     public void log(int evtClass, Properties prop, int source, int level, String msg,
             Object params[]) {
-        mLogQueue.log(create(evtClass, prop, source, level, msg, params, ILogger.L_SINGLELINE));
+        ILogEvent iLEvent = create(evtClass, prop, source, level, msg, params, ILogger.L_SINGLELINE);
+        if (iLEvent != null)
+            mLogQueue.log(iLEvent);
     }
 
     //******************** multiline log *************************
@@ -342,7 +344,9 @@ public class Logger implements ILogger {
      */
     public void log(int evtClass, Properties prop, int source, int level, String msg,
             Object params[], boolean multiline) {
-        mLogQueue.log(create(evtClass, prop, source, level, msg, params, multiline));
+        ILogEvent iLEvent = create(evtClass, prop, source, level, msg, params, multiline);
+        if (iLEvent != null)
+            mLogQueue.log(iLEvent);
     }
 
     //******************** end  multiline log *************************
diff --git a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
index df19bd9fea3aa76f6a3dcb2f34952303bb9727f8..9ab2f85052435b77685c42819d35942d1a8d376e 100644
--- a/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
+++ b/base/java-tools/src/com/netscape/cmstools/PKCS12Export.java
@@ -221,8 +221,9 @@ public class PKCS12Export {
 
         // get password
         String pwd = null;
+        BufferedReader in = null;
         try {
-            BufferedReader in = new BufferedReader(new FileReader(pwdfile));
+            in = new BufferedReader(new FileReader(pwdfile));
             pwd = in.readLine();
             if (pwd == null) {
                 pwd = "";
@@ -230,10 +231,17 @@ public class PKCS12Export {
         } catch (Exception e) {
             debug("Failed to read the keydb password from the file. Exception: " + e.toString());
             System.exit(1);
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
-
         String pk12pwd = null;
-        BufferedReader in = null;
+
         try {
             in = new BufferedReader(new FileReader(pk12pwdfile));
             pk12pwd = in.readLine();
diff --git a/base/kra/src/com/netscape/kra/StorageKeyUnit.java b/base/kra/src/com/netscape/kra/StorageKeyUnit.java
index f968a8c44e12ba8a3850959fb0d58890eb683e6e..6ef3d7d16dc7af876fa0913a7d939c2966656acb 100644
--- a/base/kra/src/com/netscape/kra/StorageKeyUnit.java
+++ b/base/kra/src/com/netscape/kra/StorageKeyUnit.java
@@ -216,7 +216,7 @@ public class StorageKeyUnit extends EncryptionUnit implements
 
             // read certificate from file
             byte certData[] = null;
-
+            FileInputStream fi = null;
             try {
                 if (mKeySplitting) {
                     File certFile = new File(
@@ -224,11 +224,9 @@ public class StorageKeyUnit extends EncryptionUnit implements
 
                     certData = new byte[
                             (Long.valueOf(certFile.length())).intValue()];
-                    FileInputStream fi = new FileInputStream(certFile);
+                    fi = new FileInputStream(certFile);
 
                     fi.read(certData);
-                    fi.close();
-
                     // pick up cert by nickName
                     mCert = mManager.findCertByNickname(
                             config.getString(PROP_NICKNAME));
@@ -257,11 +255,18 @@ public class StorageKeyUnit extends EncryptionUnit implements
                             CMS.getLogMessage("CMSCORE_KRA_STORAGE_IMPORT_CERT", e.toString()));
                     throw new EBaseException(CMS.getUserMessage("CMS_BASE_CERT_ERROR", ex.toString()));
                 }
+            } finally {
+                if (fi != null) {
+                    try {
+                        fi.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
             }
 
             if (mKeySplitting) {
                 // read private key from the file
-                FileInputStream fi = null;
                 try {
                     File priFile = new File(mConfig.getString(PROP_KEYDB));
 
diff --git a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
index 71e5e8810b7575e6e2a7a1a6093d1cbb322fe675..d78374779abf9a4206d8100cc4d5e1d8d200d415 100644
--- a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
+++ b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
@@ -188,9 +188,8 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
     // posts the given query data
     // returns HTTPResponse
     public HTTPResponse sslConnectClientAuth(String hostname, String portnumber,
-                                String client_cert, String url, String query) {
+                                String client_cert, String url, String query) throws Exception {
 
-        boolean st = true;
         HTTPResponse hr = null;
         PrintStream ps = null;
         SSLSocket socket = null;
@@ -247,8 +246,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
 
         catch (Exception e) {
             System.err.println("Exception: Unable to Send Request:" + e);
-            e.printStackTrace();
-            st = false;
+            throw e;
         } finally {
             if (ps != null) {
                 ps.close();
@@ -281,10 +279,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
                 }
         }
 
-        if (!st)
-            return null;
-        else
-            return hr;
+        return hr;
     }
 
     // performs ssl connect to given host/port
diff --git a/base/util/src/netscape/security/x509/CRLDistributionPoint.java b/base/util/src/netscape/security/x509/CRLDistributionPoint.java
index 2c70bf3e625591a24a5ba20cb879b0802cbb326d..435392de78e4aef2ac61fd1eaed3cdbef71863e2 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPoint.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPoint.java
@@ -253,6 +253,7 @@ public class CRLDistributionPoint implements ASN1Value {
 
     public static void main(String args[]) {
         ByteArrayOutputStream bos = null;
+        FileOutputStream fos = null;
         try {
             if (args.length != 1) {
                 System.out.println("Usage: CRLDistributionPoint <outfile>");
@@ -298,7 +299,8 @@ public class CRLDistributionPoint implements ASN1Value {
             cdps.encode(bos);
 
             byte[] encoded = bos.toByteArray();
-            (new FileOutputStream(args[0])).write(encoded);
+            fos = new FileOutputStream(args[0]);
+            fos.write(encoded);
 
             SEQUENCE.OF_Template seqt = new SEQUENCE.OF_Template(getTemplate());
 
@@ -345,6 +347,13 @@ public class CRLDistributionPoint implements ASN1Value {
                     e.printStackTrace();
                 }
             }
+            if (fos != null) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
         }
     }
 
-- 
1.7.10.2

