From d2f799164d4ea493611075c92b1487ad6868f53f Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 3 Jun 2024 14:23:31 +0100
Subject: [PATCH] Issue #3451701 by dww, Mingsong, MegaphoneJon: The update
 module should not crash with releases that contain invalid values for
 core_version_requirement

(cherry picked from commit d3b2a6a56ee10ac1fe3ce990a9f2ca679c8268b8)
---
 .../update/src/ProjectCoreCompatibility.php   |  7 ++-
 .../src/Unit/ProjectCoreCompatibilityTest.php | 47 +++++++++++++++++++
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/core/modules/update/src/ProjectCoreCompatibility.php b/core/modules/update/src/ProjectCoreCompatibility.php
index 66c37d03bdaf..16356093b583 100644
--- a/core/modules/update/src/ProjectCoreCompatibility.php
+++ b/core/modules/update/src/ProjectCoreCompatibility.php
@@ -169,7 +169,12 @@ public function setReleaseMessage(array &$project_data) {
    *   version of Drupal core, otherwise FALSE.
    */
   protected function isCoreCompatible($core_compatibility_constraint) {
-    return Semver::satisfies($this->existingCoreVersion, $core_compatibility_constraint);
+    try {
+      return Semver::satisfies($this->existingCoreVersion, $core_compatibility_constraint);
+    }
+    catch (\Exception $e) {
+      return FALSE;
+    }
   }
 
   /**
diff --git a/core/modules/update/tests/src/Unit/ProjectCoreCompatibilityTest.php b/core/modules/update/tests/src/Unit/ProjectCoreCompatibilityTest.php
index 15b407c60925..c98380f5f876 100644
--- a/core/modules/update/tests/src/Unit/ProjectCoreCompatibilityTest.php
+++ b/core/modules/update/tests/src/Unit/ProjectCoreCompatibilityTest.php
@@ -156,4 +156,51 @@ public static function providerSetProjectCoreCompatibilityRanges() {
     return $test_cases;
   }
 
+  /**
+   * @covers ::isCoreCompatible
+   * @dataProvider providerIsCoreCompatible
+   *
+   * @param string $constraint
+   *   The core_version_constraint to test.
+   * @param string $installed_core
+   *   The installed version of core to compare against.
+   * @param bool $expected
+   *   The expected result.
+   */
+  public function testIsCoreCompatible(string $constraint, string $installed_core, bool $expected): void {
+    $core_data['existing_version'] = $installed_core;
+    $project_compatibility = new ProjectCoreCompatibility($core_data, [], []);
+    $reflection = new \ReflectionClass(ProjectCoreCompatibility::class);
+    $reflection_method = $reflection->getMethod('isCoreCompatible');
+    $result = $reflection_method->invokeArgs($project_compatibility, [$constraint]);
+    $this->assertSame($expected, $result);
+  }
+
+  /**
+   * Data provider for testIsCoreCompatible().
+   */
+  public static function providerIsCoreCompatible(): array {
+    $test_cases['compatible exact'] = [
+      '10.3.0',
+      '10.3.0',
+      TRUE,
+    ];
+    $test_cases['compatible with OR'] = [
+      '^9 || ^10',
+      '10.3.0',
+      TRUE,
+    ];
+    $test_cases['incompatible'] = [
+      '^10',
+      '11.0.0',
+      FALSE,
+    ];
+    $test_cases['broken'] = [
+      '^^11',
+      '11.0.0',
+      FALSE,
+    ];
+    return $test_cases;
+  }
+
 }
-- 
GitLab