Skip to content

Commit

Permalink
Additional command line option sanity checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mubes committed Aug 22, 2024
1 parent 6302668 commit 6a60b57
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Inc/generics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
10 changes: 9 additions & 1 deletion Src/generics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
31 changes: 27 additions & 4 deletions Src/orbcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ------------------------------------
Expand All @@ -517,7 +524,6 @@ bool _processOptions( int argc, char *argv[] )

// ------------------------------------
case 'g':
printf( "%s" EOL, optarg );
options.tsTrigger = genericsUnescape( optarg )[0];
break;

Expand Down Expand Up @@ -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;

// ------------------------------------
Expand Down Expand Up @@ -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;

// ------------------------------------
Expand All @@ -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;
Expand All @@ -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++;

Expand All @@ -633,7 +657,6 @@ bool _processOptions( int argc, char *argv[] )
return false;
}

//*chanIndex++ = 0;
options.presFormat[chan] = strdup( genericsUnescape( chanIndex ) );
break;

Expand Down
36 changes: 34 additions & 2 deletions Src/orbuculum.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ------------------------------------
Expand Down Expand Up @@ -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;

// ------------------------------------
Expand Down Expand Up @@ -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;

// ------------------------------------
Expand All @@ -515,7 +543,6 @@ bool _processOptions( int argc, char *argv[], struct RunTime *r )

if ( *a == ':' )
{
*a = 0;
r->options->nwserverPort = atoi( ++a );
}

Expand All @@ -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;

// ------------------------------------
Expand Down

0 comments on commit 6a60b57

Please sign in to comment.