Crash on Json::Value::IsMember
Hi!
The following code dumps core in the isMember function:
#include <iostream>
int main()
{
Json::Value value = Json::Value(Json::arrayValue);
if(value.isMember("HI")) {
std::cout << "Member";
} else {
std::cout << "Not";
}
}
While I think isMember could just return false in this case. On my PC it prints:
terminate called after throwing an instance of 'std::runtime_error'
what(): in Json::Value::operator[](char const*)const: requires objectValue
Aborted (core dumped)
I think it is misleading to return either true or false for a Json object predicate when we are not in fact holding a Json object (we are holding a Json array). The method call itself is nonsensical and should not be given a definitive answer. I would even say that the API would be justified in calling this undefined behavior. it is throwing a run time exception as a courtesy, which is fine. and defined behavior can mean anything including throwing an exception.
your program is only dumping core in the isMember function because you neglected to catch the exception it threw.
On Monday, March 30, 2015, Cjedilo [email protected] wrote:
Hi!
The following code dumps core in the isMember function; #include
int main() { Json::Value value = Json::Value(Json::arrayValue);
if(value.isMember("HI")) { std::cout << "Member"; } else { std::cout << "Not"; }
}
While I think isMember could just return false in this case. On my PC it prints: terminate called after throwing an instance of 'std::runtime_error' what(): in Json::Value::operator http://char%20const*const: requires objectValue Aborted (core dumped)
— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/230.
ǝnɥɐuop ʎllıq
Then why do you support size on a boolean value?
I agree 100% with @BillyGoto. And @Cjedilo, you're right: arguably size() should also fail on a boolean, but it seems like a minor blemish in the API.
Anyway, you can either check in advance of the call, or trap the exception. I am concerned only by crashes caused by the input, since those would be security holes. Maybe a crash which is unlikely to be prevented would be bad too, but this one is only an exception, not a fault or other signal.