Skip to content

Commit fda274d

Browse files
authored
Fix Value::resize to fill all array elements (open-source-parsers#1265)
* Fix Value::resize to fill all array elements Fixes open-source-parsers#1264
1 parent da9e17d commit fda274d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/lib_json/json_value.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,8 @@ void Value::resize(ArrayIndex newSize) {
912912
if (newSize == 0)
913913
clear();
914914
else if (newSize > oldSize)
915-
this->operator[](newSize - 1);
915+
for (ArrayIndex i = oldSize; i < newSize; ++i)
916+
(*this)[i];
916917
else {
917918
for (ArrayIndex index = newSize; index < oldSize; ++index) {
918919
value_.map_->erase(index);

src/test_lib_json/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "fuzz.h"
1414
#include "jsontest.h"
15+
#include <algorithm>
1516
#include <cmath>
1617
#include <cstring>
1718
#include <functional>
@@ -24,6 +25,7 @@
2425
#include <memory>
2526
#include <sstream>
2627
#include <string>
28+
#include <vector>
2729

2830
using CharReaderPtr = std::unique_ptr<Json::CharReader>;
2931

@@ -347,6 +349,17 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, resizeArray) {
347349
JSONTEST_ASSERT_EQUAL(array.size(), 0);
348350
}
349351
}
352+
353+
JSONTEST_FIXTURE_LOCAL(ValueTest, resizePopulatesAllMissingElements) {
354+
int n = 10;
355+
Json::Value v;
356+
v.resize(n);
357+
JSONTEST_ASSERT_EQUAL(n, v.size());
358+
JSONTEST_ASSERT_EQUAL(n, std::distance(v.begin(), v.end()));
359+
for (const Json::Value& e : v)
360+
JSONTEST_ASSERT_EQUAL(e, Json::Value{});
361+
}
362+
350363
JSONTEST_FIXTURE_LOCAL(ValueTest, getArrayValue) {
351364
Json::Value array;
352365
for (Json::ArrayIndex i = 0; i < 5; i++)

0 commit comments

Comments
 (0)