Skip to content

Commit

Permalink
Fix JSON parsing crashing on empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Oct 20, 2023
1 parent ba553dd commit 6799e3d
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/fnjson/fnjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ void FNJSON::setReadQuery(const string &queryString, uint8_t queryParam)
*/
cJSON *FNJSON::resolveQuery()
{
// Queries must start with a slash, else the JSON parsing crashes FujiNet
// An alternative fix would be to check if the returned value was equal to _json and do something with it, but this is simpler.
_queryString = prependSlash(_queryString);
if (_queryString.empty())
return _json;

return cJSONUtils_GetPointer(_json, _queryString.c_str());
}

Expand Down Expand Up @@ -156,8 +156,6 @@ string FNJSON::getValue(cJSON *item)
return string("");
}

// char *asString = cJSON_PrintUnformatted(item);
// Debug_printf("FNJSON::getValue called with item: >%s<\r\n", asString);
stringstream ss;

if (cJSON_IsString(item))
Expand Down Expand Up @@ -204,21 +202,29 @@ string FNJSON::getValue(cJSON *item)
setLineEnding("\x0a");
#endif

item = item->child;

do
if (item->child == NULL)
{
Debug_printf("FNJSON::getValue OBJECT has no CHILD, adding empty string\r\n");
ss << lineEnding;
}
else
{
#ifdef BUILD_IEC
// Convert key to PETSCII
string tempStr = string((const char *)item->string);
mstr::toPETSCII(tempStr);
ss << tempStr;
#else
ss << item->string;
#endif

ss << lineEnding + getValue(item);
} while ((item = item->next) != NULL);
item = item->child;
do
{
#ifdef BUILD_IEC
// Convert key to PETSCII
string tempStr = string((const char *)item->string);
mstr::toPETSCII(tempStr);
ss << tempStr;
#else
ss << item->string;
#endif

ss << lineEnding + getValue(item);
} while ((item = item->next) != NULL);
}

}
else if (cJSON_IsArray(item))
{
Expand Down

0 comments on commit 6799e3d

Please sign in to comment.