1

Crashlytics has reported about the crash in my app:

Code:

asciiEncoder = Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1
...
public static boolean isPureAscii(String v) {
    return asciiEncoder.canEncode(v);
}

Error:

Fatal Exception: java.lang.IllegalArgumentException: ucnv_fromUnicode failed: U_ILLEGAL_ARGUMENT_ERROR
       at libcore.icu.NativeConverter.encode(NativeConverter.java)
       at java.nio.charset.CharsetEncoderICU.implFlush(CharsetEncoderICU.java:140)
       at java.nio.charset.CharsetEncoder.flush(CharsetEncoder.java:464)
       at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:266)
       at java.nio.charset.CharsetEncoder.canEncode(CharsetEncoder.java:202)
       at CorrectLocationUtfTokenizable.isPureAscii(CorrectLocationUtfTokenizable.java:38)

Device: ASUS T00J, Android OS: 5.0

The string to fail on is unknown, but it's not null.

Any thoughts?

4ntoine
  • 19,816
  • 21
  • 96
  • 220

1 Answers1

0

Probably the ASUS T00J implementation of this libcore.icu.NativeConverter has a bug on this edge case. You can try to catch RuntimeException inside isPureAscii(), and return false of anything is thrown.
But Probably a cheaper method will work better:

public static boolean isPureAscii(String v) {
     return v==null? false: v.matches("\\A\\p{ASCII}*\\z");
}

(credit)

Community
  • 1
  • 1
Amir Uval
  • 14,425
  • 4
  • 50
  • 74