project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
785 stars 237 forks source link

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.' #269

Open anataffy opened 7 years ago

anataffy commented 7 years ago

This is my App.delegate When I run the app it crashes with the following error This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation

Some advice??

pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

}

pragma mark - Core Data Saving support

lolgear commented 7 years ago

@anataffy it is hard to read your question. Please, use backquotes to format code, e.g.

NSString *code = @"code";

Code sample should start with three backquotes sequence.

Also add objective-c after first three backquotes.

Learn about markdown and all related stuff here:

https://guides.github.com/features/mastering-markdown/

tomasz-czyzak commented 7 years ago

I got the same issue. V3.1 retrieved thru CocoaPods Pinned it down to EncryptedStore.m

method: + (NSPersistentStoreCoordinator *)makeStoreWithOptions:(NSDictionary *)options managedObjectModel:(NSManagedObjectModel *)objModel error:(NSError *__autoreleasing *)error calls:

 [persistentCoordinator addPersistentStoreWithType:EncryptedStoreType configuration:nil URL:databaseURL
        options:options error:error];

if (*error)
{
    NSLog(@"Unable to add persistent store.");
    NSLog(@"Error: %@\n%@\n%@", *error, [*error userInfo], [*error localizedDescription]);
}
return persistentCoordinator;

that returned persistentCoordinator doesn't contain store and error is nil so no log either

tomasz-czyzak commented 7 years ago

Pinned down issue to bug in:

- (BOOL)setDatabasePassphrase:(NSString *)passphrase error:(NSError *__autoreleasing*)error {
    BOOL result;
    int status;

    if ([passphrase length] > 0) {
        // Password provided, use it to key the DB
        const char *string = [passphrase UTF8String];
        status = sqlite3_key(database, string, (int)strlen(string));
    } else {
[...]

Crash scenario is:

  1. passphrase passed as param start with "\0" for example "\0abdefbindiwdomd" (which may happen pretty often if you use some autogenerated helper based on NSData random bytes.
  2. first char in string ptr will be set to 0 and strlen will return 0
  3. managed object context doesn't have stores
  4. crash when saving data into database
tomasz-czyzak commented 7 years ago

fix is:

 -        const char *string = [passphrase UTF8String];
 -        status = sqlite3_key(database, string, (int)strlen(string));
 +        NSData *passBytes = [passphrase dataUsingEncoding:NSUTF8StringEncoding];
 +        status = sqlite3_key(database, passBytes.bytes, passBytes.length);

and similar changes in changeDatabasePassphrase method too

DanielBroad commented 7 years ago

I look forward to your PR.

tomasz-czyzak commented 7 years ago

Ready: https://github.com/project-imas/encrypted-core-data/pull/279

StevenAppJob commented 2 years ago

Hi, I have the same error when I updated from master folder. Any solution for this?