From 0f34cdd433fad0dd8ca3f118998c35932e214b8b Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Wed, 17 Jan 2018 14:51:45 +0000
Subject: [PATCH] Issue #2936941 by alexpott: Diff can infinitely loop since
 #2885309

---
 .../Drupal/Component/Diff/Engine/DiffEngine.php |  3 ++-
 .../Component/Diff/Engine/DiffEngineTest.php    | 17 +++++++++++++++++
 .../Component/Diff/Engine/fixtures/file1.txt    |  3 +++
 .../Component/Diff/Engine/fixtures/file2.txt    |  5 +++++
 4 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file1.txt
 create mode 100644 core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file2.txt

diff --git a/core/lib/Drupal/Component/Diff/Engine/DiffEngine.php b/core/lib/Drupal/Component/Diff/Engine/DiffEngine.php
index c32017e300fe..27b0f325724b 100644
--- a/core/lib/Drupal/Component/Diff/Engine/DiffEngine.php
+++ b/core/lib/Drupal/Component/Diff/Engine/DiffEngine.php
@@ -201,8 +201,9 @@ protected function _diag($xoff, $xlim, $yoff, $ylim, $nchunks) {
           continue;
         }
         $matches = $ymatches[$line];
+        $found_empty = FALSE;
         foreach ($matches as $y) {
-          if (!isset($found_empty)) {
+          if (!$found_empty) {
             if (empty($this->in_seq[$y])) {
               $k = $this->_lcs_pos($y);
               $this::USE_ASSERTS && assert($k > 0);
diff --git a/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php b/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php
index 69f92daec9c7..1714e663be55 100644
--- a/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php
+++ b/core/tests/Drupal/Tests/Component/Diff/Engine/DiffEngineTest.php
@@ -86,4 +86,21 @@ public function testDiff($expected, $from, $to) {
     }
   }
 
+  /**
+   * Tests that two files can be successfully diffed.
+   *
+   * @covers ::diff
+   */
+  public function testDiffInfiniteLoop() {
+    $from = explode("\n", file_get_contents(__DIR__ . '/fixtures/file1.txt'));
+    $to = explode("\n", file_get_contents(__DIR__ . '/fixtures/file2.txt'));
+    $diff_engine = new DiffEngine();
+    $diff = $diff_engine->diff($from, $to);
+    $this->assertCount(4, $diff);
+    $this->assertEquals($diff[0], new DiffOpDelete(['    - image.style.max_650x650']));
+    $this->assertEquals($diff[1], new DiffOpCopy(['    - image.style.max_325x325']));
+    $this->assertEquals($diff[2], new DiffOpAdd(['    - image.style.max_650x650', '_core:', '  default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM']));
+    $this->assertEquals($diff[3], new DiffOpCopy(['fallback_image_style: max_325x325', '']));
+  }
+
 }
diff --git a/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file1.txt b/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file1.txt
new file mode 100644
index 000000000000..e50f1e8091d0
--- /dev/null
+++ b/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file1.txt
@@ -0,0 +1,3 @@
+    - image.style.max_650x650
+    - image.style.max_325x325
+fallback_image_style: max_325x325
diff --git a/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file2.txt b/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file2.txt
new file mode 100644
index 000000000000..81a03582310b
--- /dev/null
+++ b/core/tests/Drupal/Tests/Component/Diff/Engine/fixtures/file2.txt
@@ -0,0 +1,5 @@
+    - image.style.max_325x325
+    - image.style.max_650x650
+_core:
+  default_config_hash: 3mjM9p-kQ8syzH7N8T0L9OnCJDSPvHAZoi3q6jcXJKM
+fallback_image_style: max_325x325
-- 
GitLab