From 78a5eaf8e37548e409792e00798b11a6d867fa9f Mon Sep 17 00:00:00 2001 From: Keith Winstein Date: Thu, 24 May 2012 17:16:03 -0400 Subject: [PATCH] Clearer error message on invalid port (closes #278) --- src/frontend/mosh-client.cc | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/frontend/mosh-client.cc b/src/frontend/mosh-client.cc index fa5114b..a9ab798 100644 --- a/src/frontend/mosh-client.cc +++ b/src/frontend/mosh-client.cc @@ -73,7 +73,7 @@ int main( int argc, char *argv[] ) } } - char *ip; + char *ip, *desired_port; int port; if ( argc - optind != 2 ) { @@ -82,7 +82,24 @@ int main( int argc, char *argv[] ) } ip = argv[ optind ]; - port = myatoi( argv[ optind + 1 ] ); + desired_port = argv[ optind + 1 ]; + + /* Sanity-check arguments */ + if ( ip + && ( strspn( ip, "0123456789." ) != strlen( ip ) ) ) { + fprintf( stderr, "%s: Bad IP address (%s)\n\n", argv[ 0 ], ip ); + usage( argv[ 0 ] ); + exit( 1 ); + } + + if ( desired_port + && ( strspn( desired_port, "0123456789" ) != strlen( desired_port ) ) ) { + fprintf( stderr, "%s: Bad UDP port (%s)\n\n", argv[ 0 ], desired_port ); + usage( argv[ 0 ] ); + exit( 1 ); + } + + port = myatoi( desired_port ); /* Read key from environment */ char *env_key = getenv( "MOSH_KEY" );