From 5ebe0b8becd994223c854fbd1e29bdbbb21d0c18 Mon Sep 17 00:00:00 2001 From: Gary Heckman Date: Tue, 20 Jul 2021 15:16:46 -0400 Subject: [PATCH] Fix itsNextName not clearing when not found An issue exists when loading vectors of objects where, if the last nvp of the previous object does not exist in the json file, the itsNextName variable within the json serializer is not cleared. This causes the vector serializer to search for that name next (when it should be searching for a nameless object.) The json serializer then throws during the named search. Mild reworking of itsNextName solution --- include/cereal/archives/json.hpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/cereal/archives/json.hpp b/include/cereal/archives/json.hpp index 61e30766..456b6893 100644 --- a/include/cereal/archives/json.hpp +++ b/include/cereal/archives/json.hpp @@ -564,18 +564,20 @@ namespace cereal @throws Exception if an expectedName is given and not found */ inline void search() { + // store pointer to itsNextName locally and reset to nullptr in case search() throws + auto localNextName = itsNextName; + itsNextName = nullptr; + // The name an NVP provided with setNextName() - if( itsNextName ) + if( localNextName ) { // The actual name of the current node auto const actualName = itsIteratorStack.back().name(); // Do a search if we don't see a name coming up, or if the names don't match - if( !actualName || std::strcmp( itsNextName, actualName ) != 0 ) - itsIteratorStack.back().search( itsNextName ); + if( !actualName || std::strcmp( localNextName, actualName ) != 0 ) + itsIteratorStack.back().search( localNextName ); } - - itsNextName = nullptr; } public: