Skip to content

Commit

Permalink
Remove unhelpful ParseArgs help text validation (#17394)
Browse files Browse the repository at this point in the history
- Help text validation when
  CHIP_CONFIG_ENABLE_ARG_PARSER_VALIDITY_CHECKS are enabled
  are both unhelpful and unsafe. Unhelpful because
  such failures are unlikely to get better code delivered,
  and unsafe because the algorithms used make use of
  sketchy tricky code with negative indexing that was shown
  to sometimes fail on ARM builds.

This PR removes the help content checks

Fixes #17323
  • Loading branch information
tcarmelveilleux authored Apr 14, 2022
1 parent ea4c3dd commit c2e0357
Showing 1 changed file with 0 additions and 46 deletions.
46 changes: 0 additions & 46 deletions src/lib/support/CHIPArgParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ static void PutStringWithNewLine(FILE * s, const char * str);
static void PutStringWithBlankLine(FILE * s, const char * str);
#if CHIP_CONFIG_ENABLE_ARG_PARSER_VALIDITY_CHECKS
static bool SanityCheckOptions(OptionSet * optSets[]);
static bool HelpTextContainsLongOption(const char * optName, const char * helpText);
static bool HelpTextContainsShortOption(char optChar, const char * helpText);
#endif // CHIP_CONFIG_ENABLE_ARG_PARSER_VALIDITY_CHECKS

static inline bool IsShortOptionChar(int ch)
Expand Down Expand Up @@ -1511,53 +1509,9 @@ static bool SanityCheckOptions(OptionSet * optSets[])
}
}

// Fail if the option help texts do not contain a description for each option, including both
// the option's long and short forms.
for (OptionSet ** optSetP = optSets; *optSetP != nullptr; optSetP++)
for (OptionDef * optionDef = (*optSetP)->OptionDefs; optionDef->Name != nullptr; optionDef++)
{
if (!HelpTextContainsLongOption(optionDef->Name, (*optSetP)->OptionHelp))
{
PrintArgError("INTERNAL ERROR: No help text defined for option: --%s\n", optionDef->Name);
res = false;
}

if (IsShortOptionChar(optionDef->Id) &&
!HelpTextContainsShortOption(static_cast<char>(optionDef->Id), (*optSetP)->OptionHelp))
{
PrintArgError("INTERNAL ERROR: No help text defined for option: -%c\n", optionDef->Id);
res = false;
}
}

return res;
}

static bool HelpTextContainsLongOption(const char * optName, const char * helpText)
{
size_t nameLen = strlen(optName);

for (const char * p = helpText; (p = strstr(p, optName)) != nullptr; p += nameLen)
if ((p - helpText) >= 2 && p[-1] == '-' && p[-2] == '-' && !isalnum(p[nameLen]) && p[nameLen] != '-')
return true;

return false;
}

static bool HelpTextContainsShortOption(char optChar, const char * helpText)
{
char optStr[3];
optStr[0] = '-';
optStr[1] = optChar;
optStr[2] = 0;

for (const char * p = helpText; (p = strstr(p, optStr)) != nullptr; p += 2)
if ((p == helpText || (!isalnum(p[-1]) && p[-1] != '-')) && !isalnum(p[2]) && p[2] != '-')
return true;

return false;
}

#endif // CHIP_CONFIG_ENABLE_ARG_PARSER_VALIDITY_CHECKS

} // namespace ArgParser
Expand Down

0 comments on commit c2e0357

Please sign in to comment.