Commit cfc2ada7 authored by sagnowski's avatar sagnowski
Browse files

CmdLnParser: Do not use strnlen - it is not always available

parent 5b95c6db
Loading
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -225,3 +225,28 @@ bool isEmptyString(
{
    return str[0] == '\0';
}


/*---------------------------------------------------------------------*
 * strLength()
 *
 * Get the length of a string up to a maximum length.
 *
 * Equivalent to strnlen, which is not part of the C standard and only provided by POSIX.
 *
 *---------------------------------------------------------------------*/

int32_t strLength(
    const char *str,
    int32_t maxlen )
{
    int32_t len;

    len = 0;
    while ( len < maxlen && str[len] != '\0' )
    {
        len++;
    }

    return len;
}
+2 −0
Original line number Diff line number Diff line
@@ -50,4 +50,6 @@ void clearString( char *str );

bool isEmptyString( const char *str );

int32_t strLength( const char *str, int32_t maxlen );

#endif /* CMDL_TOOLS_H */
+7 −6
Original line number Diff line number Diff line
@@ -52,7 +52,8 @@ typedef struct
} Option;

/* Error enum for internal use */
typedef enum {
typedef enum
{
    CMDLN_PARSER_ERR_OK = 0,
    CMDLN_PARSER_ERR_FAILED_PARSING = -1,
    CMDLN_PARSER_ERR_MISCONFIGURED = -2,
@@ -94,12 +95,12 @@ static CmdLnParserError validateOptionProps(
    }

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