Skip to content

Json::Value::null is constructed at random time during program initialization, and can cause segfault #488

Closed
@marklakata

Description

@marklakata

The Json::Value::null object is a global reference object. The rules of C++ allow other constructors from other translation units to access the Json::Value::null symbol before it is constructed, which means that the reference is bogus, and you get a segmentation violation for accessing it. That means if you have a global object that uses Json::Value in its constructor, it can easily crash, depending on the whim of the linker deciding what to link first.

The solution (which I have shown works in my private workspace) is to replace all instances of Json::Value::null with static function (say Json::Value::null_()) in the style of a Meyers Singleton. Then accesses to null_() will always be valid and not depend on a race condition of the global construction list.

const Value& Value::null_()   
{
 static const Json::Value nullStatic(nullValue);
 return nullStatic;
}

// for backwards compatibility, we'll leave this global references around, but DO NOT 
// use them in JSONCPP library code any more!
Json::Value& null = null_();
Json::Value& nullRef = null_();

and replaced all the references to null in the JSONCPP code to null_(). This works now and I can use Json::Value in a constructor of a global object without it crashing.

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