AAkira / Napier

Logging library for Kotlin Multiplatform
Apache License 2.0
820 stars 36 forks source link

ArrayIndexOutOfBoundsException in performTag method for Android #122

Open UbadahJ opened 10 months ago

UbadahJ commented 10 months ago

I am experiencing a recurring crash in my application, which seems to be related to an ArrayIndexOutOfBoundsException. The stack trace is as follows:

Exception java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
    at io.github.aakira.napier.DebugAntilog.performTag (DebugAntilog.java:78)
    at io.github.aakira.napier.DebugAntilog.performLog (DebugAntilog.java:78)
    at io.github.aakira.napier.Antilog.rawLog$napier_release (Antilog.java:78)
    at io.github.aakira.napier.Napier.rawLog (Napier.kt:78)
    at io.github.aakira.napier.Napier.log (Napier.kt:78)
    at io.github.aakira.napier.Napier.e (Napier.kt:136)
    at io.github.aakira.napier.Napier.e$default (Napier.java:136)

Code Inspection: Upon reviewing the related code, I noticed the potential cause in the performTag function:

private fun performTag(tag: String): String {
    val thread = Thread.currentThread().stackTrace

    return if (thread.size >= CALL_STACK_INDEX) {
        thread[CALL_STACK_INDEX].run {
            "${createStackElementTag(className)}\$$methodName"
        }
    } else {
        tag
    }
}

Possible Solution: It appears that replacing thread.size >= CALL_STACK_INDEX with thread.size > CALL_STACK_INDEX could resolve the issue.

Question: Is this indeed a bug, or am I missing some other aspect of the implementation that could be causing this error?

robertvoigt030 commented 5 months ago
  thread[CALL_STACK_INDEX].run { ....

As it is private const val CALL_STACK_INDEX = 9 for Android Module and all other modules it is private const val CALL_STACK_INDEX = 8. So simply editing the constant CALL_STACK_INDEX to the value 8 for Android / Java module should fix this crash.

LambergaR commented 2 months ago

@AAkira is this getting fixed? :)

Endless-Lemniscate commented 1 month ago

Faced with same issue. Any updates?

OSemenovBoyarka commented 2 weeks ago

Same issue for me, but it reproduces only for minified version of the app.

LambergaR commented 2 weeks ago

well, this was a complete waste of another couple of hours ...

this can also happen on the .d method call, at least in the minified version

robertvoigt030 commented 2 weeks ago

Still the same...any updates here?