javiersantos / AppUpdater

A library that checks for your apps' updates on Google Play, GitHub, Amazon, F-Droid or your own server. API 9+ required.
Apache License 2.0
1.98k stars 412 forks source link

Never let UtilsAsync.LatestAppVersion async job crash the app #106

Closed tdtran closed 6 years ago

tdtran commented 6 years ago

I got crash reports like this from the field

Fatal Exception: java.lang.RuntimeException: An error occured while executing doInBackground()
       at android.os.AsyncTask$3.done(AsyncTask.java:299)
       at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
       at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
       at java.util.concurrent.FutureTask.run(FutureTask.java:239)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
       at java.lang.Thread.run(Thread.java:838)
Caused by java.lang.ArrayIndexOutOfBoundsException: src.length=0 srcPos=0 dst.length=23197 dstPos=0 length=18184
       at java.lang.System.arraycopy(System.java)
       at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
       at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
       at java.lang.StringBuilder.append(StringBuilder.java:271)
       at java.io.BufferedReader.readLine(BufferedReader.java:417)
       at com.github.javiersantos.appupdater.UtilsLibrary.getLatestAppVersionHttp(SourceFile:151)
       at com.github.javiersantos.appupdater.UtilsAsync$LatestAppVersion.doInBackground(SourceFile:83)
       at com.github.javiersantos.appupdater.UtilsAsync$LatestAppVersion.doInBackground(SourceFile:15)
       at android.os.AsyncTask$2.call(AsyncTask.java:287)
       at java.util.concurrent.FutureTask.run(FutureTask.java:234)
       at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
       at java.lang.Thread.run(Thread.java:838)

I suspect there are something very wrong with the user device but nonetheless AppUpdater should never ever crash. In this case it crashes in the async task spawned by the library itself. The app developer has no chance to catch and handle the unchecked exception.

AppUpdater should be super-defensive. This PR should achieve it.