1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-03-03 14:52:21 +02:00

Fix help when a valid option is invalid for the specified command.

Getting help for a valid option that was invalid for the command would segfault.

Add a check to ensure the option is valid for the command's default role.
This commit is contained in:
David Steele 2021-04-22 11:48:04 -04:00 committed by GitHub
parent 72dac5b10b
commit 45f83558ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -14,6 +14,19 @@
<release-list>
<release date="XXXX-XX-XX" version="2.34dev" title="UNDER DEVELOPMENT">
<release-core-list>
<release-bug-list>
<release-item>
<github-pull-request id="1375"/>
<release-item-contributor-list>
<release-item-ideator id="cynthia.shang"/>
<release-item-reviewer id="stefan.fercot"/>
</release-item-contributor-list>
<p>Fix <cmd>help</cmd> when a valid option is invalid for the specified command.</p>
</release-item>
</release-bug-list>
<release-improvement-list>
<release-item>
<github-issue id="986"/>

View File

@ -482,16 +482,23 @@ helpRender(void)
const String *optionName = strLstGet(cfgCommandParam(), 0);
CfgParseOptionResult option = cfgParseOption(optionName);
// If the option was not found it might be an indexed option without the index, e.g. repo-host instead of
// repo1-host. This is valid for help even though the parser will reject it.
if (!option.found)
{
int optionId = cfgParseOptionId(strZ(optionName));
if (optionId == -1)
THROW_FMT(OptionInvalidError, "option '%s' is not valid for command '%s'", strZ(optionName), commandName);
else
if (optionId != -1)
{
option.id = (unsigned int)optionId;
option.found = true;
}
}
// Error when option is not found or is invalid for the current command
if (!option.found || !cfgParseOptionValid(cfgCommand(), cfgCmdRoleDefault, option.id))
THROW_FMT(OptionInvalidError, "option '%s' is not valid for command '%s'", strZ(optionName), commandName);
// Output option summary and description
strCatFmt(
result,

View File

@ -307,6 +307,14 @@ testRun(void)
TEST_RESULT_VOID(harnessCfgLoadRaw(strLstSize(argList), strLstPtr(argList)), "parse bogus option");
TEST_ERROR(helpRender(), OptionInvalidError, "option 'BOGUS' is not valid for command 'archive-push'");
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, CFGCMD_HELP);
strLstAddZ(argList, CFGCMD_ARCHIVE_PUSH);
strLstAddZ(argList, CFGOPT_PROCESS);
TEST_RESULT_VOID(harnessCfgLoadRaw(strLstSize(argList), strLstPtr(argList)), "parse option invalid for command");
TEST_ERROR(helpRender(), OptionInvalidError, "option 'process' is not valid for command 'archive-push'");
// -------------------------------------------------------------------------------------------------------------------------
const char *optionHelp = strZ(strNewFmt(
"%s - 'archive-push' command - 'buffer-size' option help\n"