]> git.lyx.org Git - features.git/commitdiff
Avoid using a dangling pointer
authorEnrico Forestieri <forenr@lyx.org>
Tue, 30 Jun 2015 16:54:19 +0000 (18:54 +0200)
committerEnrico Forestieri <forenr@lyx.org>
Tue, 30 Jun 2015 16:54:19 +0000 (18:54 +0200)
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.

src/mathed/MathMacro.cpp

index 78ea2bb3654913789464948ac5efeb8a89684fec..e772bc1b04eec024a6dceee9e0f2a2f950971af5 100644 (file)
@@ -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_;
        }