Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 166c7217 authored by Sean McQuillan's avatar Sean McQuillan
Browse files

Don't crash after unsetting char[] in TextView

TextView.setText(char[]) is from API 1 and follows a running with
scissors API style of not copying the passed array.

To avoid a leak, in TextView.setText(String), the char[] would be nulled
out. However, an internal object could have been read using .getText()
prior to this second setText would immmediatly become a
CharSequence that crashed when you called any methods on it.

After this change, the CharWrapper will stay valid if had been
previously retrieved. The general shape of the API will be maintained.

Fixes: b/227218386
Test: atest android.widget.TextViewTest
Relnote: "Calling TextView.getText() after calling TextView.setText(char[])
will now return a valid CharSequence. The char[] pointed to by this char
sequnece may still be mutated by future calls to setText(char[]), but it
will no longer allow a (char[]) null to be set, which lead to crashes
when reading CharSequence returned from getText on TextView."

Change-Id: I35a2a76d58ec1946dace2f615cacf6a6085efdeb
parent bfd0db18
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -6456,9 +6456,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    public void setText(CharSequence text, BufferType type) {
        setText(text, type, true, 0);
        if (mCharWrapper != null) {
            mCharWrapper.mChars = null;
        }
        // drop any potential mCharWrappper leaks
        mCharWrapper = null;
    }
    @UnsupportedAppUsage
@@ -6672,11 +6671,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     * since the TextView has no way to know that the text
     * has changed and that it needs to invalidate and re-layout.
     *
     * @throws NullPointerException if text is null
     * @throws IndexOutOfBoundsException if start or start+len are not in 0 to text.length
     *
     * @param text char array to be displayed
     * @param start start index in the char array
     * @param len length of char count after {@code start}
     */
    public final void setText(char[] text, int start, int len) {
    public final void setText(/* @NonNull */ char[] text, int start, int len) {
        int oldlen = 0;
        if (start < 0 || len < 0 || start + len > text.length) {
@@ -13942,16 +13944,17 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    }
    private static class CharWrapper implements CharSequence, GetChars, GraphicsOperations {
        @NonNull
        private char[] mChars;
        private int mStart, mLength;
        public CharWrapper(char[] chars, int start, int len) {
        CharWrapper(@NonNull char[] chars, int start, int len) {
            mChars = chars;
            mStart = start;
            mLength = len;
        }
        /* package */ void set(char[] chars, int start, int len) {
        /* package */ void set(@NonNull char[] chars, int start, int len) {
            mChars = chars;
            mStart = start;
            mLength = len;
+17 −0
Original line number Diff line number Diff line
@@ -304,6 +304,23 @@ public class TextViewTest {
        assertFalse(mTextView.isCursorVisible());
    }

    @Test(expected = NullPointerException.class)
    @UiThreadTest
    public void setTextCharArrayNullThrows() {
        mTextView = new TextView(mActivity);
        mTextView.setText((char[]) null, 0, 0);
    }

    @Test
    @UiThreadTest
    public void setTextCharArrayValidAfterSetTextString() {
        mTextView = new TextView(mActivity);
        mTextView.setText(new char[] { 'h', 'i'}, 0, 2);
        CharSequence charWrapper = mTextView.getText();
        mTextView.setText("null out char wrapper");
        assertEquals("hi", charWrapper.toString());
    }

    private String createLongText() {
        int size = 600 * 1000;
        final StringBuilder builder = new StringBuilder(size);