Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rapidjson::Value::AddMember crashes on some keys #2287

Open
rexkogitans opened this issue Jun 8, 2024 · 0 comments
Open

rapidjson::Value::AddMember crashes on some keys #2287

rexkogitans opened this issue Jun 8, 2024 · 0 comments

Comments

@rexkogitans
Copy link

rexkogitans commented Jun 8, 2024

Maybe there is something I do completely overcomplicated or even wrong, but since this function is working for nearly all values, I think it is not too wrong. However, when adding some values, it crashes. It does not depend on the inserted value, but on the inserted key. I found out, that rapidjson::Value::AddMember crashes with a key which contains a slash.

This is my function to add any value to the JSON data. Please note that this function is part of a huge application which is working fine except if const std::string *key is passed a string containing a slash:

rapidjson::Value *JSONDeskModel::addObjectNode(const std::vector<std::string> path, const std::string *key, const rapidjson::Value& newJSONValue) {
	/** Append to parentJSONValue **/
	rapidjson::Value *ret = nullptr;
	if (key == nullptr) {
		if (path.size() == 0) {
			this->newDocument();
			this->jsonRoot->CopyFrom(newJSONValue, this->jsonRoot->GetAllocator());
			ret = (rapidjson::Value *)&this->jsonRoot;
			this->validJSON = true;
		}
	} else {
		rapidjson::Value *parentJSONValue = this->getJSONValue(path);
		if (parentJSONValue != nullptr) {
			if (parentJSONValue->IsObject()) {
				rapidjson::Value localKey;
				localKey.SetString(key->c_str(), key->length(), this->jsonRoot->GetAllocator());
				if (!parentJSONValue->HasMember(localKey)) {
					rapidjson::Value localNewJSONValue;
					localNewJSONValue.CopyFrom(newJSONValue, this->jsonRoot->GetAllocator());
					rapidjson::Value& pj = *((rapidjson::Value *)parentJSONValue);
					pj.AddMember(localKey.Move(), localNewJSONValue.Move(), this->jsonRoot->GetAllocator());  // this is the offending line
					rapidjson::Value::MemberIterator it = pj.FindMember(rapidjson::Value(key->c_str(), key->length(), this->jsonRoot->GetAllocator()));
					ret = &it->value;
					this->validJSON = true;
				}
			}
		}
	}
	return ret;
}

What am I missing here? How can I protect this function from crashing? It should either just fail and return nullptr, or just work. I already tried wrapping the offending line with a try ... catch block with no success, because it is a SIGSEGV which cannot be caught.

I put it here in the issues because maybe it is really a bug in RapidJSON.

Kind regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant