commit c4658f046fc929d557331040ca592c7785378ba1
Author: Richard Kimberly Heck <[email protected]>
Date:   Mon May 27 00:06:10 2019 -0400

    Fix crash reported by Kornel.
    
    See https://marc.info/?l=lyx-devel&m=155879185229073&w=2.
    
    The problem is that, after saving the document and reloading, the
    TOC is corrupted, more or less, when we run through updateBuffer.
    So we reset it first.
---
 src/Buffer.cpp     |    4 ++++
 src/TocBackend.cpp |   21 ++++++++++++++-------
 src/TocBackend.h   |    2 ++
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index 6665a73..848f1f4 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -4980,6 +4980,10 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType 
utype) const
        setChangesPresent(false);
 
        Buffer & cbuf = const_cast<Buffer &>(*this);
+       // if we are reloading, then we could have a dangling TOC,
+       // in effect. so we need to go ahead and reset, even though
+       // we will do so again when we rebuild the TOC later.
+       cbuf.tocBackend().reset();
 
        // do the real work
        ParIterator parit = cbuf.par_iterator_begin();
diff --git a/src/TocBackend.cpp b/src/TocBackend.cpp
index a253d57..65f96ba 100644
--- a/src/TocBackend.cpp
+++ b/src/TocBackend.cpp
@@ -204,17 +204,24 @@ bool TocBackend::updateItem(DocIterator const & dit_in)
 }
 
 
-void TocBackend::update(bool output_active, UpdateType utype)
+void TocBackend::reset()
 {
-       for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
-               it->second->clear();
+       for (auto const & t: tocs_)
+               t.second->clear();
        tocs_.clear();
        builders_.clear();
        resetOutlinerNames();
-       if (!buffer_->isInternal()) {
-               DocIterator dit;
-               buffer_->inset().addToToc(dit, output_active, utype, *this);
-       }
+}
+
+
+void TocBackend::update(bool output_active, UpdateType utype)
+{
+       reset();
+       if (buffer_->isInternal())
+               return;
+
+       DocIterator dit;
+       buffer_->inset().addToToc(dit, output_active, utype, *this);
 }
 
 
diff --git a/src/TocBackend.h b/src/TocBackend.h
index a38a972..0e123ee 100644
--- a/src/TocBackend.h
+++ b/src/TocBackend.h
@@ -113,6 +113,8 @@ public:
        void setBuffer(Buffer const * buffer) { buffer_ = buffer; }
        ///
        void update(bool output_active, UpdateType utype);
+       ///
+       void reset();
        /// \return true if the item was updated.
        bool updateItem(DocIterator const & pit);
        ///

Reply via email to