Skip to content

Commit 72b5293

Browse files
committed
Merge pull request #207 from cdunn2001/fix_CZString_copy_constructor
Fix czstring copy constructor
2 parents 401e982 + a63d82d commit 72b5293

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/lib_json/json_value.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,16 @@ void Value::CommentInfo::setComment(const char* text, size_t len) {
196196
Value::CZString::CZString(ArrayIndex index) : cstr_(0), index_(index) {}
197197

198198
Value::CZString::CZString(char const* str, unsigned length, DuplicationPolicy allocate)
199-
: cstr_(allocate == duplicate ? duplicateStringValue(str) : str)
199+
: cstr_(str)
200200
{
201+
// allocate != duplicate
201202
storage_.policy_ = allocate;
202203
storage_.length_ = length;
203204
}
204205

205206
Value::CZString::CZString(const CZString& other)
206207
: cstr_(other.storage_.policy_ != noDuplication && other.cstr_ != 0
207-
? duplicateStringValue(other.cstr_)
208+
? duplicateStringValue(other.cstr_, other.storage_.length_)
208209
: other.cstr_)
209210
{
210211
storage_.policy_ = (other.cstr_

src/test_lib_json/main.cpp

+36-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <iostream>
12
// Copyright 2007-2010 Baptiste Lepilleur
23
// Distributed under MIT license, or public domain if desired and
34
// recognized in your jurisdiction.
@@ -213,6 +214,31 @@ JSONTEST_FIXTURE(ValueTest, objects) {
213214
JSONTEST_ASSERT_EQUAL(false, did);
214215
}
215216

217+
JSONTEST_FIXTURE(ValueTest, nulls) {
218+
static char const keyWithNulls[] = "key\0with\0nulls";
219+
std::string const strKeyWithNulls(keyWithNulls, sizeof keyWithNulls);
220+
object1_[strKeyWithNulls] = "object1_[keyWithNulls]";
221+
Json::Value::Members f = object1_.getMemberNames();
222+
std::cout << "size:" << f.size() << "\n";
223+
for (int i=0; i<f.size(); ++i) {
224+
std::cout << f[i].size() << ":" << f[i] << "\n";
225+
}
226+
//abort();
227+
Json::Value const& o = object1_;
228+
Json::Value const& temp = o[strKeyWithNulls];
229+
JSONTEST_ASSERT_EQUAL(Json::Value("object1_[keyWithNulls]"), temp);
230+
JSONTEST_ASSERT(object1_.isMember(keyWithNulls, keyWithNulls + strKeyWithNulls.length()));
231+
//JSONTEST_ASSERT(object1_.isMember(keyWithNulls, keyWithNulls + sizeof(keyWithNulls)));
232+
JSONTEST_ASSERT(!object1_.isMember("key"));
233+
234+
Json::Value got;
235+
bool did;
236+
did = object1_.removeMember(strKeyWithNulls, &got);
237+
JSONTEST_ASSERT_EQUAL(Json::Value("object1_[keyWithNulls]"), got);
238+
JSONTEST_ASSERT_EQUAL(true, did);
239+
did = object1_.removeMember(strKeyWithNulls, &got);
240+
JSONTEST_ASSERT_EQUAL(false, did);
241+
}
216242
JSONTEST_FIXTURE(ValueTest, arrays) {
217243
const unsigned int index0 = 0;
218244

@@ -1585,8 +1611,9 @@ JSONTEST_FIXTURE(ValueTest, CommentBefore) {
15851611
}
15861612

15871613
JSONTEST_FIXTURE(ValueTest, zeroes) {
1588-
std::string binary("hi", 3); // include trailing 0
1589-
JSONTEST_ASSERT_EQUAL(3, binary.length());
1614+
char const cstr[] = "h\0i";
1615+
std::string binary(cstr, sizeof(cstr)); // include trailing 0
1616+
JSONTEST_ASSERT_EQUAL(4U, binary.length());
15901617
Json::StreamWriterBuilder b;
15911618
{
15921619
Json::Value root;
@@ -1600,26 +1627,26 @@ JSONTEST_FIXTURE(ValueTest, zeroes) {
16001627
JSONTEST_ASSERT_STRING_EQUAL(binary, root[top].asString());
16011628
Json::Value removed;
16021629
bool did;
1603-
did = root.removeMember(top, top + 3U,
1630+
did = root.removeMember(top, top + sizeof(top) - 1U,
16041631
&removed);
16051632
JSONTEST_ASSERT(did);
16061633
JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString());
1607-
did = root.removeMember(top, top + 3U,
1634+
did = root.removeMember(top, top + sizeof(top) - 1U,
16081635
&removed);
16091636
JSONTEST_ASSERT(!did);
16101637
JSONTEST_ASSERT_STRING_EQUAL(binary, removed.asString()); // still
16111638
}
16121639
}
16131640

16141641
JSONTEST_FIXTURE(ValueTest, zeroesInKeys) {
1615-
std::string binary("hi", 3); // include trailing 0
1616-
JSONTEST_ASSERT_EQUAL(3, binary.length());
1617-
Json::StreamWriterBuilder b;
1642+
char const cstr[] = "h\0i";
1643+
std::string binary(cstr, sizeof(cstr)); // include trailing 0
1644+
JSONTEST_ASSERT_EQUAL(4U, binary.length());
16181645
{
16191646
Json::Value root;
16201647
root[binary] = "there";
16211648
JSONTEST_ASSERT_STRING_EQUAL("there", root[binary].asString());
1622-
JSONTEST_ASSERT(!root.isMember("hi"));
1649+
JSONTEST_ASSERT(!root.isMember("h"));
16231650
JSONTEST_ASSERT(root.isMember(binary));
16241651
JSONTEST_ASSERT_STRING_EQUAL("there", root.get(binary, Json::Value::nullRef).asString());
16251652
Json::Value removed;
@@ -2306,6 +2333,7 @@ int main(int argc, const char* argv[]) {
23062333
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, typeChecksThrowExceptions);
23072334
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, StaticString);
23082335
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, CommentBefore);
2336+
//JSONTEST_REGISTER_FIXTURE(runner, ValueTest, nulls);
23092337
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, zeroes);
23102338
JSONTEST_REGISTER_FIXTURE(runner, ValueTest, zeroesInKeys);
23112339

0 commit comments

Comments
 (0)