This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new 427851d  [VFS-707] [SFTP] SftpFileSystem.executeCommand(String, 
StringBuilder) can leak ChannelExec objects.
427851d is described below

commit 427851de6c5bdab1cbfdfabde6e89f256a459dc9
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Sat Apr 27 13:47:11 2019 -0400

    [VFS-707] [SFTP] SftpFileSystem.executeCommand(String, StringBuilder)
    can leak ChannelExec objects.
---
 .../commons/vfs2/provider/sftp/SftpFileSystem.java | 40 ++++++++++++----------
 src/changes/changes.xml                            |  3 ++
 2 files changed, 24 insertions(+), 19 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystem.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystem.java
index fb3d40d..c7c385a 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystem.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/sftp/SftpFileSystem.java
@@ -268,30 +268,32 @@ public class SftpFileSystem extends AbstractFileSystem {
     private int executeCommand(final String command, final StringBuilder 
output) throws JSchException, IOException {
         ensureSession();
         final ChannelExec channel = (ChannelExec) session.openChannel("exec");
+        try {
+            channel.setCommand(command);
+            channel.setInputStream(null);
+            try (final InputStreamReader stream = new 
InputStreamReader(channel.getInputStream())) {
+                channel.setErrStream(System.err, true);
+                channel.connect(connectTimeoutMillis);
 
-        channel.setCommand(command);
-        channel.setInputStream(null);
-        try (final InputStreamReader stream = new 
InputStreamReader(channel.getInputStream())) {
-            channel.setErrStream(System.err, true);
-            channel.connect(connectTimeoutMillis);
-
-            // Read the stream
-            final char[] buffer = new char[EXEC_BUFFER_SIZE];
-            int read;
-            while ((read = stream.read(buffer, 0, buffer.length)) >= 0) {
-                output.append(buffer, 0, read);
+                // Read the stream
+                final char[] buffer = new char[EXEC_BUFFER_SIZE];
+                int read;
+                while ((read = stream.read(buffer, 0, buffer.length)) >= 0) {
+                    output.append(buffer, 0, read);
+                }
             }
-        }
 
-        // Wait until the command finishes (should not be long since we read 
the output stream)
-        while (!channel.isClosed()) {
-            try {
-                Thread.sleep(SLEEP_MILLIS);
-            } catch (final Exception ee) {
-                // TODO: swallow exception, really?
+            // Wait until the command finishes (should not be long since we 
read the output stream)
+            while (!channel.isClosed()) {
+                try {
+                    Thread.sleep(SLEEP_MILLIS);
+                } catch (final Exception ee) {
+                    // TODO: swallow exception, really?
+                }
             }
+        } finally {
+            channel.disconnect();
         }
-        channel.disconnect();
         return channel.getExitStatus();
     }
 }
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5d9be6f..650b0ff 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -92,6 +92,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="VFS-707" dev="ggregory" type="add" due-to="Gary Gregory">
         Update Apache HttpClient from 4.5.7 to 4.5.8.
       </action>
+      <action issue="VFS-707" dev="ggregory" type="fix" due-to="Gary Gregory">
+        [SFTP] SftpFileSystem.executeCommand(String, StringBuilder) can leak 
ChannelExec objects.
+      </action>
     </release>
     <release version="2.3" date="2019-02-01" description="New features and bug 
fix release.">
       <action issue="VFS-645" dev="ggregory" type="fix">

Reply via email to