diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index e2caa19cf..19009dcf2 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -332,13 +332,14 @@ void ConsoleController::printText(const char * text, size_t length) { void ConsoleController::autoImportScript(Script script, bool force) { if (script.importationStatus() || force) { // Create the command "from scriptName import *". + assert(strlen(k_importCommand1) + strlen(script.name()) - strlen(ScriptStore::k_scriptExtension) + strlen(k_importCommand2) + 1 <= k_maxImportCommandSize); char command[k_maxImportCommandSize]; - size_t currentChar = strlcpy(command, k_importCommand1, strlen(k_importCommand1)+1); + size_t currentChar = strlcpy(command, k_importCommand1, k_maxImportCommandSize); const char * scriptName = script.name(); - currentChar += strlcpy(command+currentChar, scriptName, strlen(scriptName)+1); + currentChar += strlcpy(command+currentChar, scriptName, k_maxImportCommandSize - currentChar); // Remove the name extension ".py" currentChar -= strlen(ScriptStore::k_scriptExtension); - currentChar += strlcpy(command+currentChar, k_importCommand2, strlen(k_importCommand2)+1); + currentChar += strlcpy(command+currentChar, k_importCommand2, k_maxImportCommandSize - currentChar); runAndPrintForCommand(command); } if (force) { diff --git a/apps/code/console_store.cpp b/apps/code/console_store.cpp index 1d238f0e0..186ead226 100644 --- a/apps/code/console_store.cpp +++ b/apps/code/console_store.cpp @@ -4,6 +4,8 @@ namespace Code { +static inline int min(int x, int y) { return (xname()[0]; buffer[1] = '\''; double y = function->approximateDerivative(cursor->x(), app->localContext()); - numberOfChar += PoincareHelpers::ConvertFloatToText(y, buffer + legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits), Constant::ShortNumberOfSignificantDigits); - strlcpy(buffer+numberOfChar, space, spaceLength+1); + numberOfChar += PoincareHelpers::ConvertFloatToText(y, buffer + numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits), Constant::ShortNumberOfSignificantDigits); + strlcpy(buffer+numberOfChar, space, bufferSize - numberOfChar); buffer[k_maxDigitLegendLength+6] = 0; bannerView()->setLegendAtIndex(buffer, 2); } diff --git a/apps/graph/graph/intersection_graph_controller.cpp b/apps/graph/graph/intersection_graph_controller.cpp index edec549e7..06158dff3 100644 --- a/apps/graph/graph/intersection_graph_controller.cpp +++ b/apps/graph/graph/intersection_graph_controller.cpp @@ -21,18 +21,18 @@ const char * IntersectionGraphController::title() { void IntersectionGraphController::reloadBannerView() { m_bannerView->setNumberOfSubviews(2); reloadBannerViewForCursorOnFunction(m_cursor, m_function, 'x'); - char buffer[FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + size_t bufferSize = FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char buffer[bufferSize]; const char * space = " "; int spaceLength = strlen(space); const char * legend = "0(x)=0(x)="; int legendLength = strlen(legend); int numberOfChar = 0; - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); buffer[0] = m_function->name()[0]; buffer[5] = m_intersectedFunction->name()[0]; numberOfChar += PoincareHelpers::ConvertFloatToText(m_cursor->y(), buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); - strlcpy(buffer+numberOfChar, space, spaceLength+1); + strlcpy(buffer+numberOfChar, space, bufferSize - numberOfChar); buffer[FunctionBannerDelegate::k_maxDigitLegendLength+legendLength] = 0; bannerView()->setLegendAtIndex(buffer, 1); } diff --git a/apps/graph/graph/tangent_graph_controller.cpp b/apps/graph/graph/tangent_graph_controller.cpp index 7180963cf..2dcda7bc5 100644 --- a/apps/graph/graph/tangent_graph_controller.cpp +++ b/apps/graph/graph/tangent_graph_controller.cpp @@ -42,17 +42,16 @@ void TangentGraphController::reloadBannerView() { FunctionBannerDelegate::reloadBannerViewForCursorOnFunction(m_cursor, m_function, 'x'); TextFieldDelegateApp * myApp = (TextFieldDelegateApp *)app(); GraphControllerHelper::reloadDerivativeInBannerViewForCursorOnFunction(m_cursor, m_function, myApp); - char buffer[FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferSize = FunctionBannerDelegate::k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char buffer[bufferSize]; const char * legend = "a="; - int legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); + int legendLength = strlcpy(buffer, legend, bufferSize); double y = m_function->approximateDerivative(m_cursor->x(), myApp->localContext()); PoincareHelpers::ConvertFloatToText(y, buffer + legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); m_bannerView->setLegendAtIndex(buffer, 4); legend = "b="; - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); + legendLength = strlcpy(buffer, legend, bufferSize); y = -y*m_cursor->x()+m_function->evaluateAtAbscissa(m_cursor->x(), myApp->localContext()); PoincareHelpers::ConvertFloatToText(y, buffer + legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); m_bannerView->setLegendAtIndex(buffer, 5); diff --git a/apps/hardware_test/battery_test_controller.cpp b/apps/hardware_test/battery_test_controller.cpp index 62ff2fe7e..6ef8af451 100644 --- a/apps/hardware_test/battery_test_controller.cpp +++ b/apps/hardware_test/battery_test_controller.cpp @@ -40,26 +40,23 @@ void BatteryTestController::viewWillAppear() { } void BatteryTestController::updateBatteryState(float batteryLevel, bool batteryCharging) { - char bufferLevel[ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferLevelSize = ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char bufferLevel[bufferLevelSize]; const char * legend = "Battery level: "; - int legendLength = strlen(legend); - strlcpy(bufferLevel, legend, legendLength+1); + int legendLength = strlcpy(bufferLevel, legend, bufferLevelSize); PrintFloat::convertFloatToText(batteryLevel, bufferLevel+legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); m_view.batteryLevelTextView()->setText(bufferLevel); - char bufferCharging[ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferChargingSize = ContentView::k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char bufferCharging[bufferChargingSize]; int numberOfChars = 0; legend = "Battery charging: "; - legendLength = strlen(legend); - strlcpy(bufferCharging, legend, legendLength+1); - numberOfChars += legendLength; + numberOfChars += strlcpy(bufferCharging, legend, bufferChargingSize); legend = "no"; if (batteryCharging) { legend = "yes"; } - legendLength = strlen(legend); - strlcpy(bufferCharging+numberOfChars, legend, legendLength+1); - numberOfChars += legendLength; + numberOfChars += strlcpy(bufferCharging+numberOfChars, legend, bufferChargingSize); bufferCharging[numberOfChars] = 0; m_view.batteryChargingTextView()->setText(bufferCharging); } diff --git a/apps/probability/calculation_controller.cpp b/apps/probability/calculation_controller.cpp index 46a9f687a..f5395066b 100644 --- a/apps/probability/calculation_controller.cpp +++ b/apps/probability/calculation_controller.cpp @@ -274,11 +274,12 @@ void CalculationController::updateTitle() { int currentChar = 0; for (int index = 0; index < m_law->numberOfParameter(); index++) { m_titleBuffer[currentChar++] = I18n::translate(m_law->parameterNameAtIndex(index))[0]; - strlcpy(m_titleBuffer+currentChar, " = ", 4); + strlcpy(m_titleBuffer+currentChar, " = ", k_maxNumberOfTitleCharacters); currentChar += 3; - char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits)]; + const size_t bufferSize = PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits); + char buffer[bufferSize]; PrintFloat::convertFloatToText(m_law->parameterValueAtIndex(index), buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits), Constant::ShortNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); - strlcpy(m_titleBuffer+currentChar, buffer, strlen(buffer)+1); + strlcpy(m_titleBuffer+currentChar, buffer, bufferSize - currentChar); currentChar += strlen(buffer); m_titleBuffer[currentChar++] = ' '; } diff --git a/apps/regression/graph_controller.cpp b/apps/regression/graph_controller.cpp index 348cc0ebd..f1cd970e7 100644 --- a/apps/regression/graph_controller.cpp +++ b/apps/regression/graph_controller.cpp @@ -92,32 +92,25 @@ void GraphController::reloadBannerView() { } // Set point equals: "P(...) =" - char buffer[k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferSize = k_maxNumberOfCharacters + PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char buffer[bufferSize]; int numberOfChar = 0; const char * legend = " P("; - int legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); if (*m_selectedDotIndex == m_store->numberOfPairsOfSeries(*m_selectedSeriesIndex)) { legend = I18n::translate(I18n::Message::MeanDot); - legendLength = strlen(legend); - strlcpy(buffer+numberOfChar, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer+numberOfChar, legend, bufferSize - numberOfChar); } else if (*m_selectedDotIndex < 0) { legend = I18n::translate(I18n::Message::Reg); - legendLength = strlen(legend); - strlcpy(buffer+numberOfChar, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer+numberOfChar, legend, bufferSize - numberOfChar); } else { numberOfChar += PrintFloat::convertFloatToText(std::round((float)*m_selectedDotIndex+1.0f), buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::ShortNumberOfSignificantDigits), Constant::ShortNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); } legend = ") "; - legendLength = strlen(legend); - strlcpy(buffer+numberOfChar, legend, legendLength+1); + strlcpy(buffer+numberOfChar, legend, bufferSize - numberOfChar); buffer[k_maxLegendLength] = 0; m_bannerView.setLegendAtIndex(buffer, 0); - // Set "x=..." or "xmean=..." numberOfChar = 0; legend = "x="; @@ -128,9 +121,7 @@ void GraphController::reloadBannerView() { legend = legX; x = m_store->meanOfColumn(*m_selectedSeriesIndex, 0); } - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); numberOfChar += PoincareHelpers::ConvertFloatToText(x, buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); for (int i = numberOfChar; i < k_maxLegendLength; i++) { buffer[numberOfChar++] = ' '; @@ -147,9 +138,7 @@ void GraphController::reloadBannerView() { legend = legY; y = m_store->meanOfColumn(*m_selectedSeriesIndex, 1); } - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); numberOfChar += PoincareHelpers::ConvertFloatToText(y, buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); for (int i = numberOfChar; i < k_maxLegendLength; i++) { buffer[numberOfChar++] = ' '; @@ -194,9 +183,7 @@ void GraphController::reloadBannerView() { numberOfChar = 0; char leg[] = {' ', coefficientName, '=', 0}; legend = leg; - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); numberOfChar += PoincareHelpers::ConvertFloatToText(coefficients[i], buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits); buffer[k_maxLegendLength] = 0; m_bannerView.setLegendAtIndex(buffer, 4 + i); @@ -208,9 +195,7 @@ void GraphController::reloadBannerView() { numberOfChar = 0; legend = " r="; double r = m_store->correlationCoefficient(*m_selectedSeriesIndex); - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); numberOfChar += PoincareHelpers::ConvertFloatToText(r, buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits); buffer[k_maxLegendLength+10] = 0; m_bannerView.setLegendAtIndex(buffer, 6); @@ -219,9 +204,7 @@ void GraphController::reloadBannerView() { numberOfChar = 0; legend = " r2="; double r2 = m_store->squaredCorrelationCoefficient(*m_selectedSeriesIndex); - legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); - numberOfChar += legendLength; + numberOfChar += strlcpy(buffer, legend, bufferSize); numberOfChar += PoincareHelpers::ConvertFloatToText(r2, buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits); buffer[k_maxLegendLength] = 0; m_bannerView.setLegendAtIndex(buffer, 7); diff --git a/apps/sequence/sequence.cpp b/apps/sequence/sequence.cpp index 63aadc708..b457fdb50 100644 --- a/apps/sequence/sequence.cpp +++ b/apps/sequence/sequence.cpp @@ -29,10 +29,11 @@ Sequence::Sequence(const char * text, KDColor color) : } uint32_t Sequence::checksum() { - char data[k_dataLengthInBytes/sizeof(char)] = {}; - strlcpy(data, text(), TextField::maxBufferSize()); - strlcpy(data+TextField::maxBufferSize(), firstInitialConditionText(), TextField::maxBufferSize()); - strlcpy(data+2*TextField::maxBufferSize(), secondInitialConditionText(), TextField::maxBufferSize()); + constexpr size_t dataSize = k_dataLengthInBytes/sizeof(char); + char data[dataSize] = {}; + strlcpy(data, text(), dataSize); + strlcpy(data+TextField::maxBufferSize(), firstInitialConditionText(), dataSize - TextField::maxBufferSize()); + strlcpy(data+2*TextField::maxBufferSize(), secondInitialConditionText(), dataSize - 2*TextField::maxBufferSize()); int * intAdress = (int *)(&data[3*TextField::maxBufferSize()]); *intAdress = m_initialRank; data[k_dataLengthInBytes-3] = (char)m_type; diff --git a/apps/shared/function_banner_delegate.cpp b/apps/shared/function_banner_delegate.cpp index 404b2fe47..d735db1c1 100644 --- a/apps/shared/function_banner_delegate.cpp +++ b/apps/shared/function_banner_delegate.cpp @@ -7,17 +7,18 @@ using namespace Poincare; namespace Shared { void FunctionBannerDelegate::reloadBannerViewForCursorOnFunction(CurveViewCursor * cursor, Function * function, char symbol) { - char buffer[k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferSize = k_maxNumberOfCharacters+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char buffer[bufferSize]; const char * space = " "; int spaceLength = strlen(space); const char * legend = "0="; int legendLength = strlen(legend); int numberOfChar = 0; - strlcpy(buffer, legend, legendLength+1); + strlcpy(buffer, legend, bufferSize); numberOfChar += legendLength; buffer[0] = symbol; numberOfChar += PoincareHelpers::ConvertFloatToText(cursor->x(), buffer+numberOfChar, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); - strlcpy(buffer+numberOfChar, space, spaceLength+1); + strlcpy(buffer+numberOfChar, space, bufferSize - numberOfChar); buffer[k_maxDigitLegendLength+2] = 0; bannerView()->setLegendAtIndex(buffer, 0); @@ -25,11 +26,11 @@ void FunctionBannerDelegate::reloadBannerViewForCursorOnFunction(CurveViewCursor legend = "0(x)="; legendLength = strlen(legend); numberOfChar += legendLength; - strlcpy(buffer, legend, legendLength+1); + strlcpy(buffer, legend, bufferSize); buffer[2] = symbol; buffer[0] = function->name()[0]; numberOfChar += PoincareHelpers::ConvertFloatToText(cursor->y(), buffer+legendLength, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::MediumNumberOfSignificantDigits), Constant::MediumNumberOfSignificantDigits); - strlcpy(buffer+numberOfChar, space, spaceLength+1); + strlcpy(buffer+numberOfChar, space, bufferSize - numberOfChar); buffer[k_maxDigitLegendLength+5] = 0; bannerView()->setLegendAtIndex(buffer, 1); } diff --git a/apps/shared/sum_graph_controller.cpp b/apps/shared/sum_graph_controller.cpp index 97ed82b0a..01974e1e1 100644 --- a/apps/shared/sum_graph_controller.cpp +++ b/apps/shared/sum_graph_controller.cpp @@ -243,7 +243,8 @@ void SumGraphController::LegendView::setSumSymbol(Step step, double start, doubl EmptyLayout(EmptyLayoutNode::Color::Yellow, false, KDFont::SmallFont, false)); } else { m_sumLayout = LayoutHelper::String(sigma, sizeof(sigma)); - char buffer[2+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)]; + constexpr size_t bufferSize = 2+PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits); + char buffer[bufferSize]; PrintFloat::convertFloatToText(start, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); Layout start = LayoutHelper::String(buffer, strlen(buffer), KDFont::SmallFont); PrintFloat::convertFloatToText(end, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits, Preferences::PrintFloatMode::Decimal); @@ -252,7 +253,7 @@ void SumGraphController::LegendView::setSumSymbol(Step step, double start, doubl LayoutHelper::String(sigma, sizeof(sigma)), start, end); - strlcpy(buffer, "= ", 3); + strlcpy(buffer, "= ", bufferSize); PoincareHelpers::ConvertFloatToText(result, buffer+2, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits); m_sumLayout = HorizontalLayout( m_sumLayout, diff --git a/apps/statistics/histogram_controller.cpp b/apps/statistics/histogram_controller.cpp index f67769491..2154781e2 100644 --- a/apps/statistics/histogram_controller.cpp +++ b/apps/statistics/histogram_controller.cpp @@ -88,13 +88,14 @@ void HistogramController::reloadBannerView() { if (selectedSeriesIndex() < 0) { return; } - char buffer[k_maxNumberOfCharacters+ PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)*2]; + const size_t bufferSize = k_maxNumberOfCharacters+ PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)*2; + char buffer[bufferSize]; int numberOfChar = 0; // Add Interval Data const char * legend = " ["; int legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); + strlcpy(buffer, legend, bufferSize); numberOfChar += legendLength; // Add lower bound @@ -123,7 +124,7 @@ void HistogramController::reloadBannerView() { numberOfChar = 0; legend = ": "; legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); + strlcpy(buffer, legend, bufferSize); numberOfChar += legendLength; double size = 0; if (selectedSeriesIndex() >= 0) { @@ -141,7 +142,7 @@ void HistogramController::reloadBannerView() { numberOfChar = 0; legend = ": "; legendLength = strlen(legend); - strlcpy(buffer, legend, legendLength+1); + strlcpy(buffer, legend, bufferSize); numberOfChar += legendLength; if (selectedSeriesIndex() >= 0) { double frequency = size/m_store->sumOfOccurrences(selectedSeriesIndex()); diff --git a/apps/title_bar_view.cpp b/apps/title_bar_view.cpp index 67cf74185..0948b1ab2 100644 --- a/apps/title_bar_view.cpp +++ b/apps/title_bar_view.cpp @@ -82,18 +82,16 @@ void TitleBarView::layoutSubviews() { } void TitleBarView::refreshPreferences() { - char buffer[13]; + constexpr size_t bufferSize = 13; + char buffer[bufferSize]; int numberOfChar = 0; if (Preferences::sharedPreferences()->displayMode() == Preferences::PrintFloatMode::Scientific) { - strlcpy(buffer, I18n::translate(I18n::Message::Sci), strlen(I18n::translate(I18n::Message::Sci))+1); - numberOfChar += strlen(I18n::translate(I18n::Message::Sci)); + numberOfChar += strlcpy(buffer, I18n::translate(I18n::Message::Sci), bufferSize); } if (Preferences::sharedPreferences()->angleUnit() == Preferences::AngleUnit::Radian) { - strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Rad), strlen(I18n::translate(I18n::Message::Rad))+1); - numberOfChar += strlen(I18n::translate(I18n::Message::Rad)); + numberOfChar += strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Rad), bufferSize - numberOfChar); } else { - strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Deg), strlen(I18n::translate(I18n::Message::Sci))+1); - numberOfChar += strlen(I18n::translate(I18n::Message::Deg)); + numberOfChar += strlcpy(buffer+numberOfChar, I18n::translate(I18n::Message::Deg), bufferSize - numberOfChar); } buffer[numberOfChar] = 0; m_preferenceView.setText(buffer); diff --git a/escher/src/buffer_text_view.cpp b/escher/src/buffer_text_view.cpp index 5a456aed3..7974f2de0 100644 --- a/escher/src/buffer_text_view.cpp +++ b/escher/src/buffer_text_view.cpp @@ -23,6 +23,6 @@ void BufferTextView::appendText(const char * text) { size_t previousTextLength = strlen(m_buffer); size_t argTextLength = strlen(text); if (previousTextLength + argTextLength + 1 < k_maxNumberOfChar) { - strlcpy(&m_buffer[previousTextLength], text, argTextLength + 1); + strlcpy(&m_buffer[previousTextLength], text, k_maxNumberOfChar - previousTextLength); } } diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 19cd19ca5..295de63cb 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -99,7 +99,7 @@ bool TextField::ContentView::insertTextAtLocation(const char * text, int locatio for (int k = m_currentDraftTextLength; k >= location && k >= 0; k--) { m_draftTextBuffer[k+textSize] = m_draftTextBuffer[k]; } - strlcpy(&m_draftTextBuffer[location], text, textSize); + strlcpy(&m_draftTextBuffer[location], text, m_textBufferSize-location); if (location+textSize > 0) { m_draftTextBuffer[location+textSize-1] = text[textSize-1]; } diff --git a/poincare/src/symbol.cpp b/poincare/src/symbol.cpp index 21a236aae..bdf719636 100644 --- a/poincare/src/symbol.cpp +++ b/poincare/src/symbol.cpp @@ -122,7 +122,7 @@ int SymbolNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloat return -1; } if (bufferSize == 1) { - buffer[bufferSize-1] = 0; + buffer[0] = 0; return 0; } /* Special cases for all special symbols */