From d29419bcd69432aded1df73cb74853db86daf20d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 28 Oct 2014 21:55:08 +0000 Subject: [PATCH] Fix invalid memory access when applying modifications on an empty buffer That can happen when undoing/redoing accross a buffer reload boundary. --- src/buffer.cc | 4 +++- src/buffer.inl.hh | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 81804c79..0b0dca1e 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -351,7 +351,9 @@ void Buffer::apply_modification(const Modification& modification) kak_assert(is_valid(coord)); // in modifications, end coords should be {line_count(), 0} - kak_assert(coord != ByteCoord(line_count()-1, m_lines.back().length())); + kak_assert((m_lines.empty() and coord == ByteCoord{0,0} ) or + coord != ByteCoord(line_count()-1, m_lines.back().length())); + switch (modification.type) { case Modification::Insert: diff --git a/src/buffer.inl.hh b/src/buffer.inl.hh index 0e82b7a9..7b09f1a4 100644 --- a/src/buffer.inl.hh +++ b/src/buffer.inl.hh @@ -68,7 +68,7 @@ inline bool Buffer::is_valid(ByteCoord c) const inline bool Buffer::is_end(ByteCoord c) const { - return c >= ByteCoord{line_count() - 1, m_lines.back().length()}; + return c >= end_coord(); } inline BufferIterator Buffer::begin() const @@ -78,9 +78,7 @@ inline BufferIterator Buffer::begin() const inline BufferIterator Buffer::end() const { - if (m_lines.empty()) - return BufferIterator(*this, { 0_line, 0 }); - return BufferIterator(*this, { line_count() - 1, m_lines.back().length() }); + return BufferIterator{*this, end_coord()}; } [[gnu::always_inline]] @@ -107,6 +105,8 @@ inline ByteCoord Buffer::back_coord() const inline ByteCoord Buffer::end_coord() const { + if (m_lines.empty()) + return { 0_line, 0 }; return { line_count() - 1, m_lines.back().length() }; }