From 6311f408b453442eed50ff4d9bd5b1c039503c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 31 Mar 2020 15:41:04 +0200 Subject: [PATCH] [escher/text_area] Fix overlap in textInsert --- escher/src/text_area.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 8d4239df8..ab2162a63 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -237,11 +237,16 @@ void TextArea::Text::insertText(const char * s, int textLength, char * location) assert(m_buffer != nullptr); assert(location >= m_buffer && location < m_buffer + m_bufferSize - 1); assert(strlen(m_buffer) + textLength < m_bufferSize); + // assert the text to insert does not overlap the location where to insert + assert(s >= location || s + textLength < location); + /* The text to insert might be located after the insertion location, in which + * case we cannot simply do a memmove, as s will be shifted by the copy. */ + bool noShift = (s + textLength < location) || (s > m_buffer + m_bufferSize); size_t sizeToMove = strlen(location) + 1; assert(location + textLength + sizeToMove <= m_buffer + m_bufferSize); memmove(location + textLength, location, sizeToMove); - memmove(location, s, textLength); + memmove(location, s + (noShift ? 0 : textLength), textLength); } void TextArea::Text::insertSpacesAtLocation(int numberOfSpaces, char * location) {