Commit b5575c21 authored by sagnowski's avatar sagnowski
Browse files

CmdLnParser: Ensure match strings are within length limit

parent 8009cc26
Loading
Loading
Loading
Loading
+29 −4
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@
#include <string.h>

#define MAX_SUPPORTED_OPTS ( 1024 )
#define MAX_OPTION_LENGTH  ( 1024 )
#define MAX_OPTION_LENGTH  ( 32 )

typedef CmdLnParser_Option OptionProps;

@@ -74,7 +74,7 @@ static int16_t validateOptionProps(
    if ( props.match == NULL )
    {
        /* TODO(sgi): Don't print out usage after this - props.match is used there */
        fprintf( stderr, "[dev] Option with ID == %d - missing required property \"match\"\n", props.id );
        fprintf( stderr, "[dev] Option with ID %d - missing required property \"match\"\n", props.id );
        return -1;
    }

@@ -84,6 +84,18 @@ static int16_t validateOptionProps(
        return -1;
    }

    /* Check match string length */
    if ( strnlen( props.match, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH )
    {
        fprintf( stderr, "[dev] Option with ID %d - match string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH );
        return -1;
    }
    if ( props.matchShort != NULL && strnlen( props.matchShort, MAX_OPTION_LENGTH + 1 ) > MAX_OPTION_LENGTH )
    {
        fprintf( stderr, "[dev] Option with ID %d - matchShort string exceeds limit of %d characters.\n", props.id, MAX_OPTION_LENGTH );
        return -1;
    }

    return 0;
}

@@ -128,6 +140,7 @@ static int8_t stringLooksLikeOption(
    return 0;
}

// TODO: too lenient. Should strip one '-' for short and two '-' for match
static const char *stringToOptionName(
    const char *str )
{
@@ -208,7 +221,13 @@ static int16_t parseOpts(
                /* Check if already parsed */
                if ( optToMatch->hasBeenParsed )
                {
                    fprintf( stderr, "Duplicate option provided: --%s/-%s\n", optToMatch->props.match, optToMatch->props.matchShort );
                    fprintf( stderr, "Duplicate option provided: --%s", optToMatch->props.match );
                    if ( optToMatch->props.matchShort != NULL )
                    {
                        fprintf( stderr, "/-%s", optToMatch->props.matchShort );
                    }
                    fprintf( stderr, "\n" );

                    return -1;
                }

@@ -277,7 +296,13 @@ static int16_t parseOpts(

        if ( opt.props.isMandatory && !opt.hasBeenParsed )
        {
            fprintf( stderr, "Missing mandatory parameter: --%s/-%s\n", opt.props.match, opt.props.matchShort );
            fprintf( stderr, "Missing mandatory parameter: --%s", opt.props.match );
            if ( opt.props.matchShort != NULL )
            {
                fprintf( stderr, "/-%s", opt.props.matchShort );
            }
            fprintf( stderr, "\n" );

            missingMandatory = true;
        }
    }