From 0f3451befbc14bd6ec29d9e1e3845f970f288653 Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Tue, 26 Jun 2012 15:37:15 -0400 Subject: 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 | 69 +++++++++++++++------- .../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, 102 insertions(+), 53 deletions(-) (limited to 'base') diff --git a/base/ca/src/com/netscape/ca/CAService.java b/base/ca/src/com/netscape/ca/CAService.java index 12011ced4..19778505e 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 feecec6a8..f66192cf5 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(e.toString()); } } @@ -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(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 59effbe81..6a1d528ac 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"); + try { if (object instanceof X509Certificate) { X509Certificate cert = (X509Certificate) object; @@ -295,28 +296,45 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { File.separator + "cert-" + sno.toString(); if (mDerAttr) { - String fileName = name + ".der"; - FileOutputStream fos = new FileOutputStream(fileName); - fos.write(cert.getEncoded()); - fos.close(); + FileOutputStream fos = null; + try { + String fileName = name + ".der"; + fos = new FileOutputStream(fileName); + fos.write(cert.getEncoded()); + } finally { + if (fos != null) + 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; + FileOutputStream fos = 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(); + } + if (fos != null) + fos.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; @@ -324,10 +342,15 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { File renameFile = null; if (mDerAttr) { - fos = new FileOutputStream(tempFile); - encodedArray = crl.getEncoded(); - fos.write(encodedArray); - fos.close(); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(tempFile); + encodedArray = crl.getEncoded(); + fos.write(encodedArray); + } finally { + if (fos != null) + fos.close(); + } if (mZipCRL) { try { zos = new ZipOutputStream(new FileOutputStream(baseName + ".zip")); @@ -368,10 +391,14 @@ public class FileBasedPublisher implements ILdapPublisher, IExtendedPluginInfo { if (mB64Attr == true) { if (encodedArray == null) encodedArray = crl.getEncoded(); - - fos = new FileOutputStream(tempFile); - fos.write(Utils.base64encode(encodedArray).getBytes()); - fos.close(); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(tempFile); + fos.write(Utils.base64encode(encodedArray).getBytes()); + } finally { + if (fos != null) + fos.close(); + } destName = baseName + ".b64"; destFile = new File(destName); diff --git a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java index fb3b9b24a..556cab9f5 100644 --- a/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java +++ b/base/common/src/com/netscape/cmscore/dbs/KeyRepository.java @@ -515,7 +515,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 82b515846..aa5714668 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 b27147f20..aec4caec7 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 df19bd9fe..9ab2f8505 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 f968a8c44..6ef3d7d16 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 49d9e1846..4ab7c606d 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 2c70bf3e6..435392de7 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 "); @@ -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(); + } + } } } -- cgit