summaryrefslogtreecommitdiffstats
path: root/base/util/src
diff options
context:
space:
mode:
authorAbhishek Koneru <akoneru@redhat.com>2012-06-20 13:56:57 -0400
committerEndi Sukma Dewata <edewata@redhat.com>2012-06-25 13:38:26 -0500
commit8a8436f73d4c4edd10b43313b724f32ccb7b957e (patch)
tree657c8f9accd34d286acf0569f768c72684a12b75 /base/util/src
parentde3aaef15e9b1f192344019f52d6c80860055b5e (diff)
downloadpki-8a8436f73d4c4edd10b43313b724f32ccb7b957e.tar.gz
pki-8a8436f73d4c4edd10b43313b724f32ccb7b957e.tar.xz
pki-8a8436f73d4c4edd10b43313b724f32ccb7b957e.zip
Fixes for Coverity issues of type Resource Leaks - Remaining
Diffstat (limited to 'base/util/src')
-rw-r--r--base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java25
-rw-r--r--base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java12
-rw-r--r--base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java24
-rw-r--r--base/util/src/com/netscape/cmsutil/util/Utils.java27
-rw-r--r--base/util/src/netscape/security/extensions/GenericASN1Extension.java52
-rw-r--r--base/util/src/netscape/security/extensions/PresenceServerExtension.java84
-rw-r--r--base/util/src/netscape/security/x509/CRLDistributionPoint.java11
-rw-r--r--base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java14
-rw-r--r--base/util/src/netscape/security/x509/FreshestCRLExtension.java14
-rw-r--r--base/util/src/netscape/security/x509/IssuingDistributionPoint.java12
-rw-r--r--base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java13
-rw-r--r--base/util/src/netscape/security/x509/OIDMap.java23
12 files changed, 216 insertions, 95 deletions
diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java
index adad6ee37..990d0c156 100644
--- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java
+++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordFile.java
@@ -37,10 +37,15 @@ public class PlainPasswordFile implements IPasswordStore {
mPwdStore = new Properties();
// initialize mPwdStore
mPwdPath = pwdPath;
-
- FileInputStream file = new FileInputStream(mPwdPath);
- mPwdStore.load(file);
- file.close();
+ FileInputStream file = null;
+ try {
+ file = new FileInputStream(mPwdPath);
+ mPwdStore.load(file);
+ } finally {
+ if (file != null) {
+ file.close();
+ }
+ }
}
public String getPassword(String tag) {
@@ -63,8 +68,14 @@ public class PlainPasswordFile implements IPasswordStore {
public void commit()
throws IOException, ClassCastException, NullPointerException {
- FileOutputStream file = new FileOutputStream(mPwdPath);
- mPwdStore.store(file, PASSWORD_WRITER_HEADER);
- file.close();
+ FileOutputStream file = null;
+ try {
+ file = new FileOutputStream(mPwdPath);
+ mPwdStore.store(file, PASSWORD_WRITER_HEADER);
+ } finally {
+ if (file != null) {
+ file.close();
+ }
+ }
}
}
diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java
index 3a75097ec..d4de53bca 100644
--- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java
+++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordReader.java
@@ -37,9 +37,15 @@ public class PlainPasswordReader implements IPasswordReader {
mPwdPath = pwdPath;
mPwdStore = new Properties();
- FileInputStream file = new FileInputStream(mPwdPath);
- mPwdStore.load(file);
- file.close();
+ FileInputStream file = null;
+ try {
+ file = new FileInputStream(mPwdPath);
+ mPwdStore.load(file);
+ } finally {
+ if (file != null) {
+ file.close();
+ }
+ }
}
public String getPassword(String tag) {
diff --git a/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java b/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java
index 3ceac4bd6..0965b7b31 100644
--- a/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java
+++ b/base/util/src/com/netscape/cmsutil/password/PlainPasswordWriter.java
@@ -37,9 +37,15 @@ public class PlainPasswordWriter implements IPasswordWriter {
mPwdPath = pwdPath;
mPwdStore = new Properties();
- FileInputStream file = new FileInputStream(mPwdPath);
- mPwdStore.load(file);
- file.close();
+ FileInputStream file = null;
+ try {
+ file = new FileInputStream(mPwdPath);
+ mPwdStore.load(file);
+ } finally {
+ if (file != null) {
+ file.close();
+ }
+ }
}
public Object putPassword(String tag, String password) {
@@ -48,9 +54,13 @@ public class PlainPasswordWriter implements IPasswordWriter {
public void commit()
throws IOException, ClassCastException, NullPointerException {
- FileOutputStream file = new FileOutputStream(mPwdPath);
- mPwdStore.store(file, PASSWORD_WRITER_HEADER);
- file.close();
+ FileOutputStream file = null;
+ try {
+ file = new FileOutputStream(mPwdPath);
+ mPwdStore.store(file, PASSWORD_WRITER_HEADER);
+ } finally {
+ if (file != null)
+ file.close();
+ }
}
-
}
diff --git a/base/util/src/com/netscape/cmsutil/util/Utils.java b/base/util/src/com/netscape/cmsutil/util/Utils.java
index 75e63f2b0..c8d6b438d 100644
--- a/base/util/src/com/netscape/cmsutil/util/Utils.java
+++ b/base/util/src/com/netscape/cmsutil/util/Utils.java
@@ -17,8 +17,6 @@
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmsutil.util;
-import org.apache.commons.codec.binary.Base64;
-
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
@@ -36,6 +34,8 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
+import org.apache.commons.codec.binary.Base64;
+
public class Utils {
/**
* Checks if this is NT.
@@ -152,10 +152,12 @@ public class Utils {
InetAddress.getByName(hostname);
}
- public static void copy(String orig, String dest) {
+ public static void copy(String orig, String dest) throws Exception {
+ BufferedReader in = null;
+ PrintWriter out = null;
try {
- BufferedReader in = new BufferedReader(new FileReader(orig));
- PrintWriter out = new PrintWriter(
+ in = new BufferedReader(new FileReader(orig));
+ out = new PrintWriter(
new BufferedWriter(new FileWriter(dest)));
String line = "";
while (in.ready()) {
@@ -163,9 +165,20 @@ public class Utils {
if (line != null)
out.println(line);
}
- in.close();
- out.close();
} catch (Exception ee) {
+ ee.printStackTrace();
+ throw ee;
+ } finally {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (out != null) {
+ out.close();
+ }
}
}
diff --git a/base/util/src/netscape/security/extensions/GenericASN1Extension.java b/base/util/src/netscape/security/extensions/GenericASN1Extension.java
index 78d294573..b51846498 100644
--- a/base/util/src/netscape/security/extensions/GenericASN1Extension.java
+++ b/base/util/src/netscape/security/extensions/GenericASN1Extension.java
@@ -419,31 +419,37 @@ public class GenericASN1Extension extends Extension
}
FileInputStream fis = new FileInputStream(fname);
- int n = 0;
- while ((n = fis.available()) > 0) {
- buff = new byte[n];
- int result = fis.read(buff);
- if (result == -1)
- break;
- s = new String(buff);
- }
+ try {
+ int n = 0;
+ while ((n = fis.available()) > 0) {
+ buff = new byte[n];
+ int result = fis.read(buff);
+ if (result == -1)
+ 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;
- }
+ 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);
+ s = new String(buff);
- return s;
+ return s;
+ } finally {
+ if (fis != null) {
+ fis.close();
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/extensions/PresenceServerExtension.java b/base/util/src/netscape/security/extensions/PresenceServerExtension.java
index 276c91547..5ee802468 100644
--- a/base/util/src/netscape/security/extensions/PresenceServerExtension.java
+++ b/base/util/src/netscape/security/extensions/PresenceServerExtension.java
@@ -259,7 +259,7 @@ public class PresenceServerExtension extends Extension implements CertAttrSet {
public void setOID(String oid) {
}
- public static void main(String args[]) throws Exception {
+ public static void main(String args[]) {
/*
0 30 115: SEQUENCE {
2 06 9: OBJECT IDENTIFIER '2 16 840 1 113730 1 100'
@@ -285,36 +285,56 @@ public class PresenceServerExtension extends Extension implements CertAttrSet {
: }
: }
*/
- boolean critical = false;
- int version = 1;
- String streetAddress = "401E Middlefield Rd.,MV,CA94041";
- String telephoneNumber = "650-111-1111";
- String rfc822Name = "admin@netscape.com";
- String ID = "ps-capitol";
- String hostName = "capitol";
- int portNumber = 80;
- int maxUsers = 10;
- int serviceLevel = 1;
-
- PresenceServerExtension ext = new PresenceServerExtension(
- critical,
- version, streetAddress, telephoneNumber,
- rfc822Name, ID, hostName, portNumber,
- maxUsers, serviceLevel);
-
- // encode
-
- ByteArrayOutputStream dos = new ByteArrayOutputStream();
- ext.encode(dos);
- FileOutputStream fos = new FileOutputStream("pse.der");
- fos.write(dos.toByteArray());
- fos.close();
-
- Extension ext1 = new Extension(new DerValue(dos.toByteArray()));
-
- @SuppressWarnings("unused")
- PresenceServerExtension ext2 = new PresenceServerExtension(
- Boolean.valueOf(false), ext1.getExtensionValue());
-
+ ByteArrayOutputStream dos = null;
+ FileOutputStream fos = null;
+ try {
+ boolean critical = false;
+ int version = 1;
+ String streetAddress = "401E Middlefield Rd.,MV,CA94041";
+ String telephoneNumber = "650-111-1111";
+ String rfc822Name = "admin@netscape.com";
+ String ID = "ps-capitol";
+ String hostName = "capitol";
+ int portNumber = 80;
+ int maxUsers = 10;
+ int serviceLevel = 1;
+
+ PresenceServerExtension ext = new PresenceServerExtension(
+ critical,
+ version, streetAddress, telephoneNumber,
+ rfc822Name, ID, hostName, portNumber,
+ maxUsers, serviceLevel);
+
+ // encode
+
+ dos = new ByteArrayOutputStream();
+ ext.encode(dos);
+ fos = new FileOutputStream("pse.der");
+ fos.write(dos.toByteArray());
+ Extension ext1 = new Extension(new DerValue(dos.toByteArray()));
+
+ @SuppressWarnings("unused")
+ PresenceServerExtension ext2 = new PresenceServerExtension(
+ Boolean.valueOf(false), ext1.getExtensionValue());
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (CertificateException e) {
+ e.printStackTrace();
+ } finally {
+ if (dos != null) {
+ try {
+ dos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (fos != null) {
+ try {
+ fos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/x509/CRLDistributionPoint.java b/base/util/src/netscape/security/x509/CRLDistributionPoint.java
index 442957449..2c70bf3e6 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPoint.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPoint.java
@@ -252,13 +252,14 @@ public class CRLDistributionPoint implements ASN1Value {
}
public static void main(String args[]) {
+ ByteArrayOutputStream bos = null;
try {
if (args.length != 1) {
System.out.println("Usage: CRLDistributionPoint <outfile>");
System.exit(-1);
}
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ bos = new ByteArrayOutputStream();
SEQUENCE cdps = new SEQUENCE();
@@ -336,6 +337,14 @@ public class CRLDistributionPoint implements ASN1Value {
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
index 4180473d4..d8dee03a7 100644
--- a/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
+++ b/base/util/src/netscape/security/x509/CRLDistributionPointsExtension.java
@@ -243,7 +243,7 @@ public class CRLDistributionPointsExtension extends Extension
* Test driver.
*/
public static void main(String args[]) {
-
+ BufferedOutputStream bos = null;
try {
if (args.length != 1) {
@@ -252,7 +252,7 @@ public class CRLDistributionPointsExtension extends Extension
System.exit(-1);
}
- BufferedOutputStream bos = new BufferedOutputStream(
+ bos = new BufferedOutputStream(
new FileOutputStream(args[0]));
// URI only
@@ -291,10 +291,16 @@ public class CRLDistributionPointsExtension extends Extension
crldpExt.setCritical(true);
crldpExt.encode(bos);
- bos.close();
-
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/x509/FreshestCRLExtension.java b/base/util/src/netscape/security/x509/FreshestCRLExtension.java
index f108b4599..222a5e959 100644
--- a/base/util/src/netscape/security/x509/FreshestCRLExtension.java
+++ b/base/util/src/netscape/security/x509/FreshestCRLExtension.java
@@ -248,7 +248,7 @@ public class FreshestCRLExtension extends Extension
* Test driver.
*/
public static void main(String args[]) {
-
+ BufferedOutputStream bos = null;
try {
if (args.length != 1) {
@@ -257,7 +257,7 @@ public class FreshestCRLExtension extends Extension
System.exit(-1);
}
- BufferedOutputStream bos = new BufferedOutputStream(
+ bos = new BufferedOutputStream(
new FileOutputStream(args[0]));
// URI only
@@ -296,10 +296,16 @@ public class FreshestCRLExtension extends Extension
crldpExt.setCritical(true);
crldpExt.encode(bos);
- bos.close();
-
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/x509/IssuingDistributionPoint.java b/base/util/src/netscape/security/x509/IssuingDistributionPoint.java
index 504587bf4..06638f2dd 100644
--- a/base/util/src/netscape/security/x509/IssuingDistributionPoint.java
+++ b/base/util/src/netscape/security/x509/IssuingDistributionPoint.java
@@ -272,6 +272,7 @@ public class IssuingDistributionPoint implements ASN1Value {
}
public static void main(String args[]) {
+ BufferedOutputStream bos = null;
try {
if (args.length != 1) {
@@ -279,7 +280,7 @@ public class IssuingDistributionPoint implements ASN1Value {
System.exit(-1);
}
- BufferedOutputStream bos = new BufferedOutputStream(
+ bos = new BufferedOutputStream(
new FileOutputStream(args[0]));
SEQUENCE idps = new SEQUENCE();
@@ -306,9 +307,16 @@ public class IssuingDistributionPoint implements ASN1Value {
idps.addElement(idp);
idps.encode(bos);
- bos.close();
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
diff --git a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
index 0f8c8835c..b78a891c2 100644
--- a/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
+++ b/base/util/src/netscape/security/x509/IssuingDistributionPointExtension.java
@@ -361,6 +361,7 @@ public class IssuingDistributionPointExtension extends Extension
*/
public static void main(String args[]) {
+ BufferedOutputStream bos = null;
try {
if (args.length != 1) {
@@ -369,7 +370,7 @@ public class IssuingDistributionPointExtension extends Extension
System.exit(-1);
}
- BufferedOutputStream bos = new BufferedOutputStream(
+ bos = new BufferedOutputStream(
new FileOutputStream(args[0]));
// URI only
@@ -410,10 +411,18 @@ public class IssuingDistributionPointExtension extends Extension
idpExt.setCritical(false);
idpExt.encode(bos);
- bos.close();
+
} catch (Exception e) {
e.printStackTrace();
+ } finally {
+ if (bos != null) {
+ try {
+ bos.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
}
diff --git a/base/util/src/netscape/security/x509/OIDMap.java b/base/util/src/netscape/security/x509/OIDMap.java
index 17573a019..4cebcf2c8 100644
--- a/base/util/src/netscape/security/x509/OIDMap.java
+++ b/base/util/src/netscape/security/x509/OIDMap.java
@@ -165,12 +165,20 @@ public class OIDMap {
if (!namesMap.exists()) {
loadNamesDefault(props);
} else {
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(namesMap);
+ fis = new FileInputStream(namesMap);
props.load(fis);
- fis.close();
} catch (IOException e) {
loadNamesDefault(props);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}
@@ -193,11 +201,20 @@ public class OIDMap {
if (!classMap.exists()) {
loadClassDefault(props);
} else {
+ FileInputStream fis = null;
try {
- FileInputStream fis = new FileInputStream(classMap);
+ fis = new FileInputStream(classMap);
props.load(fis);
} catch (IOException e) {
loadClassDefault(props);
+ } finally {
+ if (fis != null) {
+ try {
+ fis.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
}