parseJSON: Ensure NULL values from failed lookups do not persist

The operator[] insets values if the lookup fails, which creates null
pointers.

See: https://en.cppreference.com/w/cpp/container/map/operator_at
This commit is contained in:
Tomas Babej 2021-06-22 23:22:59 -04:00
parent 75422e80e3
commit 6d81acd355

View file

@ -757,14 +757,19 @@ void Task::parseJSON (const json::object* root_obj)
for (auto& annotations : atts->_data)
{
auto annotation = (json::object*)annotations;
auto when = (json::string*)annotation->_data["entry"];
auto what = (json::string*)annotation->_data["description"];
if (! when)
throw format ("Annotation is missing an entry date: {1}", root_obj-> dump ());
if (! when) {
annotation->_data.erase ("entry"); // Erase NULL entry inserted by failed lookup above
throw format ("Annotation is missing an entry date: {1}", annotation-> dump ());
}
if (! what)
throw format ("Annotation is missing a description: {1}", root_obj->dump ());
if (! what) {
annotation->_data.erase ("description"); // Erase NULL description inserted by failed lookup above
throw format ("Annotation is missing a description: {1}", annotation->dump ());
}
// Extract 64-bit annotation entry value
// Time travelers from 2038, we have your back.