diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index da723264b..bb1d59410 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -14,9 +14,10 @@ extern "C" { #include "py/nlr.h" } - namespace Code { +static inline int minInt(int x, int y) { return x < y ? x : y; } + VariableBoxController::VariableBoxController(ScriptStore * scriptStore) : NestedMenuController(nullptr, I18n::Message::FunctionsAndVariables), m_scriptStore(scriptStore), @@ -369,10 +370,10 @@ bool VariableBoxController::selectLeaf(int rowIndex) { } void VariableBoxController::insertTextInCaller(const char * text, int textLength) { - int commandBufferMaxSize = strlen(text)+1; + int textLen = textLength < 0 ? strlen(text) : textLength; + int commandBufferMaxSize = minInt(k_maxScriptObjectNameSize, textLen + 1); char commandBuffer[k_maxScriptObjectNameSize]; - assert(commandBufferMaxSize <= k_maxScriptObjectNameSize); - Shared::ToolboxHelpers::TextToInsertForCommandText(text, textLength, commandBuffer, commandBufferMaxSize, true); + Shared::ToolboxHelpers::TextToInsertForCommandText(text, textLen, commandBuffer, commandBufferMaxSize, true); sender()->handleEventWithText(commandBuffer); } diff --git a/apps/shared/toolbox_helpers.cpp b/apps/shared/toolbox_helpers.cpp index 5cf13ce2a..8758ca763 100644 --- a/apps/shared/toolbox_helpers.cpp +++ b/apps/shared/toolbox_helpers.cpp @@ -62,6 +62,7 @@ void TextToInsertForCommandText(const char * command, int commandLength, char * index += UTF8Decoder::CodePointToChars(codePoint, buffer + index, bufferSize - index); } else { if (replaceArgsWithEmptyChar && !argumentAlreadyReplaced) { + assert(index < bufferSize); index += UTF8Decoder::CodePointToChars(UCodePointEmpty, buffer + index, bufferSize - index); argumentAlreadyReplaced = true; }