diff options
| author | Sean McQuillan <seanmcq@google.com> | 2022-07-12 13:55:44 -0700 |
|---|---|---|
| committer | Sean McQuillan <seanmcq@google.com> | 2022-07-19 20:38:10 +0000 |
| commit | 81cde0b5847674eb060d4e97f8e6cc9081e1e520 (patch) | |
| tree | 22d5aa12aa0193872cb37158328df75505830868 /core/java/android/widget/TextView.java | |
| parent | 3ca7a5ab25d62cf8137d7234ecb50e6f862c1051 (diff) | |
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."
Merged-In: I35a2a76d58ec1946dace2f615cacf6a6085efdeb
Change-Id: I35a2a76d58ec1946dace2f615cacf6a6085efdeb
(cherry picked from commit 166c7217f45d312f026ea2be94101e7b39b6a2c5)
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 23393ffe885c..2268bef2c1d9 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -6440,9 +6440,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 @@ -6653,11 +6652,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) { @@ -13888,16 +13890,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; |
