From 661f6eb9f2ed82f32f0e968c7339d9f1820fec2c Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Mon, 30 Jul 2012 08:33:28 +0000
Subject: [PATCH] Task #2669: Fixes ever-expanding input buffer for
 SSHconnection

---
 RTCP/IONProc/src/SSH.cc | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/RTCP/IONProc/src/SSH.cc b/RTCP/IONProc/src/SSH.cc
index dbbc16a7ff5..dc2d734f785 100644
--- a/RTCP/IONProc/src/SSH.cc
+++ b/RTCP/IONProc/src/SSH.cc
@@ -35,6 +35,8 @@
 #include <string.h>
 #include <errno.h>
 #include <vector>
+#include <string>
+#include <sstream>
 
 #ifdef HAVE_LIBSSH2
 #include <Scheduling.h>
@@ -217,7 +219,6 @@ void SSHconnection::commThread()
 
   LOG_DEBUG_STR( itsLogPrefix << "Connected" );
 
-  stringstream buffer;
   int rc;
   int exitcode;
   char *exitsignal=(char *)"none";
@@ -247,16 +248,24 @@ void SSHconnection::commThread()
 
   LOG_DEBUG_STR( itsLogPrefix << "Remote command started, waiting for output" );
 
+  // raw input buffer
+  char data[0x1000];
+
+  // the current line (or line remnant)
+  string line("");
+
   /* Session I/O */
   for( ;; )
   {
     /* loop until we block */
     do {
-      char data[0x4000];
-
       rc = libssh2_channel_read( channel, data, sizeof data );
       if( rc > 0 )
       {
+        // create a buffer for line + data
+        stringstream buffer;
+
+        buffer << line;
         buffer.write( data, rc );
 
         /* extract and log lines */
@@ -264,16 +273,15 @@ void SSHconnection::commThread()
         {
           Cancellation::point();
 
-          buffer.getline( data, sizeof data );
+          std::getline( buffer, line );
 
-          if (buffer.fail()) {
-            // no line found
-            buffer.clear();
+          if (!buffer.good()) {
+            // 'line' now holds the remnant
             break;
           }
 
           // TODO: Use logger somehow (we'd duplicate the prefix if we just use LOG_* macros..)
-          cout << data << endl;
+          cout << line << endl;
         }
       } else {
         if( rc < 0 && rc != LIBSSH2_ERROR_EAGAIN ) {
-- 
GitLab