NSData AES trigger unrecognized selector sent to instance error - ios

I use a AES library from github NSData+AES by nicerobot's objc
My codes are below :
-- AESLib.h--
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCrypto.h>
#interface AESLib : NSObject
-(NSData *) encodeAES:(NSData *) argSource;
#end
-- AESLib.m --
-(NSData *) encodeAES:(NSData *) argSource
{
NSString APIV1_AES_KEY = #"//AAAAAAAAAAAAAAAAAAAAAA"; // 24 char
NSString APIV1_AES_IV = #"//BBBBBBBBBBBBBB"; // 16 char
NSData *key = [NSData dataWithBytes:APIV1_AES_KEY.UTF8String length:strlen(APIV1_AES_KEY.UTF8String)];
NSData *iv = [NSData dataWithBytes:APIV1_AES_IV.UTF8String length:strlen(APIV1_AES_IV.UTF8String)];
NSData *encData;
#try {
encData = [argSource encrypt:key withInitial:iv andPadding:kCCOptionPKCS7Padding];
// Error Here
return encData;
}
#catch (NSException *exception) {
NSLog(#"Exception : %#", [exception reason]);
}
return nil;
}
//Error
> XCode trigger error 2013-10-22 16:15:58.206 Aessample[2913:c07]
> -[NSConcreteMutableData encrypt:withInitial:andPadding:]: unrecognized selector sent to instance 0x7523300 2013-10-22 16:15:58.207
> Aessample[2913:c07] Exception : -[NSConcreteMutableData
> encrypt:withInitial:andPadding:]: unrecognized selector sent to
> instance 0x7523300
Why this error happen?
--- Add to NSMutableString
Thank you NSMutableString.
I did tried again likewise your answers, but my code triggers still same errors.
Help again NSMutableString plz.

Delete the category files and add them again checking the target.

Related

App crash - when installed from App store but not locally

