From 71a00189480c27406aa9693cbff7691d1e746df6 Mon Sep 17 00:00:00 2001 From: Mihai Popa Date: Tue, 25 Jun 2019 11:15:18 +0100 Subject: [PATCH] Fix Layout.primaryIsTrailingPreviousAllLineOffsets The CL fixes a crash in Layout.primaryIsTrailingPreviousAllLineOffsets. The crash was happening when the method was called for a line beginning with an empty bidi run. This could happen, for example, for empty text - I was unable to find any other case. The CL improves the existing test for the method with this case, which was previously crashing. The CL also fixes a potential crash in getLineHorizontals. However, this bug could never happen as in the current code path clamped is always false (and kept as parameter for parity with getHorizontal). Bug: 135444178 Test: atest FrameworksCoreTests:android.text.LayoutTest\#testPrimaryIsTrailingPrevious Change-Id: I47157abe1d74675884734e3810628a566e40c1b4 --- core/java/android/text/Layout.java | 9 ++++++--- core/tests/coretests/src/android/text/LayoutTest.java | 3 +++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/java/android/text/Layout.java b/core/java/android/text/Layout.java index bf0ef9465fcdc..2c2c2953ed514 100644 --- a/core/java/android/text/Layout.java +++ b/core/java/android/text/Layout.java @@ -1122,6 +1122,9 @@ public abstract class Layout { if (limit > lineEnd) { limit = lineEnd; } + if (limit == start) { + continue; + } level[limit - lineStart - 1] = (byte) ((runs[i + 1] >>> RUN_LEVEL_SHIFT) & RUN_LEVEL_MASK); } @@ -1220,8 +1223,8 @@ public abstract class Layout { } /** - * Computes in linear time the results of calling - * #getHorizontal for all offsets on a line. + * Computes in linear time the results of calling #getHorizontal for all offsets on a line. + * * @param line The line giving the offsets we compute information for * @param clamped Whether to clamp the results to the width of the layout * @param primary Whether the results should be the primary or the secondary horizontal @@ -1257,7 +1260,7 @@ public abstract class Layout { TextLine.recycle(tl); if (clamped) { - for (int offset = 0; offset <= wid.length; ++offset) { + for (int offset = 0; offset < wid.length; ++offset) { if (wid[offset] > mWidth) { wid[offset] = mWidth; } diff --git a/core/tests/coretests/src/android/text/LayoutTest.java b/core/tests/coretests/src/android/text/LayoutTest.java index 990161a88c227..93a6b15f744c3 100644 --- a/core/tests/coretests/src/android/text/LayoutTest.java +++ b/core/tests/coretests/src/android/text/LayoutTest.java @@ -743,6 +743,9 @@ public class LayoutTest { assertPrimaryIsTrailingPrevious( RTL + LRI + RTL + LTR + PDI + RTL, new boolean[]{false, false, true, false, false, false, false}); + assertPrimaryIsTrailingPrevious( + "", + new boolean[]{false}); } }