get_SSH_IP: Don’t leak SSH_writable memory

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg
2013-01-26 14:17:57 -05:00
committed by Keith Winstein
parent 2686b7c634
commit d6ff754a9a
+9 -11
View File
@@ -35,6 +35,7 @@
#include <errno.h> #include <errno.h>
#include <locale.h> #include <locale.h>
#include <string.h> #include <string.h>
#include <sstream>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.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" ); fprintf( stderr, "Warning: SSH_CONNECTION not found; binding to any interface.\n" );
return string( "0.0.0.0" ); return string( "0.0.0.0" );
} }
char *SSH_writable = strdup( SSH_CONNECTION ); istringstream ss( SSH_CONNECTION );
fatal_assert( SSH_writable ); string dummy, local_interface_IP;
ss >> dummy >> dummy >> local_interface_IP;
strtok( SSH_writable, " " ); if ( !ss ) {
strtok( NULL, " " );
const char *local_interface_IP = strtok( NULL, " " );
if ( !local_interface_IP ) {
fprintf( stderr, "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n" ); fprintf( stderr, "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n" );
return string( "0.0.0.0" ); return string( "0.0.0.0" );
} }
@@ -146,12 +144,12 @@ string get_SSH_IP( void )
/* Strip IPv6 prefix. */ /* Strip IPv6 prefix. */
const char IPv6_prefix[] = "::ffff:"; const char IPv6_prefix[] = "::ffff:";
if ( ( strlen( local_interface_IP ) > strlen( IPv6_prefix ) ) if ( ( local_interface_IP.length() > strlen( IPv6_prefix ) )
&& ( 0 == strncasecmp( local_interface_IP, IPv6_prefix, strlen( IPv6_prefix ) ) ) ) { && ( 0 == strncasecmp( local_interface_IP.c_str(), IPv6_prefix, strlen( IPv6_prefix ) ) ) ) {
return string( local_interface_IP + 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[] ) int main( int argc, char *argv[] )