Skip to content

Commit c8a8cfc

Browse files
ycqiucdunn2001
authored andcommitted
fix
In value.h, ValueConstIterator can convert to ValueIterator, I think that is a bug. the correct way is ValueIterator can convert to ValueConstIterator.
1 parent 4994c77 commit c8a8cfc

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

include/json/value.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ class JSON_API ValueConstIterator : public ValueIteratorBase {
738738
typedef ValueConstIterator SelfType;
739739

740740
ValueConstIterator();
741+
ValueConstIterator(ValueIterator const& other);
741742

742743
private:
743744
/*! \internal Use by Value to create an iterator.
@@ -787,7 +788,7 @@ class JSON_API ValueIterator : public ValueIteratorBase {
787788
typedef ValueIterator SelfType;
788789

789790
ValueIterator();
790-
ValueIterator(const ValueConstIterator& other);
791+
explicit ValueIterator(const ValueConstIterator& other);
791792
ValueIterator(const ValueIterator& other);
792793

793794
private:

src/lib_json/json_valueiterator.inl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ ValueConstIterator::ValueConstIterator(
129129
const Value::ObjectValues::iterator& current)
130130
: ValueIteratorBase(current) {}
131131

132+
ValueConstIterator::ValueConstIterator(ValueIterator const& other)
133+
: ValueIteratorBase(other) {}
134+
132135
ValueConstIterator& ValueConstIterator::
133136
operator=(const ValueIteratorBase& other) {
134137
copy(other);
@@ -149,7 +152,9 @@ ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current)
149152
: ValueIteratorBase(current) {}
150153

151154
ValueIterator::ValueIterator(const ValueConstIterator& other)
152-
: ValueIteratorBase(other) {}
155+
: ValueIteratorBase(other) {
156+
throwRuntimeError("ConstIterator to Iterator should never be allowed.");
157+
}
153158

154159
ValueIterator::ValueIterator(const ValueIterator& other)
155160
: ValueIteratorBase(other) {}

src/test_lib_json/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <limits>
1111
#include <sstream>
1212
#include <string>
13-
#include <iostream>
1413
#include <iomanip>
1514

1615
// Make numeric limits more convenient to talk about.
@@ -2436,7 +2435,9 @@ JSONTEST_FIXTURE(IteratorTest, indexes) {
24362435

24372436
JSONTEST_FIXTURE(IteratorTest, const) {
24382437
Json::Value const v;
2439-
Json::Value::iterator it = v.begin(); // This *should not* compile, but does.
2438+
JSONTEST_ASSERT_THROWS(
2439+
Json::Value::iterator it(v.begin()) // Compile, but throw.
2440+
);
24402441

24412442
Json::Value value;
24422443

0 commit comments

Comments
 (0)