Skip to content

Commit

Permalink
Merge pull request #789 from Sequel-Ace/some-crash-fixes
Browse files Browse the repository at this point in the history
#fixed Some crashes
  • Loading branch information
Jason-Morcos committed Jan 15, 2021
2 parents a53198a + 2780ca2 commit f220ccb
Show file tree
Hide file tree
Showing 20 changed files with 678 additions and 189 deletions.
260 changes: 139 additions & 121 deletions Source/Controllers/DataExport/SPExportController.m

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -467,16 +467,39 @@ - (IBAction)chooseKeyLocation:(NSButton *)sender
NSString *selectedFilePath=[[self->keySelectionPanel URL] path];
NSError *err=nil;

SPLog(@"calling addBookmarkForUrl");
CLS_LOG(@"calling addBookmarkForUrl");
// this needs to be read-only to handle keys with 400 perms so we add the bitwise OR NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->keySelectionPanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:NO] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");
// check it's really a URL
if(![self->keySelectionPanel.URL isKindOfClass:[NSURL class]]){
NSMutableString *classStr = [NSMutableString string];
[classStr appendStringOrNil:NSStringFromClass(self->keySelectionPanel.URL.class)];

SPLog(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
CLS_LOG(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
// JCS - should we stop here?

NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"self->keySelectionPanel.URL is not a URL",
@"class": classStr
};

[FIRCrashlytics.crashlytics recordError:[NSError errorWithDomain:@"chooseFile" code:1 userInfo:userInfo]];

}
else{
SPLog(@"addBookmarkForUrl failed");
CLS_LOG(@"addBookmarkForUrl failed");
SPLog(@"calling addBookmarkForUrl");
CLS_LOG(@"calling addBookmarkForUrl");
// this needs to be read-only to handle keys with 400 perms so we add the bitwise OR NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->keySelectionPanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:NO] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");
}
else{
SPLog(@"addBookmarkForUrl failed");
CLS_LOG(@"addBookmarkForUrl failed");
// JCS - should we stop here?
// No just the act of selecting the file in the NSOpenPanel calls startAccessingSecurityScopedResource
// the only downside of this failing is that we won't have a bookmark,
// and therefore no access on the next app start
}
}

// SSH key file selection
Expand Down Expand Up @@ -2315,12 +2338,16 @@ - (void)sshTunnelCallback:(SPSSHTunnel *)theTunnel

// If the user cancelled the password prompt dialog, continue with no further action.
if ([theTunnel passwordPromptCancelled]) {
SPLog(@"user cancelled the password prompt dialog, continue with no further action");
CLS_LOG(@"user cancelled the password prompt dialog, continue with no further action");
[self _restoreConnectionInterface];

return;
}