When i test my app as DEBUG then it works. But when i release to App store and from App Store install the app then it crash.
Therefore i changed the DEBUG RUN method into RELEASE run method for testing.
When i now run as RELEASE run method, then the app crash too (same as APP store Crash). The app crash is pointing at line 19.
How do i fix it? here i am telling to make an DNS (A or AAAA record) self.server = [self.server stringByAppendingString:#".example.com"]; by joining one string with the string i have from line 18.
- (void)myMethodHere:(NSString *)a {
//where a = #"splitme://test1/test2/test3";
NSArray *work_array = [a componentsSeparatedByString:#"splitme://"];
self.use_url =[work_array objectAtIndex:1];
if ([self.use_url length] > 0) {
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
self.server = [work_array objectAtIndex:0];
// -------------------
// CRASH HERE // CRASH HERE
// -------------------
if([self.server length] > 0) {
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 1: %#", self.server); // WORKS
// Here we go CRASHing...
self.server = [self.server stringByAppendingString:#".example.com"];
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 2: %#", self.server); // CRASH CRASH
}
else {
self.server = #"test.example.com";
}
}
else {
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 9: %#", self.use_url);
}
[self load_later];
}
EDIT 1: Following way of doing let me pass but if i have again self.server outside of this scope then app crash again.
NSString *part1 = self.server;
NSString *part2 = #".example.com";
NSString *result =[part1 stringByAppendingString:part2];
self.server = result;
NSLog(#"!!!!!!!!!!!!!!!!!!!!!! 4 - part 2: %#", self.server);
EDIT 2: crashing on AppDelegate.m, after commenting out the NSLog lines.
2017-04-08 10:32:01.773648+0200 app[344:18271] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-04-08 10:32:01.774146+0200 app[344:18271] [MC] Reading from public effective user settings.
2017-04-08 10:32:01.778647+0200 app[344:18271] -[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
2017-04-08 10:32:01.778888+0200 app[344:18271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0'
*** First throw call stack:
(0x18a956fd8 0x1893b8538 0x18a95def4 0x18a95af4c 0x18a856d2c 0x100006f78 0x1000076e8 0x1000066cc 0x190a85f9c 0x190a85b78 0x190a8c3f8 0x190a89894 0x190afb2fc 0x190d038b4 0x190d092a8 0x190d1dde0 0x190d0653c 0x18c4ff884 0x18c4ff6f0 0x18c4ffaa0 0x18a905424 0x18a904d94 0x18a9029a0 0x18a832d94 0x190af045c 0x190aeb130 0x100007d78 0x18984159c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
EDIT 3: Crashing NSString not NSString even with explicit cast
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"!!!!!!!!!!!lol!!!!!!!!!!!!! Bug! Set breakpoint here!");
}
self.server = value;
}
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
NSString *setServerURL =(NSString *) [work_array objectAtIndex:0];
[self setServer:setServerURL];
EDIT 4: Crashing
- (void)myMethodHere:(NSString *)a {
NSArray *work_array = [a componentsSeparatedByString:#"test://"];
id objectOne = [work_array objectAtIndex:1];
if ([objectOne isKindOfClass:[NSString class] ] ) {
NSString *objectOneIntoString = objectOne;
self.use_url = (NSString *) objectOneIntoString;
}
if ([self.use_url length] > 0) {
NSLog(#"3: %#", self.use_url);
NSArray *work_array = [self.use_url componentsSeparatedByString:#"/"];
id objectTwo = [work_array objectAtIndex:0];
if ([objectTwo isKindOfClass:[NSString class] ]) {
NSString *objectTwoIntoString = objectTwo;
self.server = objectTwoIntoString;
}
if([self.server length] > 0) {
NSLog(#"4 - part 1: %#", self.server);
NSString *part1 = self.server;
NSString *part2 = #".example.com";
NSString *result = (NSString *) [part1 stringByAppendingString:part2];
self.server = result;
NSLog(#"4 - part 2: %#", self.server);
}
else {
self.server = #"dns.example.com";
NSLog(#"5:%#", self.server);
}
NSLog(#"8: %#", self.server);
}
else {
NSLog(#"9: %#", self.use_url);
}
NSLog(#"10: %#", self.use_url);
}
The error message in your pastebin is:
2017-04-08 10:32:01.778647+0200 app[344:18271] -[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
2017-04-08 10:32:01.778888+0200 app[344:18271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0'
This tells you that there was an array on which you called length. So if it really is related to your server property then you are accidentally storing an NSArray there somewhere.
Here's an idea on how to catch that: implement the setServer: method and check whether the type is correct.
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"Bug! Set breakpoint here!");
}
_server = value;
}
Edit: You implemented your setter incorrectly and would normally have produced an endless recursion. So let's start from scratch:
I assume you have a property defined like this:
#property (strong) NSString * server; // Or maybe "copy"
If your property does not look like this you need to tell us!
Then you implement the setter exactly like this:
- (void)setServer:(NSString *)value
{
if (value != nil && ![value isKindOfClass:[NSString class]]) {
NSLog(#"Bug! Set breakpoint here!");
}
_server = value;
// DO NOT DO self.server = value HERE!
}
Then you can just do self.server = blabla; in the rest of your code and if your code sets something that is not a string the NSLog will be hit. So set a breakpoint there!
Edit 2:
Now I've seen in another comment of yours what's wrong…
#property (assign, nonatomic) NSString *server;
The assign does not retain the object! You need to use strong or (in the case if NSString *) copy. As it is, the following will happen:
You assign an object to server.
Since it is not retained, the object is deallocated soon.
But server still has a pointer to that memory location.
Two things can happen now:
Either a new, different object is created at memory location before you access the property. You now get an unexpected object back. Calling methods on this object may work or it may crash since the object does not know the method.
Or the memory location is deemed invalid and you get a crash right away.
Never use assign with objects!
-[__NSArrayM length]: unrecognized selector sent to instance 0x1740457c0
self.server class is NSArray .must string length

Unrecognized selector which doesn't appear in source code causes crash

Contents of NSString+sha1.h:
#include <CommonCrypto/CommonDigest.h>
#include <Foundation/Foundation.h>
#interface NSString (sha1)
- (NSString *) sha1;
#end
Contents of NSString+sha1.m:
#include "NSString+sha1.h"
#implementation NSString (sha1)
- (NSString *) sha1 {
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:#"%02x", digest[i]];
return [NSString stringWithString:output];
}
#end
Contents of UIImage+RenderBatteryImage.m:
#include "UIImage+RenderBatteryImage.h"
#include "NSString+sha1.h"
[...]
[#"A string (but not this one)" sha1]
When the code from the third file runs, I get this error:
-[__NSCFString sha1]: unrecognized selector sent to instance 0x12ee1caf0
What is causing this? I can confirm that I have no instances of uppercase SHA1 in any of my source files.
I have an assumption that require validation. In syslog you have following error:
-[__NSCFString sha1]: unrecognized selector sent to instance 0x12ee1caf0
Looks like your method sha1 is not found and it cause a crash. How it may happen? Compiler replace an object of #"A string (but not this one)" with some internal object that represents constant string and you do not have a category method defined for this particular constant string type.
Here is workflow that I suggest to apply to validate and fix assumption:
Comment out string where you do sha1 call and check that error is not happening
Replace literal with an actual object [NSString withString: #"your string"] to oversmart compiler
Here is actually some proof that my assumption right regarding compile-time replacement of a string by constant representation — What is the different between NSCFString and NSConstantString?
Sample code, a simple self-contained method, just add it to your class:
// Add Security.framework to the project.
#include <CommonCrypto/CommonDigest.h>
+ (NSString *) sha1:(NSString *)string {
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, hash.mutableBytes);
NSMutableString *hexAscii = [NSMutableString new];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[hexAscii appendFormat:#"%02x", ((uint8_t *)hash.mutableBytes)[i]];
return [hexAscii copy]; // Make immutable
}
Test (assuming that the method is in the class Test.
NSString *hashHexASCII = [Test sha1:#"test String"];
NSLog(#"hashHexASCII: %#", hashHexASCII);
Output:
hashHexASCII: 9269ca2a6a1695eff8d5acd47b57c045698e3ce1

NSNull stringByReplacingOccurrencesOfString:withString: issue with a JSON

i've a problem with parsing a JSON:
in mycallback method i copy object for key "a" in a NSDictionary *test as follow:
[[test sharedManager] MyTestCallback:^(MyTestSessionStatus status, NSDictionary * t, NSError * error){
test = [t objectForKey:#"a"];
}
then in an other method i copy in an array the value
NSArray *temp = [discoveredMedia valueForKeyPath:#"b"];
and then when i try to do
NSString *temp2 = (NSString *)[temp objectAtIndex:0];
but value is -> Printing description of temp2: < null >
and in debugger -> temp2 NSString * class name = NSNull 0x383ed3d0
it crushed when
temp2 = (NSString *)[temp2 stringByReplacingOccurrencesOfString:#"<$size$" withString:#"350"];
in console:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x383ed3d0'
what's going on?
Thanks
In JSON you have 'null' in dictionary and it will return in objective c not nil but NSNull.
You need to treat NSNull as nil.
I have made a simple NSNull workaround, to avoid crashes on NSNull calls
1) Add this code in your AppDelegate
#import "EXTNil.h"
#import <objc/runtime.h>
+ (id)swizzled_null
{
return [EXTNil null];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SEL originalSelector = #selector(null);
SEL swizzledSelector = #selector(swizzled_null);
Method originalMethod = class_getClassMethod([NSNull class], originalSelector);
Method swizzledMethod = class_getClassMethod([self class], swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
return YES;
}
2) Added (EXTNil and EXTRuntimeExtensions) classes to your project from repo https://github.com/jspahrsummers/libextobjc/tree/master/extobjc
Now You can call any method on NSNull

encodeData writeToFile EXC_BAD_ACCESS

I am trying to save my game data. I began with this tutorial from Ray: How to Save your Game’s Data: Part 1/2
Everything works fine except -(void)save;
-(void)save
{
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:[RWGameData filePath] atomically:YES];
}
When I called this method I get this;
-[__NSCFArray getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x1663dcc0
2014-07-14 18:42:15.029 App[3629:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x1663dcc0'
Or this:
exc_bad_access code=1 address=0xc or exc_bad_access code=1 address=0x7000000c or exc_bad_access code=1 address=0x1000000c
on this line:
[encodedData writeToFile:[RWGameData filePath] atomically:YES];
How can I fix this? Source code from tutorial works fine. But in my game its crashed.
UPDATE:
In Ray's source code Objective C ARC: YES. But then I use ARC, it gives me tons of errors.
EDIT: I changed code a little bit and go to a second part of a tutorial. Save/Load work correctly first run, but on second run it load correctly only once and when I trying to change score values I have exc_bad_access.
+(instancetype)loadInstance
{
pathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"gamedata"];
NSData* decodedData = [NSData dataWithContentsOfFile: pathString];
if (decodedData) {
//1
NSString* checksumOfSavedFile = [KeychainWrapper computeSHA256DigestForData: decodedData];
//2
NSString* checksumInKeychain = [KeychainWrapper keychainStringFromMatchingIdentifier: SSGameDataChecksumKey];
//3
if ([checksumOfSavedFile isEqualToString: checksumInKeychain]) {
RWGameData* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
return gameData;
}
//4
}
return [[RWGameData alloc] init];
}
-(void)save
{
pathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"gamedata"];
NSData* encodedData = [NSKeyedArchiver archivedDataWithRootObject: self];
[encodedData writeToFile:pathString atomically:YES];
NSString* checksum = [KeychainWrapper computeSHA256DigestForData: encodedData];
if ([KeychainWrapper keychainStringFromMatchingIdentifier: SSGameDataChecksumKey]) {
[KeychainWrapper updateKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
} else {
[KeychainWrapper createKeychainValue:checksum forIdentifier:SSGameDataChecksumKey];
}
if([NSUbiquitousKeyValueStore defaultStore]) {
[self updateiCloud];
}
}
EXC_BAD_ACCSES on this line in my gamelayer after updated score:
[RWGameData sharedGameData].score += 1;
But if I call this line from init method - its working.
Check your +[RWGameData filePath] method implementation. It appears to be actually returning an array, not a string as expected.
You can see this from the "unrecognized selector" message, thrown by NSCFArray (which is an internal implementation of an NSArray). That array is complaining that someone is trying to access a method on it that only exists on strings. Since you're not doing that yourself directly, the -writeToFile:... method must be doing that on the result of -filePath on RWGameData.

iOS app crash when trying to log NSDictionary

I have a problem where I try to log NSDictionary content and as soon as the method is called the app crashes.
This is the code I have tried after advice from a talented "hacker":
%hook UserData
-(int)getVariable:(NSDictionary *)fp8 {
for (NSString *key in [fp8 allKeys]) {
%log(#"key: %#, value: %# \n", key, [fp8 objectForKey:key]);
}
return %orig;
}
%end
also tried:
%hook UserData
-(int)getVariable {
int originalValue = %orig;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSError *error;
[[fp8 description] writeToFile:[NSString stringWithFormat:#"%#/lol_%d.txt",basePath,fp8.count] atomically:NO encoding:NSUTF8StringEncoding error:&error];
return %orig;
}
%end
Both ways result in a crash of the app. This is on a iphone 4 with ios 6.1.3 tethered JB.
DoD EN[1000]: -[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88
DoD EN[1000]: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88'
*** First throw call stack:
Does anyone have a suggestion on what may be wrong here?
The crash log provide quite an explanation
-[__NSCFConstantString allKeys]: unrecognized selector sent to instance 0x2a7a88
I guess this is the crash log associated with the first piece of code you posted :
%hook UserData
-(int)getVariable:(NSDictionary *)fp8 {
for (NSString *key in [fp8 allKeys]) {
%log(#"key: %#, value: %# \n", key, [fp8 objectForKey:key]);
}
return %orig;
}
%end
It crashes at the execution of [fp8 allKeys], claiming that allKeys is not a valid selector for objects of class __NSCFConstantString
What it tells you is that fp8 is not an NSDictionary* but rather a __NSCFConstantString* (that is, a pointer to a constant instance of NSString such as one defined like NSString* foo = #"bar").
If that is true, then the second code you posted would crash also because count is not a valid selector for class NSString.
Why don't you just try the following, see what it gives you :
%hook UserData
-(int)getVariable:(id)fp8 {
%log(#"fp8: %# : %#\n", NSStringFromClass([fp8 class]), [fp8 description]);
return %orig;
}
%end
If you print the description it should work. Following Way :
NSLog(#"%#", [dictionary description]);

Resources