hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
From 07d7c3b2e0f8c6b269ba167117cd3e549df2f342 Mon Sep 17 00:00:00 2001
From: Vinnie Falco <vinnie.falco@gmail.com>
Date: Wed, 13 Apr 2022 05:49:05 -0700
Subject: [PATCH] array::erase relocates correctly
 
fix #692
 
Signed-off-by: Michael Nosthoff<buildroot@heine.tech>
[Upstream status: https://github.com/boostorg/json/issues/692]
---
 boost/json/impl/array.ipp |  5 ++++-
 test/array.cpp                    | 16 ++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 
diff --git a/boost/json/impl/array.ipp b/boost/json/impl/array.ipp
index 4d067fb5..a2c7fd6d 100644
--- a/boost/json/impl/array.ipp
+++ b/boost/json/impl/array.ipp
@@ -491,8 +491,11 @@ erase(
     auto const p = &(*t_)[0] +
         (pos - &(*t_)[0]);
     destroy(p, p + 1);
-    relocate(p, p + 1, 1);
     --t_->size;
+    if(t_->size > 0)
+        relocate(p, p + 1,
+            t_->size - (p -
+                &(*t_)[0]));
     return p;
 }
 
diff --git a/libs/json/test/array.cpp b/libs/json/test/array.cpp
index 1cc87566..4516cc78 100644
--- a/libs/json/test/array.cpp
+++ b/libs/json/test/array.cpp
@@ -1269,6 +1269,21 @@ class array_test
             array{nullptr, "a", "b"}));
     }
 
+    void
+    testIssue692()
+    {
+        array a;
+        object obj;
+        obj["test1"] = "hello";
+        a.push_back(obj);
+        a.push_back(obj);
+        a.push_back(obj);
+        a.push_back(obj);
+        a.push_back(obj);
+        while(a.size())
+            a.erase(a.begin());
+    }
+
     void
     run()
     {
@@ -1283,6 +1298,7 @@ class array_test
         testExceptions();
         testEquality();
         testHash();
+        testIssue692();
     }
 };