From 6a60b57df2e4c05739be5cd84db058cd6466b025 Mon Sep 17 00:00:00 2001 From: Dave Marples Date: Thu, 22 Aug 2024 12:51:51 +0100 Subject: [PATCH] Additional command line option sanity checking --- Inc/generics.h | 2 +- Src/generics.c | 10 +++++++++- Src/orbcat.c | 31 +++++++++++++++++++++++++++---- Src/orbuculum.c | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/Inc/generics.h b/Inc/generics.h index b4a20b82..736d0fb7 100644 --- a/Inc/generics.h +++ b/Inc/generics.h @@ -87,7 +87,7 @@ char *genericsEscape( char *str ); char *genericsUnescape( char *str ); uint64_t genericsTimestampuS( void ); uint32_t genericsTimestampmS( void ); -void genericsSetReportLevel( enum verbLevel lset ); +bool genericsSetReportLevel( enum verbLevel lset ); void genericsPrintf( const char *fmt, ... ); char *genericsGetBaseDirectory( void ); const char *genericsBasename( const char *n ); diff --git a/Src/generics.c b/Src/generics.c index 513d7042..5e7a2036 100644 --- a/Src/generics.c +++ b/Src/generics.c @@ -165,10 +165,18 @@ char *genericsUnescape( char *str ) // ==================================================================================================== static enum verbLevel lstore = V_WARN; -void genericsSetReportLevel( enum verbLevel lset ) +bool genericsSetReportLevel( enum verbLevel lset ) { + /* Set is respected to maintain historic behaviour, even if out of range */ lstore = lset; + + if ( ( lset < V_ERROR ) || ( lset > V_MAX_VERBLEVEL ) ) + { + return false; + } + + return true; } // ==================================================================================================== enum verbLevel genericsGetReportLevel( void ) diff --git a/Src/orbcat.c b/Src/orbcat.c index f7897ed8..4e7d1c50 100644 --- a/Src/orbcat.c +++ b/Src/orbcat.c @@ -493,6 +493,13 @@ bool _processOptions( int argc, char *argv[] ) // ------------------------------------ case 'C': options.cps = atoi( optarg ) * 1000; + + if ( options.cps <= 0 ) + { + genericsReport( V_ERROR, "cps out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -517,7 +524,6 @@ bool _processOptions( int argc, char *argv[] ) // ------------------------------------ case 'g': - printf( "%s" EOL, optarg ); options.tsTrigger = genericsUnescape( optarg )[0]; break; @@ -555,6 +561,13 @@ bool _processOptions( int argc, char *argv[] ) case 't': options.useTPIU = true; options.tpiuChannel = atoi( optarg ); + + if ( options.tpiuChannel <= 0 ) + { + genericsReport( V_ERROR, "tpiuChannel out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -596,7 +609,12 @@ bool _processOptions( int argc, char *argv[] ) return false; } - genericsSetReportLevel( atoi( optarg ) ); + if ( !genericsSetReportLevel( atoi( optarg ) ) ) + { + genericsReport( V_ERROR, "Report level out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -606,7 +624,7 @@ bool _processOptions( int argc, char *argv[] ) chan = atoi( optarg ); - if ( chan >= NUM_CHANNELS ) + if ( !chan || ( chan >= NUM_CHANNELS ) ) { genericsReport( V_ERROR, "Channel index out of range" EOL ); return false; @@ -618,6 +636,12 @@ bool _processOptions( int argc, char *argv[] ) chanIndex++; } + if ( !*chanIndex ) + { + genericsReport( V_ERROR, "Channel output spec missing" EOL ); + return false; + } + /* Step over delimiter */ chanIndex++; @@ -633,7 +657,6 @@ bool _processOptions( int argc, char *argv[] ) return false; } - //*chanIndex++ = 0; options.presFormat[chan] = strdup( genericsUnescape( chanIndex ) ); break; diff --git a/Src/orbuculum.c b/Src/orbuculum.c index f5498619..661798f5 100644 --- a/Src/orbuculum.c +++ b/Src/orbuculum.c @@ -424,6 +424,13 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r ) case 'a': r->options->speed = atoi( optarg ); r->options->dataSpeed = r->options->speed; + + if ( r->options->speed <= 0 ) + { + genericsReport( V_ERROR, "Speed out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -456,12 +463,26 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r ) case 'l': r->options->listenPort = atoi( optarg ); + + if ( ( r->options->listenPort <= 0 ) || ( r->options->listenPort > 0xffff ) ) + { + genericsReport( V_ERROR, "Port to listen on is out of range" EOL ); + return false; + } + break; // ------------------------------------ case 'm': r->options->intervalReportTime = atoi( optarg ); + + if ( r->options->intervalReportTime ) + { + genericsReport( V_ERROR, "intervalReportTime is out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -498,6 +519,13 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r ) case 'P': r->options->paceDelay = atoi( optarg ); + + if ( r->options->paceDelay <= 0 ) + { + genericsReport( V_ERROR, "paceDelay is out of range" EOL ); + return false; + } + break; // ------------------------------------ @@ -515,7 +543,6 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r ) if ( *a == ':' ) { - *a = 0; r->options->nwserverPort = atoi( ++a ); } @@ -540,7 +567,12 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r ) return false; } - genericsSetReportLevel( atoi( optarg ) ); + if ( !genericsSetReportLevel( atoi( optarg ) ) ) + { + genericsReport( V_ERROR, "Verbosity out of range" EOL ); + return false; + } + break; // ------------------------------------