Skip to content

Commit

Permalink
Fix Value::resize to fill all array elements (open-source-parsers#1265)
Browse files Browse the repository at this point in the history
* Fix Value::resize to fill all array elements

Fixes open-source-parsers#1264
  • Loading branch information
BillyDonahue authored Feb 10, 2021
1 parent da9e17d commit fda274d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib_json/json_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,8 @@ void Value::resize(ArrayIndex newSize) {
if (newSize == 0)
clear();
else if (newSize > oldSize)
this->operator[](newSize - 1);
for (ArrayIndex i = oldSize; i < newSize; ++i)
(*this)[i];
else {
for (ArrayIndex index = newSize; index < oldSize; ++index) {
value_.map_->erase(index);
Expand Down
13 changes: 13 additions & 0 deletions src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "fuzz.h"
#include "jsontest.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
Expand All @@ -24,6 +25,7 @@
#include <memory>
#include <sstream>
#include <string>
#include <vector>

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

Expand Down Expand Up @@ -347,6 +349,17 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, resizeArray) {
JSONTEST_ASSERT_EQUAL(array.size(), 0);
}
}

JSONTEST_FIXTURE_LOCAL(ValueTest, resizePopulatesAllMissingElements) {
int n = 10;
Json::Value v;
v.resize(n);
JSONTEST_ASSERT_EQUAL(n, v.size());
JSONTEST_ASSERT_EQUAL(n, std::distance(v.begin(), v.end()));
for (const Json::Value& e : v)
JSONTEST_ASSERT_EQUAL(e, Json::Value{});
}

JSONTEST_FIXTURE_LOCAL(ValueTest, getArrayValue) {
Json::Value array;
for (Json::ArrayIndex i = 0; i < 5; i++)
Expand Down

0 comments on commit fda274d

Please sign in to comment.