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 7e4b824dc0 Coerce operands of && and || in conditions (#14126)
7e4b824dc0 is described below

commit 7e4b824dc0b77ddf2328a8998550f690a51d97ad
Author: Sharang Gupta <[email protected]>
AuthorDate: Fri Nov 27 09:03:49 2026 +0000

    Coerce operands of && and || in conditions (#14126)
    
    * Coerce operands of && and || in conditions
    
    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.
    
    * Cover undefined and skipped operands in the test
    
    An undefined property counts as false, and the right operand is still 
skipped when the left one decides.
---
 .../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 03f8c166c2..6bda9207b4 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 7b40c20353..86b6dec171 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
@@ -217,6 +217,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