From 3f97a5d5ad8f102a2c401706755ce9f0567564e8 Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Wed, 22 Jun 2016 20:05:44 +0200
Subject: [PATCH 1/3] a conceptual quick fix for #269

---
 src/json.hpp      | 7 +++++--
 src/json.hpp.re2c | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/json.hpp b/src/json.hpp
index 9d6687dd..09b6a800 100644
--- a/src/json.hpp
+++ b/src/json.hpp
@@ -10002,13 +10002,16 @@ basic_json_parser_63:
                     // in a second pass, traverse the remaining elements
 
                     // remove my remaining elements
+                    const auto end_index = static_cast<difference_type>(result.size());
                     while (i < source.size())
                     {
-                        result.push_back(object(
+                        // add operations in reverse order to avoid invalid
+                        // indices
+                        result.insert(result.begin() + end_index,
                         {
                             {"op", "remove"},
                             {"path", path + "/" + std::to_string(i)}
-                        }));
+                        });
                         ++i;
                     }
 
diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c
index 9681bed2..c5de9246 100644
--- a/src/json.hpp.re2c
+++ b/src/json.hpp.re2c
@@ -9312,13 +9312,16 @@ class basic_json
                     // in a second pass, traverse the remaining elements
 
                     // remove my remaining elements
+                    const auto end_index = static_cast<difference_type>(result.size());
                     while (i < source.size())
                     {
-                        result.push_back(object(
+                        // add operations in reverse order to avoid invalid
+                        // indices
+                        result.insert(result.begin() + end_index,
                         {
                             {"op", "remove"},
                             {"path", path + "/" + std::to_string(i)}
-                        }));
+                        });
                         ++i;
                     }
 

From 59e67e768f11a18df143eed033cf4fe5a34fbd1d Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Wed, 22 Jun 2016 20:09:06 +0200
Subject: [PATCH 2/3] fix for previous commit

---
 src/json.hpp      | 4 ++--
 src/json.hpp.re2c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/json.hpp b/src/json.hpp
index 09b6a800..c514015a 100644
--- a/src/json.hpp
+++ b/src/json.hpp
@@ -10007,11 +10007,11 @@ basic_json_parser_63:
                     {
                         // add operations in reverse order to avoid invalid
                         // indices
-                        result.insert(result.begin() + end_index,
+                        result.insert(result.begin() + end_index, object(
                         {
                             {"op", "remove"},
                             {"path", path + "/" + std::to_string(i)}
-                        });
+                        }));
                         ++i;
                     }
 
diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c
index c5de9246..86d90114 100644
--- a/src/json.hpp.re2c
+++ b/src/json.hpp.re2c
@@ -9317,11 +9317,11 @@ class basic_json
                     {
                         // add operations in reverse order to avoid invalid
                         // indices
-                        result.insert(result.begin() + end_index,
+                        result.insert(result.begin() + end_index, object(
                         {
                             {"op", "remove"},
                             {"path", path + "/" + std::to_string(i)}
-                        });
+                        }));
                         ++i;
                     }
 

From ecf84dddb1abb2e7e1e937279b51e91d0e77d495 Mon Sep 17 00:00:00 2001
From: Niels <niels.lohmann@gmail.com>
Date: Wed, 22 Jun 2016 20:40:15 +0200
Subject: [PATCH 3/3] test case for #269

---
 README.md         | 2 +-
 test/src/unit.cpp | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 9f78413f..7efcabc5 100644
--- a/README.md
+++ b/README.md
@@ -453,7 +453,7 @@ $ make
 $ ./json_unit "*"
 
 ===============================================================================
-All tests passed (5568705 assertions in 31 test cases)
+All tests passed (5568722 assertions in 32 test cases)
 ```
 
 For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).
diff --git a/test/src/unit.cpp b/test/src/unit.cpp
index 609e1be9..12611b3f 100644
--- a/test/src/unit.cpp
+++ b/test/src/unit.cpp
@@ -14073,6 +14073,15 @@ TEST_CASE("regression tests")
 
         CHECK(data == json({{"key", "value"}, {"key2", "value2"}, {"key3", "value3"}}));
     }
+
+    SECTION("issue #269 - diff generates incorrect patch when removing multiple array elements")
+    {
+        json doc = R"( { "arr1": [1, 2, 3, 4] } )"_json;
+        json expected = R"( { "arr1": [1, 2] } )"_json;
+
+        // check roundtrip
+        CHECK(doc.patch(json::diff(doc, expected)) == expected);
+    }
 }
 
 // special test case to check if memory is leaked if constructor throws