Guardsquare / proguard

ProGuard, Java optimizer and obfuscator
https://www.guardsquare.com/en/products/proguard
GNU General Public License v2.0
2.87k stars 409 forks source link

ProGuard introduces NPE into obfuscated code because it removes a null check #128

Open OlafRoeder opened 3 years ago

OlafRoeder commented 3 years ago

Hello folks,

I have a situation where a final variable in a subclass is initialized in its constructor, but the superclass calls a method accessing that variable before it is initialized. Works without obfuscation because of a null check, but this very check is removed by obfuscation. SSCCE: https://github.com/OlafRoeder/ProGuardNPEFinalVarWithSetterFromParent

This situation should be detectable by some static code analysis.

Would very much appreciate a fix, but a workaround that does not involve changing code would do, too.

Greetings, Olaf

EricLafortune commented 3 years ago

It's a known issue; such use before initialization unfortunately occurs once in a while and is not entirely trivial to detect. You can keep the affected fields so their non-null initialization values are not propagated, e.g.

-keepclassmembers,allowshrinking,allowobfuscation class somepackage.SomeClass {
    somepackage.SomeType someField;
}

Alternatively, you can disable value propagation for all fields:

-optimizations !field/propagation/value