From 305a712bbb1c201416e7bed2d8e6b7362c3aa5d5 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Tue, 30 Jun 2015 18:54:19 +0200 Subject: [PATCH] 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. --- src/mathed/MathMacro.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/mathed/MathMacro.cpp b/src/mathed/MathMacro.cpp index 78ea2bb365..e772bc1b04 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_; } -- 2.39.2