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

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


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new 6176ec533c [Backport maven-4.0.x] #14126: Coerce operands of && and || 
in conditions (#14150)
6176ec533c is described below

commit 6176ec533c7c2de858a4cd78f1d23aa43e2ab6c2
Author: Guillaume Nodet <[email protected]>
AuthorDate: Fri Nov 27 13:16:04 2026 +0100

    [Backport maven-4.0.x] #14126: Coerce operands of && and || in conditions 
(#14150)
    
    ConditionParser cast the operands of && and || to boolean, so a string
    or a number, such as ${env.CI}, failed with a ClassCastException,
    although !, not(), if() and the condition as a whole coerce such a
    value.
    
    Cherry-picked from 7e4b824dc0b77ddf2328a8998550f690a51d97ad
    
    Co-authored-by: Sharang Gupta <[email protected]>
---
 .../maven/impl/model/profile/ConditionParser.java  | 10 ++++++----
 .../impl/model/profile/ConditionParserTest.java    | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
index 3189760830..8b1dea25ef 100644
--- 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
+++ 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java
@@ -210,10 +210,11 @@ private Object parseLogicalOr() {
         Object left = parseLogicalAnd();
         while (current < tokens.size() && tokens.get(current).equals("||")) {
             current++;
-            if ((boolean) left) {
+            if (toBoolean(left)) {
                 skipOperand(Set.of("||", ",", ")"));
+                left = true;
             } else {
-                left = (boolean) parseLogicalAnd();
+                left = toBoolean(parseLogicalAnd());
             }
         }
         return left;
@@ -229,10 +230,11 @@ private Object parseLogicalAnd() {
         Object left = parseComparison();
         while (current < tokens.size() && tokens.get(current).equals("&&")) {
             current++;
-            if ((boolean) left) {
-                left = (boolean) parseComparison();
+            if (toBoolean(left)) {
+                left = toBoolean(parseComparison());
             } else {
                 skipOperand(Set.of("&&", "||", ",", ")"));
+                left = false;
             }
         }
         return left;
diff --git 
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
 
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
index 08fef22ae3..740fabd9dc 100644
--- 
a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
+++ 
b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/profile/ConditionParserTest.java
@@ -220,6 +220,28 @@ void testLogicalOperatorsShortCircuit() {
         assertThrows(RuntimeException.class, () -> parser.parse("true || 
(false"));
     }
 
+    /**
+     * {@code &&} and {@code ||} coerce a non-boolean operand as {@code !} and 
{@code not()} do.
+     */
+    @Test
+    void testLogicalOperatorsCoerceOperands() {
+        assertTrue((Boolean) parser.parse("'abc' && true"));
+        assertFalse((Boolean) parser.parse("'' && true"));
+        assertTrue((Boolean) parser.parse("true && 'abc'"));
+        assertFalse((Boolean) parser.parse("true && ''"));
+        assertTrue((Boolean) parser.parse("'' || 'abc'"));
+        assertFalse((Boolean) parser.parse("'' || 0"));
+        assertTrue((Boolean) parser.parse("1 || false"));
+        assertTrue((Boolean) parser.parse("${os.name} && ${os.arch} == 
'amd64'"));
+        assertFalse((Boolean) parser.parse("'a' && 'b' && ''"));
+        // an undefined property is false, as it is for !
+        assertFalse((Boolean) parser.parse("${undefined.property} && true"));
+        assertTrue((Boolean) parser.parse("${undefined.property} || 'abc'"));
+        // the right operand is still not evaluated when the left one decides
+        assertFalse((Boolean) parser.parse("'' && unknown('x')"));
+        assertTrue((Boolean) parser.parse("'abc' || unknown('x')"));
+    }
+
     @Test
     void testContainsFunction() {
         assertTrue((Boolean) parser.parse("contains('Hello, World!', 
'World')"));

Reply via email to