Fix StringIndexOutOfBoundsException (#1618)

This commit is contained in:
Guillaume Nodet 2024-08-12 23:56:26 +02:00 committed by GitHub
parent 97bc109a43
commit 9c1871fd22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 16 deletions

View File

@ -1421,16 +1421,19 @@ public class MavenCli {
for (String token : optionValue.split(",")) { for (String token : optionValue.split(",")) {
String selector = token.trim(); String selector = token.trim();
boolean active = true; boolean active = true;
if (!selector.isEmpty()) {
if (selector.charAt(0) == '-' || selector.charAt(0) == '!') { if (selector.charAt(0) == '-' || selector.charAt(0) == '!') {
active = false; active = false;
selector = selector.substring(1); selector = selector.substring(1);
} else if (token.charAt(0) == '+') { } else if (token.charAt(0) == '+') {
selector = selector.substring(1); selector = selector.substring(1);
} }
}
boolean optional = selector.charAt(0) == '?'; boolean optional = false;
selector = selector.substring(optional ? 1 : 0); if (!selector.isEmpty() && selector.charAt(0) == '?') {
optional = true;
selector = selector.substring(1);
}
projectActivation.addProjectActivation(selector, active, optional); projectActivation.addProjectActivation(selector, active, optional);
} }
} }
@ -1450,16 +1453,19 @@ public class MavenCli {
for (String token : optionValue.split(",")) { for (String token : optionValue.split(",")) {
String profileId = token.trim(); String profileId = token.trim();
boolean active = true; boolean active = true;
if (!profileId.isEmpty()) {
if (profileId.charAt(0) == '-' || profileId.charAt(0) == '!') { if (profileId.charAt(0) == '-' || profileId.charAt(0) == '!') {
active = false; active = false;
profileId = profileId.substring(1); profileId = profileId.substring(1);
} else if (token.charAt(0) == '+') { } else if (token.charAt(0) == '+') {
profileId = profileId.substring(1); profileId = profileId.substring(1);
} }
}
boolean optional = profileId.charAt(0) == '?'; boolean optional = false;
profileId = profileId.substring(optional ? 1 : 0); if (!profileId.isEmpty() && profileId.charAt(0) == '?') {
optional = true;
profileId = profileId.substring(1);
}
profileActivation.addProfileActivation(profileId, active, optional); profileActivation.addProfileActivation(profileId, active, optional);
} }
} }

View File

@ -608,6 +608,20 @@ class MavenCliTest {
assertThat(request.getCommandLine().getArgs(), equalTo(new String[] {"prefix:3.0.0:bar", "validate"})); assertThat(request.getCommandLine().getArgs(), equalTo(new String[] {"prefix:3.0.0:bar", "validate"}));
} }
@Test
public void testEmptyProfile() throws Exception {
CliRequest request = new CliRequest(new String[] {"-P", ""}, null);
cli.cli(request);
cli.populateRequest(request);
}
@Test
public void testEmptyProject() throws Exception {
CliRequest request = new CliRequest(new String[] {"-pl", ""}, null);
cli.cli(request);
cli.populateRequest(request);
}
@ParameterizedTest @ParameterizedTest
@MethodSource("activateBatchModeArguments") @MethodSource("activateBatchModeArguments")
public void activateBatchMode(boolean ciEnv, String[] cliArgs, boolean isBatchMode) throws Exception { public void activateBatchMode(boolean ciEnv, String[] cliArgs, boolean isBatchMode) throws Exception {