diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index 12e2a9513..1da1a34f1 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -400,10 +400,12 @@ void ConsoleController::autoImportScript(Script script, bool force) { /* Copy the script name without the extension ".py". The '.' is overwritten * by the null terminating char. */ int copySizeWithNullTerminatingZero = minInt(k_maxImportCommandSize - currentChar, strlen(scriptName) - strlen(ScriptStore::k_scriptExtension)); - strlcpy(command+currentChar, scriptName, copySizeWithNullTerminatingZero); + assert(convertFloatToText >= 0); + strlcpy(command+currentChar, scriptName, ≈); currentChar += copySizeWithNullTerminatingZero-1; // Copy " import *" + assert(k_maxImportCommandSize >= currentChar); strlcpy(command+currentChar, k_importCommand2, k_maxImportCommandSize - currentChar); // Step 2 - Run the command diff --git a/apps/code/menu_controller.cpp b/apps/code/menu_controller.cpp index c9b163e1d..077bd6641 100644 --- a/apps/code/menu_controller.cpp +++ b/apps/code/menu_controller.cpp @@ -312,9 +312,9 @@ bool MenuController::textFieldDidFinishEditing(TextField * textField, const char // The user entered an empty name. Use a numbered default script name. bool foundDefaultName = Script::DefaultName(numberedDefaultName, Script::k_defaultScriptNameMaxSize); int defaultNameLength = strlen(numberedDefaultName); - assert(defaultNameLength < bufferSize); assert(UTF8Decoder::CharSizeOfCodePoint('.') == 1); numberedDefaultName[defaultNameLength++] = '.'; + assert(defaultNameLength < bufferSize); strlcpy(numberedDefaultName + defaultNameLength, ScriptStore::k_scriptExtension, bufferSize - defaultNameLength); /* If there are already scripts named script1.py, script2.py,... until * Script::k_maxNumberOfDefaultScriptNames, we want to write the last tried diff --git a/apps/graph/graph/graph_controller_helper.cpp b/apps/graph/graph/graph_controller_helper.cpp index 5a6011fa7..5fb387d31 100644 --- a/apps/graph/graph/graph_controller_helper.cpp +++ b/apps/graph/graph/graph_controller_helper.cpp @@ -25,9 +25,11 @@ void GraphControllerHelper::reloadDerivativeInBannerViewForCursorOnFunction(Shar const char * space = " "; int numberOfChar = function->derivativeNameWithArgument(buffer, bufferSize, CartesianFunction::Symbol()); const char * legend = "="; + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer+numberOfChar, legend, bufferSize-numberOfChar); double y = function->approximateDerivative(cursor->x(), App::app()->localContext()); numberOfChar += PoincareHelpers::ConvertFloatToText(y, buffer + numberOfChar, bufferSize-numberOfChar, Constant::ShortNumberOfSignificantDigits); + assert(numberOfChar <= bufferSize); strlcpy(buffer+numberOfChar, space, bufferSize-numberOfChar); bannerView()->derivativeView()->setText(buffer); bannerView()->reload(); diff --git a/apps/graph/graph/integral_graph_controller.cpp b/apps/graph/graph/integral_graph_controller.cpp index 21eb36701..feef0091a 100644 --- a/apps/graph/graph/integral_graph_controller.cpp +++ b/apps/graph/graph/integral_graph_controller.cpp @@ -41,6 +41,7 @@ Layout IntegralGraphController::createFunctionLayout(ExpiringPointernameWithArgument(buffer, bufferSize-strlen(dx), CartesianFunction::Symbol()); + assert(numberOfChars <= bufferSize); strlcpy(buffer+numberOfChars, dx, bufferSize-numberOfChars); return LayoutHelper::String(buffer, strlen(buffer), KDFont::SmallFont); } diff --git a/apps/graph/graph/intersection_graph_controller.cpp b/apps/graph/graph/intersection_graph_controller.cpp index 074944f4a..6b5f28fb5 100644 --- a/apps/graph/graph/intersection_graph_controller.cpp +++ b/apps/graph/graph/intersection_graph_controller.cpp @@ -25,12 +25,15 @@ void IntersectionGraphController::reloadBannerView() { // 'f(x)=g(x)=', keep 2 chars for '=' ExpiringPointer f = functionStore()->modelForRecord(m_record); int numberOfChar = f->nameWithArgument(buffer, bufferSize-2, CartesianFunction::Symbol()); + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer+numberOfChar, legend, bufferSize-numberOfChar); // keep 1 char for '='; ExpiringPointer g = functionStore()->modelForRecord(m_intersectedRecord); numberOfChar += g->nameWithArgument(buffer+numberOfChar, bufferSize-numberOfChar-1, CartesianFunction::Symbol()); + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer+numberOfChar, legend, bufferSize-numberOfChar); numberOfChar += PoincareHelpers::ConvertFloatToText(m_cursor->y(), buffer+numberOfChar, bufferSize-numberOfChar, Constant::MediumNumberOfSignificantDigits); + assert(numberOfChar <= bufferSize); strlcpy(buffer+numberOfChar, space, bufferSize-numberOfChar); bannerView()->ordinateView()->setText(buffer); bannerView()->reload(); diff --git a/apps/graph/list/list_controller.cpp b/apps/graph/list/list_controller.cpp index 9174738a5..ecd0babf5 100644 --- a/apps/graph/list/list_controller.cpp +++ b/apps/graph/list/list_controller.cpp @@ -56,10 +56,12 @@ bool ListController::textFieldDidFinishEditing(TextField * textField, const char // The user entered an empty name. Use a default function name. CartesianFunction::DefaultName(baseName, maxBaseNameSize); size_t defaultNameLength = strlen(baseName); + assert(defaultNameLength <= maxBaseNameSize); strlcpy(baseName + defaultNameLength, Function::k_parenthesedArgument, maxBaseNameSize - defaultNameLength); textField->setText(baseName); baseName[defaultNameLength] = 0; } else { + assert(argumentLength <= textLength + 1); strlcpy(baseName, text, textLength - argumentLength + 1); } diff --git a/apps/regression/graph_controller.cpp b/apps/regression/graph_controller.cpp index d7b4b60b1..6f3322980 100644 --- a/apps/regression/graph_controller.cpp +++ b/apps/regression/graph_controller.cpp @@ -111,14 +111,17 @@ void GraphController::reloadBannerView() { numberOfChar += strlcpy(buffer, legend, bufferSize); if (*m_selectedDotIndex == m_store->numberOfPairsOfSeries(*m_selectedSeriesIndex)) { legend = I18n::translate(I18n::Message::MeanDot); + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer + numberOfChar, legend, bufferSize - numberOfChar); } else if (*m_selectedDotIndex < 0) { legend = I18n::translate(I18n::Message::Reg); + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer + numberOfChar, legend, bufferSize - numberOfChar); } else { numberOfChar += PrintFloat::ConvertFloatToText(std::round((float)*m_selectedDotIndex+1.0f), buffer + numberOfChar, bufferSize - numberOfChar, Constant::ShortNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); } legend = ") "; + assert(numberOfChar <= bufferSize); strlcpy(buffer + numberOfChar, legend, bufferSize - numberOfChar); m_bannerView.dotNameView()->setText(buffer); diff --git a/apps/shared/function.cpp b/apps/shared/function.cpp index 8ded6d47e..21a76d388 100644 --- a/apps/shared/function.cpp +++ b/apps/shared/function.cpp @@ -68,10 +68,10 @@ int Function::nameWithArgument(char * buffer, size_t bufferSize, CodePoint arg) assert(UTF8Decoder::CharSizeOfCodePoint(arg) == 1); const char * functionName = fullName(); size_t baseNameLength = SymbolAbstract::TruncateExtension(buffer, functionName, bufferSize - k_parenthesedArgumentLength); + assert(baseNameLength <= bufferSize); size_t result = baseNameLength + strlcpy(&buffer[baseNameLength], k_parenthesedArgument, bufferSize-baseNameLength); - int bufferRemainingSize = bufferSize - (baseNameLength+1); - if (bufferRemainingSize > 0) { - UTF8Decoder::CodePointToChars(arg, buffer+baseNameLength+1, bufferRemainingSize); + if (baseNameLength + 1 < bufferSize) { + UTF8Decoder::CodePointToChars(arg, buffer+baseNameLength+1, bufferSize - (baseNameLength+1)); } return result; } diff --git a/apps/shared/function_banner_delegate.cpp b/apps/shared/function_banner_delegate.cpp index 5d81fc629..c5bf45089 100644 --- a/apps/shared/function_banner_delegate.cpp +++ b/apps/shared/function_banner_delegate.cpp @@ -13,16 +13,20 @@ void FunctionBannerDelegate::reloadBannerViewForCursorOnFunction(CurveViewCursor const char * space = " "; int numberOfChar = 0; buffer[numberOfChar++] = symbol; + assert(numberOfChar <= bufferSize); strlcpy(buffer + numberOfChar, "=", bufferSize - numberOfChar); bannerView()->abscissaSymbol()->setText(buffer); numberOfChar = PoincareHelpers::ConvertFloatToText(cursor->x(), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); + assert(numberOfChar <= bufferSize); strlcpy(buffer+numberOfChar, space, bufferSize - numberOfChar); bannerView()->abscissaValue()->setText(buffer); numberOfChar = function->nameWithArgument(buffer, bufferSize, symbol); + assert(numberOfChar <= bufferSize); numberOfChar += strlcpy(buffer+numberOfChar, "=", bufferSize-numberOfChar); numberOfChar += PoincareHelpers::ConvertFloatToText(cursor->y(), buffer+numberOfChar, bufferSize-numberOfChar, Constant::MediumNumberOfSignificantDigits); + assert(numberOfChar <= bufferSize); strlcpy(buffer+numberOfChar, space, bufferSize-numberOfChar); bannerView()->ordinateView()->setText(buffer); diff --git a/apps/title_bar_view.cpp b/apps/title_bar_view.cpp index b47c7f3a5..24ea593ae 100644 --- a/apps/title_bar_view.cpp +++ b/apps/title_bar_view.cpp @@ -88,6 +88,7 @@ void TitleBarView::refreshPreferences() { if (preferences->displayMode() == Preferences::PrintFloatMode::Scientific) { numberOfChar += strlcpy(buffer, I18n::translate(I18n::Message::Sci), bufferSize); } + assert(numberOfChar <= bufferSize); if (preferences->angleUnit() == Preferences::AngleUnit::Radian) { numberOfChar += strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Rad), bufferSize - numberOfChar); } else {