From 89290a0c461ba727bab2c34708948475605c9e3f Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 1 Nov 2017 11:26:01 -0700 Subject: [PATCH] Validate UTF-16 input in ParagraphBuilder::addText (flutter/engine#4300) Fixes https://github.com/flutter/flutter/issues/12772 --- engine/src/flutter/lib/ui/text.dart | 7 ++++++- .../flutter/lib/ui/text/paragraph_builder.cc | 21 ++++++++++++++++--- .../flutter/lib/ui/text/paragraph_builder.h | 2 +- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/lib/ui/text.dart b/engine/src/flutter/lib/ui/text.dart index 71cd5ed6cf..8cd6a447ec 100644 --- a/engine/src/flutter/lib/ui/text.dart +++ b/engine/src/flutter/lib/ui/text.dart @@ -970,7 +970,12 @@ class ParagraphBuilder extends NativeFieldWrapperClass2 { /// Adds the given text to the paragraph. /// /// The text will be styled according to the current stack of text styles. - void addText(String text) native "ParagraphBuilder_addText"; + void addText(String text) { + String error = _addText(text); + if (error != null) + throw new ArgumentError(error); + } + String _addText(String text) native "ParagraphBuilder_addText"; /// Applies the given paragraph style and returns a [Paragraph] containing the /// added text and associated styling. diff --git a/engine/src/flutter/lib/ui/text/paragraph_builder.cc b/engine/src/flutter/lib/ui/text/paragraph_builder.cc index 9b5b3b37fa..469f2ca939 100644 --- a/engine/src/flutter/lib/ui/text/paragraph_builder.cc +++ b/engine/src/flutter/lib/ui/text/paragraph_builder.cc @@ -417,19 +417,34 @@ void ParagraphBuilder::pop() { } } -void ParagraphBuilder::addText(const std::string& text) { +Dart_Handle ParagraphBuilder::addText(const std::u16string& text) { + if (text.empty()) + return Dart_Null(); + + // Use ICU to validate the UTF-16 input. Calling u_strToUTF8 with a null + // output buffer will return U_BUFFER_OVERFLOW_ERROR if the input is well + // formed. + const UChar* text_ptr = reinterpret_cast(text.data()); + UErrorCode error_code = U_ZERO_ERROR; + u_strToUTF8(nullptr, 0, nullptr, text_ptr, text.size(), &error_code); + if (error_code != U_BUFFER_OVERFLOW_ERROR) + return tonic::ToDart("string is not well-formed UTF-16"); + if (!Settings::Get().using_blink) { m_paragraphBuilder->AddText(text); } else { // Blink Version. if (!m_currentRenderObject) - return; - RenderText* renderText = new RenderText(String::fromUTF8(text).impl()); + return tonic::ToDart("paragraph has already been built"); + RenderText* renderText = + new RenderText(String(text_ptr, text.size()).impl()); RefPtr style = RenderStyle::create(); style->inheritFrom(m_currentRenderObject->style()); renderText->setStyle(style.release()); m_currentRenderObject->addChild(renderText); } + + return Dart_Null(); } fxl::RefPtr ParagraphBuilder::build() { diff --git a/engine/src/flutter/lib/ui/text/paragraph_builder.h b/engine/src/flutter/lib/ui/text/paragraph_builder.h index 091c6f1f21..ceb8c3a5de 100644 --- a/engine/src/flutter/lib/ui/text/paragraph_builder.h +++ b/engine/src/flutter/lib/ui/text/paragraph_builder.h @@ -44,7 +44,7 @@ class ParagraphBuilder : public fxl::RefCountedThreadSafe, void pop(); - void addText(const std::string& text); + Dart_Handle addText(const std::u16string& text); fxl::RefPtr build();