commit 305a712bbb1c201416e7bed2d8e6b7362c3aa5d5
Author: Enrico Forestieri <for...@lyx.org>
Date:   Tue Jun 30 18:54:19 2015 +0200

    Avoid using a dangling pointer
    
    This can happen when a macro is copied and then the document where
    it is defined is closed. In this case, the macro survives in the
    cut stack but the the buffer pointer is dangling.

diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp
index 78ea2bb..e772bc1 100644
--- a/src/mathed/MathMacro.cpp
+++ b/src/mathed/MathMacro.cpp
@@ -22,6 +22,7 @@
 #include "MathSupport.h"
 
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferView.h"
 #include "CoordCache.h"
 #include "Cursor.h"
@@ -207,7 +208,10 @@ MathMacro::MathMacro(MathMacro const & that)
                // We need to update d->macro_ by ourselves because in this case
                // MathData::metrics() is not called when selecting a math inset
                DocIterator const & pos = d->macroBackup_.pos();
-               d->macro_ = pos.buffer() ? pos.buffer()->getMacro(name(), pos) 
: 0;
+               Buffer const * buf = pos.buffer();
+               if (buf && !theBufferList().isLoaded(buf))
+                       buf = 0;
+               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
                if (!d->macro_)
                        d->macro_ = &d->macroBackup_;
        }
@@ -225,7 +229,10 @@ MathMacro & MathMacro::operator=(MathMacro const & that)
                // We need to update d->macro_ by ourselves because in this case
                // MathData::metrics() is not called when selecting a math inset
                DocIterator const & pos = d->macroBackup_.pos();
-               d->macro_ = pos.buffer() ? pos.buffer()->getMacro(name(), pos) 
: 0;
+               Buffer const * buf = pos.buffer();
+               if (buf && !theBufferList().isLoaded(buf))
+                       buf = 0;
+               d->macro_ = buf ? buf->getMacro(name(), pos) : 0;
                if (!d->macro_)
                        d->macro_ = &d->macroBackup_;
        }

Reply via email to