diff options
author | Anders Kaseorg <andersk@mit.edu> | 2013-01-26 14:17:57 -0500 |
---|---|---|
committer | Keith Winstein <keithw@mit.edu> | 2013-03-10 15:45:47 -0400 |
commit | d6ff754a9a68df157fd668eec58af3b67a2169ea (patch) | |
tree | 83c0daefb969d261acdafabaece10231fce37a07 | |
parent | 2686b7c634c7e060fe099dd127ddfd9a0790ad6e (diff) |
get_SSH_IP: Don’t leak SSH_writable memory
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
-rw-r--r-- | src/frontend/mosh-server.cc | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index d1d77b6..116d4b0 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -35,6 +35,7 @@ #include <errno.h> #include <locale.h> #include <string.h> +#include <sstream> #include <termios.h> #include <unistd.h> #include <fcntl.h> @@ -132,13 +133,10 @@ string get_SSH_IP( void ) fprintf( stderr, "Warning: SSH_CONNECTION not found; binding to any interface.\n" ); return string( "0.0.0.0" ); } - char *SSH_writable = strdup( SSH_CONNECTION ); - fatal_assert( SSH_writable ); - - strtok( SSH_writable, " " ); - strtok( NULL, " " ); - const char *local_interface_IP = strtok( NULL, " " ); - if ( !local_interface_IP ) { + istringstream ss( SSH_CONNECTION ); + string dummy, local_interface_IP; + ss >> dummy >> dummy >> local_interface_IP; + if ( !ss ) { fprintf( stderr, "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n" ); return string( "0.0.0.0" ); } @@ -146,12 +144,12 @@ string get_SSH_IP( void ) /* Strip IPv6 prefix. */ const char IPv6_prefix[] = "::ffff:"; - if ( ( strlen( local_interface_IP ) > strlen( IPv6_prefix ) ) - && ( 0 == strncasecmp( local_interface_IP, IPv6_prefix, strlen( IPv6_prefix ) ) ) ) { - return string( local_interface_IP + strlen( IPv6_prefix ) ); + if ( ( local_interface_IP.length() > strlen( IPv6_prefix ) ) + && ( 0 == strncasecmp( local_interface_IP.c_str(), IPv6_prefix, strlen( IPv6_prefix ) ) ) ) { + return local_interface_IP.substr( strlen( IPv6_prefix ) ); } - return string( local_interface_IP ); + return local_interface_IP; } int main( int argc, char *argv[] ) |