Skip to content

JSON Parse error when reading json object from file #3011

@hari269it

Description

@hari269it

Observing random crash when reading JSON file data to a JSON object. Here is the summary of the crash. The crash point is always consistent.

Description:

One process takes care of the following:

[Library Call] --> Returns serialised JSON key-value pair as char *, which is placed in std::string Example : std:string testString = libCall(); testString now holds a string like this {"IS_JSON_FLAGON": true}

To ensure there are no additional characters and to adhere to the JSON object compatibility, the std::string is parsed and then assigned to a JSON object

Example: nlohmann::json jWriteObj = nlohmann::json::parse(testString .c_str());

The JSON object is then written to JSON file
Example :

 mutex mLock; //global 
upDateFlagsFile()
{
    fstream fileDesc;
  
    mLock.lock();
    fileDesc.open(<filepath>, trunc mode);
    fileDesc << jWriteObj;
    fileDesc.close();
    mLock.unlock();
}

this creates/updates the file with .json extension Example: flagsFile.json with data {"IS_JSON_FLAGON":true}

In another process, the file written by the previous process is read

Modules want to know the Flag value, so calls isFlagOn("IS_JSON_FLAGON") which then opens the file and reads the data to a json object

Example: [Module reading the flag value] ---->isFlagOn("IS_JSON_FLAGON") definition for isFlagOn()

extern   mutex mLock; // using the same mutex for sync the access to file 
isFlagOn(char* flagName)
{
   istream fileDesc;
   bool flagValue = false; 
   nlohmann::json jReadObj;
try{
   mLock.lock();   
   fileDesc.open();
   fileDesc >> jReadObj; //  ---> Crash occurs randomly at this line
   flagValue = jReadObj[flagName].get<bool>();
   fileDesc.close();
   mLock.unlock();
  }
   catch(nlohmann::json::exception &e)
   {
       std:cout<< "Exception caught";
   }
   return flagValue;
}

The crash is very random on Ubuntu 20.04 and points to the line highlighted. The surprising part is the exception is not getting caught in spite of using the wildcard exception approach

Any suggestions on what needs to be corrected in this would help.

Snippet of crash:

#5 0x00007fe72ed436a9 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x000055fa7315cae9 in nlohmann::basic_json<std::map, std::vector, std::string, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>::operator[] (this=0x7fe67dff8840, key=0x55fa7385c64a

We are using nlohmann version 3.7.3 in our product

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions