Fix invalid memory access when applying modifications on an empty buffer

That can happen when undoing/redoing accross a buffer reload
boundary.
This commit is contained in:
Maxime Coste 2014-10-28 21:55:08 +00:00
parent 62f56378c9
commit d29419bcd6
2 changed files with 7 additions and 5 deletions

View File

@ -351,7 +351,9 @@ void Buffer::apply_modification(const Modification& modification)
kak_assert(is_valid(coord)); kak_assert(is_valid(coord));
// in modifications, end coords should be {line_count(), 0} // 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) switch (modification.type)
{ {
case Modification::Insert: case Modification::Insert:

View File

@ -68,7 +68,7 @@ inline bool Buffer::is_valid(ByteCoord c) const
inline bool Buffer::is_end(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 inline BufferIterator Buffer::begin() const
@ -78,9 +78,7 @@ inline BufferIterator Buffer::begin() const
inline BufferIterator Buffer::end() const inline BufferIterator Buffer::end() const
{ {
if (m_lines.empty()) return BufferIterator{*this, end_coord()};
return BufferIterator(*this, { 0_line, 0 });
return BufferIterator(*this, { line_count() - 1, m_lines.back().length() });
} }
[[gnu::always_inline]] [[gnu::always_inline]]
@ -107,6 +105,8 @@ inline ByteCoord Buffer::back_coord() const
inline ByteCoord Buffer::end_coord() const inline ByteCoord Buffer::end_coord() const
{ {
if (m_lines.empty())
return { 0_line, 0 };
return { line_count() - 1, m_lines.back().length() }; return { line_count() - 1, m_lines.back().length() };
} }