11package org.wordpress.aztec
22
3+ import org.wordpress.android.util.AppLog
34import org.xml.sax.Attributes
45import org.xml.sax.helpers.AttributesImpl
56
67class AztecAttributes (attributes : Attributes = AttributesImpl ()) : AttributesImpl(attributes) {
78 fun setValue (key : String , value : String ) {
89 val index = getIndex(key)
10+
911 if (index == - 1 ) {
10- addAttribute(" " , key, key, " string" , value)
12+ try {
13+ addAttribute(" " , key, key, " string" , value)
14+ } catch (e: ArrayIndexOutOfBoundsException ) {
15+ // https://github.com/wordpress-mobile/AztecEditor-Android/issues/705
16+ AppLog .e(AppLog .T .EDITOR , " Error adding attribute with name: $key and value: $value " )
17+ logInternalState()
18+ throw e
19+ }
1120 } else {
1221 setValue(index, value)
1322 }
1423 }
1524
25+ private fun logInternalState () {
26+ AppLog .e(AppLog .T .EDITOR , " Dumping internal state:" )
27+ AppLog .e(AppLog .T .EDITOR , " length = $length " )
28+ // Since the toString can throw OOB error we need to wrap it in a try/catch
29+ try {
30+ AppLog .e(AppLog .T .EDITOR , toString())
31+ } catch (e: ArrayIndexOutOfBoundsException ) {
32+ // No need to log anything here. `toString` already writes to log details, but we need to shallow the exception
33+ // we don't want to crash logging state of the app
34+ }
35+ }
36+
1637 fun isEmpty (): Boolean {
1738 return length == 0
1839 }
1940
2041 fun removeAttribute (key : String ) {
2142 if (hasAttribute(key)) {
2243 val index = getIndex(key)
23- removeAttribute(index)
44+ try {
45+ removeAttribute(index)
46+ } catch (e: ArrayIndexOutOfBoundsException ) {
47+ // https://github.com/wordpress-mobile/AztecEditor-Android/issues/705
48+ AppLog .e(AppLog .T .EDITOR , " Tried to remove attribute: $key that is not in the list" )
49+ AppLog .e(AppLog .T .EDITOR , " Reported to be at index: $index " )
50+ logInternalState()
51+ throw e
52+ }
2453 }
2554 }
2655
@@ -30,12 +59,20 @@ class AztecAttributes(attributes: Attributes = AttributesImpl()) : AttributesImp
3059
3160 override fun toString (): String {
3261 val sb = StringBuilder ()
33- for (i in 0 .. this .length - 1 ) {
34- sb.append(this .getLocalName(i))
35- sb.append(" =\" " )
36- sb.append(this .getValue(i))
37- sb.append(" \" " )
62+ try {
63+ for (i in 0 .. this .length - 1 ) {
64+ sb.append(this .getLocalName(i))
65+ sb.append(" =\" " )
66+ sb.append(this .getValue(i))
67+ sb.append(" \" " )
68+ }
69+ } catch (e: ArrayIndexOutOfBoundsException ) {
70+ // https://github.com/wordpress-mobile/AztecEditor-Android/issues/705
71+ AppLog .e(AppLog .T .EDITOR , " IOOB occurred in toString. Dumping partial state:" )
72+ AppLog .e(AppLog .T .EDITOR , sb.trimEnd().toString())
73+ throw e
3874 }
75+
3976 return sb.trimEnd().toString()
4077 }
4178}
0 commit comments