>From 5cf390179b90a958f943c5f1f67800d4856f6a3a Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Thu, 21 Jun 2012 09:44:15 -0400
Subject: [PATCH] Fixes for Forward Null Cases in Coverity for DogTag10

---
 base/ca/src/com/netscape/ca/CMSCRLExtensions.java  |    2 +-
 .../src/com/netscape/cmscore/apps/CMSEngine.java   |   37 +++++++++---------
 .../com/netscape/cmscore/cert/CertificatePair.java |    7 ++++
 .../com/netscape/cmscore/dbs/BigIntegerMapper.java |    3 ++
 .../src/com/netscape/cmscore/dbs/Repository.java   |    3 +-
 .../netscape/cmscore/security/JssSubsystem.java    |   12 +++++-
 .../src/com/netscape/cmstools/CMCRequest.java      |    3 ++
 .../src/com/netscape/cmstools/DRMTool.java         |    6 +--
 .../com/netscape/pkisilent/common/ComCrypto.java   |    2 +
 .../com/netscape/pkisilent/common/Con2Agent.java   |   10 +++--
 .../com/netscape/pkisilent/http/HTTPClient.java    |    1 +
 .../security/extensions/GenericASN1Extension.java  |   40 ++++++++++----------
 .../src/netscape/security/x509/X509CRLImpl.java    |    2 +-
 13 files changed, 77 insertions(+), 51 deletions(-)

