From 3bd809f0a58e02a707db6f57553a2a7672ae6eba Mon Sep 17 00:00:00 2001 From: Koji Ishii Date: Tue, 28 Feb 2023 17:25:38 +0000 Subject: [PATCH] [balance-text] Fix divide-by-zero when very narrow This patch fixes divide-by-zero error in `EstimateNumLines` when the available width is smaller than `1ch`. Bug: 1251079 Change-Id: I15888e6e8bbd0a0efb9dad5229dbef1050d668f9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4295483 Reviewed-by: Ian Kilpatrick Commit-Queue: Ian Kilpatrick Auto-Submit: Koji Ishii Cr-Commit-Position: refs/heads/main@{#1111030} --- .../core/layout/ng/inline/ng_paragraph_line_breaker.cc | 8 ++++++++ .../white-space/text-wrap-balance-narrow-crash.html | 2 ++ 2 files changed, 10 insertions(+) create mode 100644 third_party/blink/web_tests/external/wpt/css/css-text/white-space/text-wrap-balance-narrow-crash.html diff --git a/third_party/blink/renderer/core/layout/ng/inline/ng_paragraph_line_breaker.cc b/third_party/blink/renderer/core/layout/ng/inline/ng_paragraph_line_breaker.cc index 82264bda995053..b7c861e1f91ade 100644 --- a/third_party/blink/renderer/core/layout/ng/inline/ng_paragraph_line_breaker.cc +++ b/third_party/blink/renderer/core/layout/ng/inline/ng_paragraph_line_breaker.cc @@ -106,7 +106,15 @@ wtf_size_t EstimateNumLines(const String& text_content, const SimpleFontData* font, LayoutUnit available_width) { const float space_width = font->SpaceWidth(); + if (space_width <= 0) { + // Can't estimate without space glyph, go on to measure the actual value. + return 0; + } const wtf_size_t num_line_chars = available_width / space_width; + if (num_line_chars <= 0) { + // The width is too narrow, don't balance. + return std::numeric_limits::max(); + } return (text_content.length() + num_line_chars - 1) / num_line_chars; } diff --git a/third_party/blink/web_tests/external/wpt/css/css-text/white-space/text-wrap-balance-narrow-crash.html b/third_party/blink/web_tests/external/wpt/css/css-text/white-space/text-wrap-balance-narrow-crash.html new file mode 100644 index 00000000000000..dcc0d8773df6c7 --- /dev/null +++ b/third_party/blink/web_tests/external/wpt/css/css-text/white-space/text-wrap-balance-narrow-crash.html @@ -0,0 +1,2 @@ + +
A