Skip to content

Fix reader bug: replace asCString() with asString()  #1121

Closed
@dota17

Description

@dota17

When I add the testcase for Reader, I find a bug in

name = String(numberName.asCString());

The testcase is as follows:

JSONTEST_FIXTURE_LOCAL(ReaderTest, allowNumericKeysTest) {
  Json::Features features;
  features.allowNumericKeys_ = true;
  setFeatures(features);
  checkParse(R"({ 123 : "abc" })");
}

The result is as follows:

Testing MemberTemplateAs/BehavesSameAsNamedAs: OK
Testing MemberTemplateIs/BehavesSameAsNamedIs: OK
* Detail of ReaderTest/allowNumericKeys test failure:
../src/test_lib_json/jsontest.cpp(244): Unexpected exception caught:
  in Json::Value::asCString(): requires stringValue
113/114 tests passed (1 failure(s))
-------

By analyzing related code, I find the asCString() is not a suitable interface for NumericKeys.

const char* Value::asCString() const {
  JSON_ASSERT_MESSAGE(type() == stringValue,
                      "in Json::Value::asCString(): requires stringValue");
  if (value_.string_ == nullptr)
    return nullptr;
  unsigned this_len;
  char const* this_str;
  decodePrefixedString(this->isAllocated(), this->value_.string_, &this_len,
                       &this_str);
  return this_str;
}

numberName value is a number value(type is realvalue/uintvalue) and asCString() can not convert the numbervalue to string. We should replace asCString() with asString().

Modift content is shown in #1122

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions