From de3aaef15e9b1f192344019f52d6c80860055b5e Mon Sep 17 00:00:00 2001 From: Abhishek Koneru Date: Mon, 18 Jun 2012 14:08:21 -0400 Subject: Fixes for Resource Leaks shown in Coverity for DogTag 10 --- .../com/netscape/pkisilent/common/UserEnroll.java | 59 ++++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-) (limited to 'base/silent/src/com/netscape/pkisilent/common/UserEnroll.java') diff --git a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java index 0e31eeb57..2f6a03924 100644 --- a/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java +++ b/base/silent/src/com/netscape/pkisilent/common/UserEnroll.java @@ -20,6 +20,7 @@ package com.netscape.pkisilent.common; import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; @@ -218,7 +219,11 @@ public class UserEnroll extends TestClient { private boolean Send() { boolean st = false; - + SSLSocket socket = null; + OutputStream rawos = null; + BufferedOutputStream os = null; + PrintStream ps = null; + BufferedReader stdin = null; try { if (debug) { @@ -232,12 +237,12 @@ public class UserEnroll extends TestClient { GregorianCalendar begin = new GregorianCalendar(); // SSLSocket socket = new SSLSocket(host,port); - SSLSocket socket = new SSLSocket(host, port, null, 0, this, null); + socket = new SSLSocket(host, port, null, 0, this, null); socket.setUseClientMode(true); - OutputStream rawos = socket.getOutputStream(); - BufferedOutputStream os = new BufferedOutputStream(rawos); - PrintStream ps = new PrintStream(os); + rawos = socket.getOutputStream(); + os = new BufferedOutputStream(rawos); + ps = new PrintStream(os); ps.println("POST /enrollment HTTP/1.0"); ps.println("Connection: Keep-Alive"); @@ -248,7 +253,7 @@ public class UserEnroll extends TestClient { ps.println("\r"); ps.flush(); os.flush(); - BufferedReader stdin = new BufferedReader( + stdin = new BufferedReader( new InputStreamReader(socket.getInputStream())); if (debug) { @@ -274,16 +279,6 @@ public class UserEnroll extends TestClient { } } - stdin.close(); - socket.close(); - os.close(); - rawos.close(); - ps.close(); - os = null; - rawos = null; - stdin = null; - ps = null; - line = null; GregorianCalendar end = new GregorianCalendar(); long diff = calculateElapsedTime(begin, end); @@ -292,6 +287,38 @@ public class UserEnroll extends TestClient { } catch (Exception e) { System.err.println("some exception: in Send routine" + e); return false; + } finally { + if (ps != null) { + ps.close(); + } + if (stdin != null) { + try { + stdin.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (socket != null) { + try { + socket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (os != null) { + try { + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (rawos != null) { + try { + rawos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } } return st; -- cgit