appsquickly / typhoon

Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
https://pilgrim.ph
Apache License 2.0
2.7k stars 269 forks source link

Typhoon crashes when its symbols are prefixed #546

Closed stnslw closed 7 years ago

stnslw commented 7 years ago

The issue occurs when symbol prefixing tool is used like described in the http://blog.sigmapoint.pl/avoiding-dependency-collisions-in-ios-static-library-managed-by-cocoapods/ It might also occur when using obfuscation tools.

The reason behind the crash is creation of objects using classes provided as strings, e.g.:

[self registerTypeConverter:[[TyphoonClassFromString(@"TyphoonUIColorTypeConverter") alloc] init]];

In this case class symbol gets prefixed but the string do not, which results in following crash:

2017-02-06 16:06:30.347 Typhoon-iOS[88470:2597541] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM removeObjectForKey:]: key cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c2eed4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010bd5021e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010c286de3 -[__NSDictionaryM removeObjectForKey:] + 675
    3   Typhoon-iOS                         0x0000000109522b63 -[__ONG_TyphoonTypeConverterRegistry registerTypeConverter:] + 595
    4   Typhoon-iOS                         0x0000000109523175 -[__ONG_TyphoonTypeConverterRegistry registerPlatformConverters] + 309
    5   Typhoon-iOS                         0x000000010952262f -[__ONG_TyphoonTypeConverterRegistry init] + 671
    6   Typhoon-iOS                         0x00000001095b9eff -[__ONG_TyphoonComponentFactory init] + 831
    7   Typhoon-iOS                         0x0000000109604e0a -[__ONG_TyphoonBlockComponentFactory initWithAssemblies:] + 282
    8   Typhoon-iOS                         0x0000000109604aec +[__ONG_TyphoonBlockComponentFactory factoryWithAssemblies:] + 300
    9   Typhoon-iOS                         0x00000001095d4491 +[__ONG_TyphoonStartup requireInitialFactory] + 977
    10  Typhoon-iOS                         0x00000001095d4aae __66+[__ONG_TyphoonStartup swizzleSetDelegateMethodOnApplicationClass]_block_invoke + 590
    11  UIKit                               0x0000000109fca375 _UIApplicationMainPreparations + 1747
    12  UIKit                               0x0000000109fc9c38 UIApplicationMain + 111
    13  Typhoon-iOS                         0x00000001095cf8ed main + 285
    14  libdyld.dylib                       0x000000010d37868d start + 1
    15  ???                                 0x0000000000000005 0x0 + 5
)
libc++abi.dylib: terminating with uncaught exception of type NSException

One way to fix it is to replace hardcoded string values with dynamically created ones, e.g.:

[self registerTypeConverter:[[TyphoonClassFromString(NSStringFromClass([TyphoonUIColorTypeConverter class])) alloc] init]];

Seems that those changes needs to be only done within registerPlatformConverters method of TyphoonTypeConverterRegistry since all Typhoon unit tests pass when prefixing is done when this method is fixed.

etolstoy commented 7 years ago

Thanks for your PR!