From d937bc692b22103f8ef2c932f94590b4bb9cb2e6 Mon Sep 17 00:00:00 2001 From: Hugo Saint-Vignes Date: Thu, 13 Aug 2020 15:03:06 +0200 Subject: [PATCH] [apps/code] Fix crashes on full buffer Change-Id: If9297f1ca29015cad0cc1cdda3ad610fd1493392 --- apps/code/editor_controller.cpp | 6 +++--- apps/code/python_text_area.cpp | 8 ++++---- apps/code/script.h | 1 + ion/src/shared/storage.cpp | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index bee8bb5d9..39348a1d4 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -23,7 +23,7 @@ void EditorController::setScript(Script script, int scriptIndex) { m_script = script; m_scriptIndex = scriptIndex; - /* We edit the script direclty in the storage buffer. We thus put all the + /* We edit the script directly in the storage buffer. We thus put all the * storage available space at the end of the current edited script and we set * its size. * @@ -36,8 +36,8 @@ void EditorController::setScript(Script script, int scriptIndex) { * * */ - size_t newScriptSize = Ion::Storage::sharedStorage()->putAvailableSpaceAtEndOfRecord(m_script); - m_editorView.setText(const_cast(m_script.content()), newScriptSize - Script::StatusSize()); + Ion::Storage::sharedStorage()->putAvailableSpaceAtEndOfRecord(m_script); + m_editorView.setText(const_cast(m_script.content()), m_script.contentSize()); } void EditorController::willExitApp() { diff --git a/apps/code/python_text_area.cpp b/apps/code/python_text_area.cpp index e89e6005b..d0242debb 100644 --- a/apps/code/python_text_area.cpp +++ b/apps/code/python_text_area.cpp @@ -441,9 +441,7 @@ void PythonTextArea::addAutocompletion() { const int scriptIndex = m_contentView.pythonDelegate()->menuController()->editedScriptIndex(); m_contentView.pythonDelegate()->variableBoxController()->loadFunctionsAndVariables(scriptIndex, autocompletionTokenBeginning, autocompletionLocation - autocompletionTokenBeginning); - if (addAutocompletionTextAtIndex(0)) { - m_contentView.setAutocompleting(true); - } + addAutocompletionTextAtIndex(0); } bool PythonTextArea::addAutocompletionTextAtIndex(int nextIndex, int * currentIndexToUpdate) { @@ -468,6 +466,7 @@ bool PythonTextArea::addAutocompletionTextAtIndex(int nextIndex, int * currentIn return false; } autocompletionLocation += textToInsertLength; + m_contentView.setAutocompleting(true); m_contentView.setAutocompletionEnd(autocompletionLocation); } @@ -480,8 +479,9 @@ bool PythonTextArea::addAutocompletionTextAtIndex(int nextIndex, int * currentIn if (addParentheses && m_contentView.insertTextAtLocation(parentheses, const_cast(autocompletionLocation), parenthesesLength)) { m_contentView.setAutocompleting(true); m_contentView.setAutocompletionEnd(autocompletionLocation + parenthesesLength); + return true; } - return true; + return (textToInsertLength > 0); } void PythonTextArea::cycleAutocompletion(bool downwards) { diff --git a/apps/code/script.h b/apps/code/script.h index 9d2df78c6..6f7df9cda 100644 --- a/apps/code/script.h +++ b/apps/code/script.h @@ -49,6 +49,7 @@ public: bool autoImportationStatus() const; void toggleAutoimportationStatus(); const char * content() const; + size_t contentSize() { return value().size - k_statusSize; } /* Fetched status */ bool fetchedFromConsole() const; diff --git a/ion/src/shared/storage.cpp b/ion/src/shared/storage.cpp index 56fc333ea..2bcb3471e 100644 --- a/ion/src/shared/storage.cpp +++ b/ion/src/shared/storage.cpp @@ -99,6 +99,7 @@ void Storage::log() { size_t Storage::availableSize() { /* TODO maybe do: availableSize(char ** endBuffer) to get the endBuffer if it * is needed after calling availableSize */ + assert(k_storageSize >= (endBuffer() - m_buffer) + sizeof(record_size_t)); return k_storageSize-(endBuffer()-m_buffer)-sizeof(record_size_t); } @@ -479,7 +480,7 @@ bool Storage::isBaseNameWithExtensionTaken(const char * baseName, const char * e bool Storage::isNameOfRecordTaken(Record r, const Record * recordToExclude) { if (r == Record()) { /* If the CRC32 of fullName is 0, we want to refuse the name as it would - * interfere with our escape case in the Record contructor, when the given + * interfere with our escape case in the Record constructor, when the given * name is nullptr. */ return true; }