This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 9c1871fd22 Fix StringIndexOutOfBoundsException (#1618)
9c1871fd22 is described below

commit 9c1871fd22e3469395aa20bbe7e833559c3f6d88
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Aug 12 23:56:26 2024 +0200

    Fix StringIndexOutOfBoundsException (#1618)
---
 .../main/java/org/apache/maven/cli/MavenCli.java   | 38 +++++++++++++---------
 .../java/org/apache/maven/cli/MavenCliTest.java    | 14 ++++++++
 2 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java 
b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index 02974d2d90..5239b9b4e6 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -1421,16 +1421,19 @@ public class MavenCli {
                 for (String token : optionValue.split(",")) {
                     String selector = token.trim();
                     boolean active = true;
-                    if (selector.charAt(0) == '-' || selector.charAt(0) == 
'!') {
-                        active = false;
-                        selector = selector.substring(1);
-                    } else if (token.charAt(0) == '+') {
+                    if (!selector.isEmpty()) {
+                        if (selector.charAt(0) == '-' || selector.charAt(0) == 
'!') {
+                            active = false;
+                            selector = selector.substring(1);
+                        } else if (token.charAt(0) == '+') {
+                            selector = selector.substring(1);
+                        }
+                    }
+                    boolean optional = false;
+                    if (!selector.isEmpty() && selector.charAt(0) == '?') {
+                        optional = true;
                         selector = selector.substring(1);
                     }
-
-                    boolean optional = selector.charAt(0) == '?';
-                    selector = selector.substring(optional ? 1 : 0);
-
                     projectActivation.addProjectActivation(selector, active, 
optional);
                 }
             }
@@ -1450,16 +1453,19 @@ public class MavenCli {
                 for (String token : optionValue.split(",")) {
                     String profileId = token.trim();
                     boolean active = true;
-                    if (profileId.charAt(0) == '-' || profileId.charAt(0) == 
'!') {
-                        active = false;
-                        profileId = profileId.substring(1);
-                    } else if (token.charAt(0) == '+') {
+                    if (!profileId.isEmpty()) {
+                        if (profileId.charAt(0) == '-' || profileId.charAt(0) 
== '!') {
+                            active = false;
+                            profileId = profileId.substring(1);
+                        } else if (token.charAt(0) == '+') {
+                            profileId = profileId.substring(1);
+                        }
+                    }
+                    boolean optional = false;
+                    if (!profileId.isEmpty() && profileId.charAt(0) == '?') {
+                        optional = true;
                         profileId = profileId.substring(1);
                     }
-
-                    boolean optional = profileId.charAt(0) == '?';
-                    profileId = profileId.substring(optional ? 1 : 0);
-
                     profileActivation.addProfileActivation(profileId, active, 
optional);
                 }
             }
diff --git 
a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java 
b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
index 40e15ac382..d9da65ff3d 100644
--- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
@@ -608,6 +608,20 @@ class MavenCliTest {
         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
     @MethodSource("activateBatchModeArguments")
     public void activateBatchMode(boolean ciEnv, String[] cliArgs, boolean 
isBatchMode) throws Exception {

Reply via email to