Skip to content

Commit

Permalink
[#1680] Bugfix: disallow ArgGroups with multiplicity=0 to prevent Sta…
Browse files Browse the repository at this point in the history
…ckOverflowError during parsing

Closes #1680
  • Loading branch information
remkop committed Jun 6, 2022
1 parent 12df079 commit c1fd7fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Picocli 4.7.0 introduced a `sortSynopsis = false` attribute to let the synopsis
* [#1602] Enhancement: Fix incorrect debug output for add/removeAlias.
* [#1603] Enhancement: Improve debug tracing information for help requests and command execution.
* [#1629] Enhancement: Omit empty braces in standard prompt for interactive options without description. Thanks to [Andreas Deininger](https://github.com/deining) for raising this.
* [#1680] Bugfix: ArgGroups with `multiplicity="0"` are now disallowed at construction time and no longer throw a `StackOverflowError` while parsing. Thanks to [ARNOLD Somogyi](https://github.com/zappee) for raising this.
* [#1615][#1616] Bugfix: `getCJKAdjustedLength()` no longer miscalculates for supplementary code points. Thanks to [gwalbran](https://github.com/gwalbran) for the pull request.
* [#1575] Bugfix: Synopsis should not cluster boolean options if `posixClusteredShortOptionsAllowed` is set to false.
* [#1642] Bugfix: Negatable options should negate explicit values. Thanks to [Nat Burns](https://github.com/burnnat) for raising this.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -10339,6 +10339,8 @@ public static class ArgGroupSpec implements IOrdered {
headingKey = NO_HEADING_KEY.equals(builder.headingKey) ? null : builder.headingKey;
exclusive = builder.exclusive && builder.validate; // non-validating groups cannot be exclusive: https://github.com/remkop/picocli/issues/810
multiplicity = builder.multiplicity;
if (multiplicity.max() <= 0) { throw new InitializationException("ArgGroup must have multiplicity that allows at least one occurrence, but had multiplicity=" + multiplicity); }

validate = builder.validate;
order = builder.order;
typeInfo = builder.typeInfo;
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/picocli/ArgGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4331,4 +4331,24 @@ public void testIssue1409ModInitializeGroup2() {
assertEquals(errorB2,"z", obj.optXAndGroupOneOrGroupTwo.oneORtwo.two.b2);
}

static class Issue1680MultiplicityZeroCausesInfiniteLoop {
static class MyGroup {
@Option(names = "-x") String x;
}

@ArgGroup(multiplicity = "0")
MyGroup myGroup;
}

@Test
public void testIssue1680MultiplicityZeroCausesInfiniteLoop() {
final Object obj = new Issue1680MultiplicityZeroCausesInfiniteLoop();
try {
new CommandLine(obj).parseArgs("-x", "abc");
fail("Expected exception");
} catch (InitializationException good) {
assertEquals("ArgGroup must have multiplicity that allows at least one occurrence, but had multiplicity=0", good.getMessage());
}
}

}

0 comments on commit c1fd7fd

Please sign in to comment.