if (newState == SPMySQLProxyIdle) {
SPLog(@"SPMySQLProxyIdle, failing");
CLS_LOG(@"SPMySQLProxyIdle, failing");

[dbDocument setTitlebarStatus:NSLocalizedString(@"SSH Disconnected", @"SSH disconnected titlebar marker")];

Expand All @@ -2330,6 +2357,8 @@ - (void)sshTunnelCallback:(SPSSHTunnel *)theTunnel
rawErrorText:[theTunnel lastError]];
}
else if (newState == SPMySQLProxyConnected) {
SPLog(@"SPMySQLProxyConnected, calling initiateMySQLConnection");
CLS_LOG(@"SPMySQLProxyConnected, calling initiateMySQLConnection");
[dbDocument setTitlebarStatus:NSLocalizedString(@"SSH Connected", @"SSH connected titlebar marker")];

[self initiateMySQLConnection];
Expand Down
17 changes: 16 additions & 1 deletion Source/Controllers/MainViewControllers/SPDatabaseDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ @interface SPDatabaseDocument ()
@property (nonatomic, strong) NSImage *hideConsoleImage;
@property (nonatomic, strong) NSImage *showConsoleImage;
@property (nonatomic, strong) NSImage *textAndCommandMacwindowImage API_AVAILABLE(macos(11.0));
@property (assign) BOOL appIsTerminating;


- (void)_addDatabase;
- (void)_alterDatabase;
Expand Down Expand Up @@ -154,6 +156,7 @@ @implementation SPDatabaseDocument
@synthesize hideConsoleImage;
@synthesize showConsoleImage;
@synthesize textAndCommandMacwindowImage;
@synthesize appIsTerminating;

#pragma mark -

Expand Down Expand Up @@ -188,6 +191,7 @@ - (instancetype)init
statusLoaded = NO;
triggersLoaded = NO;
relationsLoaded = NO;
appIsTerminating = NO;

hideConsoleImage = [NSImage imageNamed:@"hideconsole"];
showConsoleImage = [NSImage imageNamed:@"showconsole"];
Expand Down Expand Up @@ -2641,6 +2645,10 @@ - (void)hasPerformedQuery:(NSNotification *)notification
*/
- (void)applicationWillTerminate:(NSNotification *)notification
{

SPLog(@"applicationWillTerminate");
CLS_LOG(@"applicationWillTerminate");
appIsTerminating = YES;
// Auto-save preferences to spf file based connection
if([self fileURL] && [[[self fileURL] path] length] && ![self isUntitled]) {
if (_isConnected && ![self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES contextInfo:nil]) {
Expand Down Expand Up @@ -6608,11 +6616,18 @@ - (void)noConnectionAvailable:(id)connection
*/
- (SPMySQLConnectionLostDecision)connectionLost:(id)connection
{

SPLog(@"connectionLost");
CLS_LOG(@"connectionLost");

SPMySQLConnectionLostDecision connectionErrorCode = SPMySQLConnectionLostDisconnect;

// Only display the reconnect dialog if the window is visible
if ([self parentWindow] && [[self parentWindow] isVisible]) {
// and we are not terminating
if ([self parentWindow] && [[self parentWindow] isVisible] && appIsTerminating == NO) {

SPLog(@"not terminating, parentWindow isVisible, showing connectionErrorDialog");
CLS_LOG(@"not terminating, parentWindow isVisible, showing connectionErrorDialog");
// Ensure the window isn't miniaturized
if ([[self parentWindow] isMiniaturized]) [[self parentWindow] deminiaturize:self];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ - (void)loadTable:(NSString *)aTable
}

// Set BINARY if collation ends with _bin for convenience
if ([collation hasSuffix:@"_bin"]) {
if (![collation isNSNull] && [collation hasSuffix:@"_bin"]) {
[theField setObject:@1 forKey:@"binary"];
}
}
Expand Down
77 changes: 48 additions & 29 deletions Source/Controllers/Preferences/Panes/SPFilePreferencePane.m
Original file line number Diff line number Diff line change
Expand Up @@ -480,37 +480,56 @@ - (void)chooseFileWithOptions:(PanelOptions*)options
// read-only.
[self->_currentFilePanel.URLs enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idxURL, BOOL *stopURL){

if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->_currentFilePanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:options.isForStaleBookmark] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");

if(options.isForStaleBookmark == YES){

// Here we need to maintain the options filnames array
// and the selectedRows index
SPLog(@"options.fileNames: %@", options.fileNames);
SPLog(@"self->selectedRows: %@", self->selectedRows);
SPLog(@"removing stale file from options.fileNames at index 0");
SPLog(@"removing stale file from self->selectedRows at index: %lu", (unsigned long)options.index);
CLS_LOG(@"removing stale file from options.fileNames");

SPLog(@"selectedRows count = %lu", (unsigned long)self->selectedRows.count);

// we need to keep track of how many files there are to prompt for
// so we remove from the filenames array. Index 0 is safe.
[options.fileNames removeObjectAtIndex:0];
// to keep things nice and tidy, remove the index?
[self->selectedRows removeIndex:options.index];
SPLog(@"options.fileNames: %@", options.fileNames);
SPLog(@"self->selectedRows: %@", self->selectedRows);

// increment for the next file
options.index++;
}
// check it's really a URL
if(![self->_currentFilePanel.URL isKindOfClass:[NSURL class]]){
NSMutableString *classStr = [NSMutableString string];
[classStr appendStringOrNil:NSStringFromClass(self->_currentFilePanel.URL.class)];

SPLog(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
CLS_LOG(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
// JCS - should we stop here?

NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"self->keySelectionPanel.URL is not a URL",
@"class": classStr,
@"URLs" : self->_currentFilePanel.URLs
};

[FIRCrashlytics.crashlytics recordError:[NSError errorWithDomain:@"chooseFile" code:1 userInfo:userInfo]];
}
else{
CLS_LOG(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
SPLog(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->_currentFilePanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:options.isForStaleBookmark] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");

if(options.isForStaleBookmark == YES){

// Here we need to maintain the options filnames array
// and the selectedRows index
SPLog(@"options.fileNames: %@", options.fileNames);
SPLog(@"self->selectedRows: %@", self->selectedRows);
SPLog(@"removing stale file from options.fileNames at index 0");
SPLog(@"removing stale file from self->selectedRows at index: %lu", (unsigned long)options.index);
CLS_LOG(@"removing stale file from options.fileNames");

SPLog(@"selectedRows count = %lu", (unsigned long)self->selectedRows.count);

// we need to keep track of how many files there are to prompt for
// so we remove from the filenames array. Index 0 is safe.
[options.fileNames removeObjectAtIndex:0];
// to keep things nice and tidy, remove the index?
[self->selectedRows removeIndex:options.index];
SPLog(@"options.fileNames: %@", options.fileNames);
SPLog(@"self->selectedRows: %@", self->selectedRows);

// increment for the next file
options.index++;
}
}
else{
CLS_LOG(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
SPLog(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
}
}
}];

Expand Down
33 changes: 26 additions & 7 deletions Source/Controllers/Preferences/Panes/SPNetworkPreferencePane.m
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,35 @@ - (void)chooseSSHConfig
// since ssh configs are able to consist of multiple files, bookmarks
// for every selected file should be created in order to access them
// read-only.
[self->_currentFilePanel.URLs enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idxURL, BOOL *stopURL){
// check if the file is out of the sandbox
[self->_currentFilePanel.URLs enumerateObjectsUsingBlock:^(NSURL *url, NSUInteger idxURL, BOOL *stopURL){
// check if the file is out of the sandbox

if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->_currentFilePanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:NO] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");
// check it's really a URL
if(![self->_currentFilePanel.URL isKindOfClass:[NSURL class]]){
NSMutableString *classStr = [NSMutableString string];
[classStr appendStringOrNil:NSStringFromClass(self->_currentFilePanel.URL.class)];

SPLog(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
CLS_LOG(@"self->keySelectionPanel.URL is not a URL: %@", classStr);
// JCS - should we stop here?

NSDictionary *userInfo = @{
NSLocalizedDescriptionKey: @"self->keySelectionPanel.URL is not a URL",
@"class": classStr,
@"URLs" : self->_currentFilePanel.URLs
};

[FIRCrashlytics.crashlytics recordError:[NSError errorWithDomain:@"chooseFile" code:1 userInfo:userInfo]];
}
else{
CLS_LOG(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
SPLog(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
if([SecureBookmarkManager.sharedInstance addBookmarkForUrl:self->_currentFilePanel.URL options:(NSURLBookmarkCreationWithSecurityScope|NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) isForStaleBookmark:NO] == YES){
SPLog(@"addBookmarkForUrl success");
CLS_LOG(@"addBookmarkForUrl success");
}
else{
CLS_LOG(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
SPLog(@"addBookmarkForUrl failed: %@", self->_currentFilePanel.URL.absoluteString);
}
}

// set the config path to the first selected file
Expand Down
47 changes: 34 additions & 13 deletions Source/Controllers/SubviewControllers/SPFieldEditorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -534,23 +534,44 @@ - (IBAction)segmentControllerChanged:(id)sender
NSError *error;
NSData *jsonData = [sheetEditData dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

if([NSJSONSerialization isValidJSONObject:jsonObject]){
NSData *prettyJsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:&error];
NSString *prettyPrintedJson = [NSString stringWithUTF8String:[prettyJsonData bytes]];
if(prettyJsonData != nil){
[jsonTextView setString:prettyPrintedJson];
}
else{
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
}

if(error != nil){
SPLog(@"JSONObjectWithData error : %@", error.localizedDescription);
CLS_LOG(@"JSONObjectWithData error : %@", error.localizedDescription);
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
}
else{
if(sheetEditData != nil){
[jsonTextView setString:sheetEditData];
if([NSJSONSerialization isValidJSONObject:jsonObject]){
SPLog(@"isValidJSONObject : %@", jsonObject);
CLS_LOG(@"isValidJSONObject : %@", jsonObject);
NSData *prettyJsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:&error];

if(error != nil){
SPLog(@"dataWithJSONObject error : %@", error.localizedDescription);
CLS_LOG(@"dataWithJSONObject error : %@", error.localizedDescription);
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
}
else{
NSString *prettyPrintedJson = [NSString stringWithUTF8String:[prettyJsonData bytes]];
if(prettyPrintedJson != nil){
SPLog(@"prettyPrintedJson : %@", prettyPrintedJson);
CLS_LOG(@"prettyPrintedJson : %@", prettyPrintedJson);
[jsonTextView setString:prettyPrintedJson];
}
else{
SPLog(@"prettyPrintedJson is nil");
CLS_LOG(@"prettyPrintedJson is nil");
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
}
}
}
else{
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
if(sheetEditData != nil){
[jsonTextView setString:sheetEditData];
}
else{
[jsonTextView setString:NSLocalizedString(@"Invalid JSON",@"Message for field editor JSON segment when JSON is invalid")];
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Controllers/SubviewControllers/SPQueryController.m
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum

if (!identifier) return returnValue;

id object = [[messagesVisibleSet objectAtIndex:row] valueForKey:identifier];
id object = [[messagesVisibleSet safeObjectAtIndex:row] valueForKey:identifier];

if ([[tableColumn identifier] isEqualToString:SPTableViewDateColumnID]) {

Expand All @@ -761,7 +761,7 @@ - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColum
}

// If this is an error message give it a red colour
if ([(SPConsoleMessage *)[messagesVisibleSet objectAtIndex:row] isError]) {
if ([(SPConsoleMessage *)[messagesVisibleSet safeObjectAtIndex:row] isError]) {
if (stringAtributes) {
[stringAtributes setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Controllers/Window/SPWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ - (BOOL)windowShouldClose:(NSWindow *)sender
*/
- (void)windowWillClose:(NSNotification *)notification
{
SPLog(@"windowWillClose, notification: %@", notification);
CLS_LOG(@"windowWillClose, notification: %@", notification);
for (NSTabViewItem *eachItem in [tabView tabViewItems])
{
[tabView removeTabViewItem:eachItem];
Expand Down
4 changes: 2 additions & 2 deletions Source/Model/SPNarrowDownCompletion.m
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ - (void)watchUserEvents
if([mutablePrefix length] == 0 || commaInsertionMode) break;

spaceCounter = 0;
[mutablePrefix deleteCharactersInRange:NSMakeRange([mutablePrefix length]-1, 1)];
[originalFilterString deleteCharactersInRange:NSMakeRange([originalFilterString length]-1, 1)];
[mutablePrefix safeDeleteCharactersInRange:NSMakeRange([mutablePrefix length]-1, 1)];
[originalFilterString safeDeleteCharactersInRange:NSMakeRange([originalFilterString length]-1, 1)];
theCharRange.length--;
theParseRange.length--;
[self filter];
Expand Down

0 comments on commit f220ccb

Please sign in to comment.