diff --git a/base/ca/src/com/netscape/ca/CMSCRLExtensions.java b/base/ca/src/com/netscape/ca/CMSCRLExtensions.java
index 11f810af2ae221b3695575fc68150b1e1c73985f..0c3fb7906440c69b9543559d551ca3a86a4f9958 100644
--- a/base/ca/src/com/netscape/ca/CMSCRLExtensions.java
+++ b/base/ca/src/com/netscape/ca/CMSCRLExtensions.java
@@ -658,8 +658,8 @@ public class CMSCRLExtensions implements ICMSCRLExtensions {
 
                 //If the CRLCACertsOnly prop is true change it to false to sync.
                 if (value.equals(Constants.FALSE) && (issuingDistPointExtEnabled == true)) {
-                    crlIssuingPointPairs.put(Constants.PR_CA_CERTS_ONLY, Constants.FALSE);
                     if (ip != null) {
+                        crlIssuingPointPairs.put(Constants.PR_CA_CERTS_ONLY, Constants.FALSE);
                         ip.updateConfig(crlIssuingPointPairs);
                         newValue = Constants.FALSE;
                         modifiedCRLConfig = true;
diff --git a/base/common/src/com/netscape/cmscore/apps/CMSEngine.java b/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
index 85507391633665ca13e933ac08e77f9bbee181aa..b534d4a7100668841f01cfb39336eed195687e66 100644
--- a/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
+++ b/base/common/src/com/netscape/cmscore/apps/CMSEngine.java
@@ -314,6 +314,7 @@ public class CMSEngine implements ICMSEngine {
                     } catch (Exception e) {
                         CMS.debug("CMSEngine: getPasswordStore(): password store initialization failure:"
                                 + e.toString());
+                        throw e;
                     }
                 }
             } else {
@@ -1877,32 +1878,32 @@ public class CMSEngine implements ICMSEngine {
                             checkRevReq.setExtData(IRequest.ISSUED_CERTS, agentCerts);
 
                             queue.processRequest(checkRevReq);
-                        } catch (EBaseException e) {
-                            log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AGENT_PROCESS_CHECKING"));
-                        }
 
-                        RequestStatus status = checkRevReq.getRequestStatus();
+                            RequestStatus status = checkRevReq.getRequestStatus();
 
-                        if (status == RequestStatus.COMPLETE) {
-                            Enumeration<String> enum1 = checkRevReq.getExtDataKeys();
+                            if (status == RequestStatus.COMPLETE) {
+                                Enumeration<String> enum1 = checkRevReq.getExtDataKeys();
 
-                            while (enum1.hasMoreElements()) {
-                                String name = enum1.nextElement();
+                                while (enum1.hasMoreElements()) {
+                                    String name = enum1.nextElement();
 
-                                if (name.equals(IRequest.REVOKED_CERTS)) {
-                                    revoked = true;
+                                    if (name.equals(IRequest.REVOKED_CERTS)) {
+                                        revoked = true;
+                                        if (mVCList != null)
+                                            mVCList.update(cert, VerifiedCert.REVOKED);
+                                    }
+                                }
+                                if (revoked == false) {
                                     if (mVCList != null)
-                                        mVCList.update(cert, VerifiedCert.REVOKED);
+                                        mVCList.update(cert, VerifiedCert.NOT_REVOKED);
                                 }
-                            }
-                            if (revoked == false) {
+
+                            } else {
                                 if (mVCList != null)
-                                    mVCList.update(cert, VerifiedCert.NOT_REVOKED);
+                                    mVCList.update(cert, VerifiedCert.CHECKED);
                             }
-
-                        } else {
-                            if (mVCList != null)
-                                mVCList.update(cert, VerifiedCert.CHECKED);
+                        } catch (EBaseException e) {
+                            log(ILogger.LL_FAILURE, CMS.getLogMessage("CMSCORE_AUTH_AGENT_PROCESS_CHECKING"));
                         }
                     }
                 }
diff --git a/base/common/src/com/netscape/cmscore/cert/CertificatePair.java b/base/common/src/com/netscape/cmscore/cert/CertificatePair.java
index 306b9d3bd8101c6e4b9db7bdc432621e6acdc2f0..779e0dafc0b9be0a8a021b2466b788dc6f0776ee 100644
--- a/base/common/src/com/netscape/cmscore/cert/CertificatePair.java
+++ b/base/common/src/com/netscape/cmscore/cert/CertificatePair.java
@@ -207,6 +207,13 @@ public class CertificatePair implements ASN1Value {
      */
     public boolean byteArraysAreEqual(byte[] a, byte[] b) {
         debug("in byteArraysAreEqual()");
+
+        if (a == null && b == null) {
+            return true;
+        }
+        if (a == null || b == null) {
+            return false;
+        }
         if (a.length != b.length) {
             debug("exiting byteArraysAreEqual(): false");
             return false;
diff --git a/base/common/src/com/netscape/cmscore/dbs/BigIntegerMapper.java b/base/common/src/com/netscape/cmscore/dbs/BigIntegerMapper.java
index 9af436295e442e96d25c66e53feac9decf1a9ff3..d59f081db71800d0ea54d7bf34cb28c43b828394 100644
--- a/base/common/src/com/netscape/cmscore/dbs/BigIntegerMapper.java
+++ b/base/common/src/com/netscape/cmscore/dbs/BigIntegerMapper.java
@@ -101,6 +101,9 @@ public class BigIntegerMapper implements IDBAttrMapper {
     }
 
     public static String BigIntegerToDB(BigInteger i) {
+        if (i == null) {
+            return null;
+        }
         int len = i.toString().length();
         String ret = null;
 
diff --git a/base/common/src/com/netscape/cmscore/dbs/Repository.java b/base/common/src/com/netscape/cmscore/dbs/Repository.java
index 4a438c07c8cb0d039577535257ddfc0fdc92314a..57ac5004d3cb5eb232acbd30c1b073e37c55802a 100644
--- a/base/common/src/com/netscape/cmscore/dbs/Repository.java
+++ b/base/common/src/com/netscape/cmscore/dbs/Repository.java
@@ -393,14 +393,13 @@ public abstract class Repository implements IRepository {
                 CMS.debug("Reached the end of the range.  Attempting to move to next range");
                 mMinSerialNo = mNextMinSerialNo;
                 mMaxSerialNo = mNextMaxSerialNo;
-                mLastSerialNo = mMinSerialNo;
                 mNextMinSerialNo = null;
                 mNextMaxSerialNo = null;
                 if ((mMaxSerialNo == null) || (mMinSerialNo == null)) {
                     throw new EDBException(CMS.getUserMessage("CMS_DBS_LIMIT_REACHED",
                             mLastSerialNo.toString()));
                 }
-
+                mLastSerialNo = mMinSerialNo;
                 // persist the changes
                 mDB.setMinSerialConfig(mRepo, mMinSerialNo.toString());
                 mDB.setMaxSerialConfig(mRepo, mMaxSerialNo.toString());
diff --git a/base/common/src/com/netscape/cmscore/security/JssSubsystem.java b/base/common/src/com/netscape/cmscore/security/JssSubsystem.java
index 12bf8d95a58d5c0392971ef6001d83aea5d8c1d5..9d97fefe9c004d16f6775455d3d1d5dc57c0f455 100644
--- a/base/common/src/com/netscape/cmscore/security/JssSubsystem.java
+++ b/base/common/src/com/netscape/cmscore/security/JssSubsystem.java
@@ -1151,8 +1151,12 @@ public final class JssSubsystem implements ICryptoSubsystem {
         try {
             @SuppressWarnings("unchecked")
             Enumeration<CryptoToken> enums = mCryptoManager.getAllTokens();
-            if (mNicknameMapCertsTable != null)
+            if (mNicknameMapCertsTable != null) {
                 mNicknameMapCertsTable.clear();
+            } else {
+                CMS.debug("mNicknameMapCertsTable is null");
+                throw new EBaseException("mNicknameMapCertsTable is null");
+            }
 
             // a temp hashtable with vectors
             Hashtable<String, Vector<X509Certificate>> vecTable = new Hashtable<String, Vector<X509Certificate>>();
@@ -1291,8 +1295,12 @@ public final class JssSubsystem implements ICryptoSubsystem {
 
         NameValuePairs pairs = getCACerts();
 
-        if (mNicknameMapUserCertsTable != null)
+        if (mNicknameMapUserCertsTable != null) {
             mNicknameMapUserCertsTable.clear();
+        } else {
+            CMS.debug("mNicknameMapCertsTable is null");
+            throw new EBaseException("mNicknameMapCertsTable is null");
+        }
 
         try {
             @SuppressWarnings("unchecked")
diff --git a/base/java-tools/src/com/netscape/cmstools/CMCRequest.java b/base/java-tools/src/com/netscape/cmstools/CMCRequest.java
index f1bc93f68a3fa079a79320186e9ce0ddec9ccd71..15975951799e71df2791858e78cbc49ce4518835 100644
--- a/base/java-tools/src/com/netscape/cmstools/CMCRequest.java
+++ b/base/java-tools/src/com/netscape/cmstools/CMCRequest.java
@@ -519,6 +519,9 @@ public class CMCRequest {
             hmacDigest.update(b);
             finalDigest = hmacDigest.digest();
         } catch (NoSuchAlgorithmException ex) {
+            System.out.println("CMCRequest::addIdentityProofAttr() - "
+                    + "No such algorithm!");
+            return -1;
         }
 
         TaggedAttribute identityProof = new TaggedAttribute(new
diff --git a/base/java-tools/src/com/netscape/cmstools/DRMTool.java b/base/java-tools/src/com/netscape/cmstools/DRMTool.java
index 9fbab5da63fac946cfb5776fe81696d1c7900e9c..8b415e98d3318878086c542d9edb776cac4a8f97 100644
--- a/base/java-tools/src/com/netscape/cmstools/DRMTool.java
+++ b/base/java-tools/src/com/netscape/cmstools/DRMTool.java
@@ -4059,7 +4059,7 @@ public class DRMTool {
         PrintWriter writer = null;
         String input = null;
         String line = null;
-        String previous_line = null;
+        String previous_line = "";
         String output = null;
         String data = null;
         String record_type = null;
@@ -4253,7 +4253,7 @@ public class DRMTool {
                         if (output == null) {
                             return FAILURE;
                         }
-                    } else if (previous_line != null &&
+                    } else if (previous_line.length() > 0 &&
                                previous_line.startsWith(
                                        DRM_LDIF_EXTDATA_AUTH_TOKEN_USER)) {
                         output = output_extdata_auth_token_user(record_type,
@@ -4261,7 +4261,7 @@ public class DRMTool {
                         if (output == null) {
                             return FAILURE;
                         }
-                    } else if (previous_line != null &&
+                    } else if (previous_line.length() > 0 &&
                                previous_line.startsWith(
                                        DRM_LDIF_EXTDATA_AUTH_TOKEN_USER_DN)) {
                         output = output_extdata_auth_token_user_dn(record_type,
diff --git a/base/silent/src/com/netscape/pkisilent/common/ComCrypto.java b/base/silent/src/com/netscape/pkisilent/common/ComCrypto.java
index 9b5cf7b5285d838fc5db4048b67b613e091ba703..07fe8464e0f6575ed51830d2bbf4b8501a2154c6 100644
--- a/base/silent/src/com/netscape/pkisilent/common/ComCrypto.java
+++ b/base/silent/src/com/netscape/pkisilent/common/ComCrypto.java
@@ -242,6 +242,7 @@ public class ComCrypto {
 
             if (manager == null) {
                 System.out.println("Manager object is null");
+                throw new Exception("Manager Object is null");
             }
 
             manager.importCertPackage(tmp.getBytes(), cn);
@@ -271,6 +272,7 @@ public class ComCrypto {
 
             if (manager == null) {
                 System.out.println("Manager object is null");
+                throw new Exception("Manager object is null");
             }
 
             manager.importCACertPackage(tmp.getBytes());
diff --git a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
index 16fc7c77ab105ed6096fc886cace3f8661f461b5..3038a1679487504ff36c0649b3f119f7a4deefd7 100644
--- a/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
+++ b/base/silent/src/com/netscape/pkisilent/common/Con2Agent.java
@@ -238,10 +238,12 @@ public class Con2Agent implements SSLClientCertificateSelectionCallback,
                     e.printStackTrace();
                 }
             }
-            if (socket.isClosed()) {
-                System.out.println("Con2Agent.java : Socket is Closed");
-            } else {
-                System.out.println("Con2Agent.java : Socket not Closed");
+            if (socket != null) {
+                if (socket.isClosed()) {
+                    System.out.println("Con2Agent.java : Socket is Closed");
+                } else {
+                    System.out.println("Con2Agent.java : Socket not Closed");
+                }
             }
         }
         return true;
diff --git a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
index 71e5e8810b7575e6e2a7a1a6093d1cbb322fe675..c98fe219335a2c05ddc9e7fadea4aa71073195ae 100644
--- a/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
+++ b/base/silent/src/com/netscape/pkisilent/http/HTTPClient.java
@@ -658,6 +658,7 @@ public class HTTPClient implements SSLCertificateApprovalCallback {
             // This should never happen -- an empty response
             System.out.println("Unable to parse response header -- empty " +
                               "header");
+            throw new Exception("Unable to create response. Empty header.");
         }
 
         // If the status code was 100 (continue), then it was an intermediate header
diff --git a/base/util/src/netscape/security/extensions/GenericASN1Extension.java b/base/util/src/netscape/security/extensions/GenericASN1Extension.java
index b51846498cad1426f24c07e3f9202e9418b22842..dfd34de2dbc42a90d613e6cd5d53d7bf63e32924 100644
--- a/base/util/src/netscape/security/extensions/GenericASN1Extension.java
+++ b/base/util/src/netscape/security/extensions/GenericASN1Extension.java
@@ -410,7 +410,7 @@ public class GenericASN1Extension extends Extension
     }
 
     private String getFromFile(String fname) throws IOException {
-        String s = null;
+        String s = "";
         byte[] buff = null;
         int i = 0;
         int j = 0;
@@ -418,8 +418,9 @@ public class GenericASN1Extension extends Extension
             throw new IOException("File name is not provided.");
         }
 
-        FileInputStream fis = new FileInputStream(fname);
+        FileInputStream fis = null;
         try {
+            fis = new FileInputStream(fname);
             int n = 0;
             while ((n = fis.available()) > 0) {
                 buff = new byte[n];
@@ -428,28 +429,27 @@ public class GenericASN1Extension extends Extension
                     break;
                 s = new String(buff);
             }
-
-            for (i = 0, j = 0; j < s.length(); j++) {
-                int ch = s.charAt(j);
-                if (ch == 10 || ch == 13 || ch == 9)
-                    continue;
-                i++;
-            }
-            buff = new byte[i];
-            for (i = 0, j = 0; j < s.length(); j++) {
-                int ch = s.charAt(j);
-                if (ch == 10 || ch == 13 || ch == 9)
-                    continue;
-                buff[i++] = (byte) ch;
-            }
-
-            s = new String(buff);
-
-            return s;
         } finally {
             if (fis != null) {
                 fis.close();
             }
         }
+        for (i = 0, j = 0; j < s.length(); j++) {
+            int ch = s.charAt(j);
+            if (ch == 10 || ch == 13 || ch == 9)
+                continue;
+            i++;
+        }
+        buff = new byte[i];
+        for (i = 0, j = 0; j < s.length(); j++) {
+            int ch = s.charAt(j);
+            if (ch == 10 || ch == 13 || ch == 9)
+                continue;
+            buff[i++] = (byte) ch;
+        }
+        s = new String(buff);
+
+        return s;
     }
+
 }
diff --git a/base/util/src/netscape/security/x509/X509CRLImpl.java b/base/util/src/netscape/security/x509/X509CRLImpl.java
index 6c84084c55ba39cb244f95cf1ccac391ff35afb6..f830ef24ad63a579f94ac0b1bb5830ab7873b80a 100755
--- a/base/util/src/netscape/security/x509/X509CRLImpl.java
+++ b/base/util/src/netscape/security/x509/X509CRLImpl.java
@@ -404,7 +404,7 @@ public class X509CRLImpl extends X509CRL {
         Signature sigVerf = null;
 
         String sigAlg = sigAlgId.getName();
-        if (sigProvider.equals("Mozilla-JSS")) {
+        if ("Mozilla-JSS".equals(sigProvider)) {
             if (sigAlg.equals("MD5withRSA")) {
                 sigAlg = "MD5/RSA";
             } else if (sigAlg.equals("MD2withRSA")) {
-- 
1.7.10.2

