From 6d81acd3554309ef6096068f1eafbdaf6faccc24 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Tue, 22 Jun 2021 23:22:59 -0400 Subject: [PATCH] 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 --- src/Task.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Task.cpp b/src/Task.cpp index d860632e7..5a60498c4 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -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.