Skip to content

Commit 2f2aafb

Browse files
committed
* Basic math is hard sometimes (fixes an issue introduced by me in b2d798b)
* Fix an exception that could occur when trying to view a damaged table * Fix a theoretical use-after-free issue by a wrongly structured retain/release in a setter
1 parent 5795587 commit 2f2aafb

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

Frameworks/SPMySQLFramework/Source/SPMySQLConnection Categories/Server Info.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ - (BOOL)serverVersionIsGreaterThanOrEqualTo:(NSUInteger)aMajorVersion minorVersi
8787
{
8888
unsigned long myver = aMajorVersion * 10000 + aMinorVersion * 100 + aReleaseVersion;
8989

90-
return (myver >= serverVersionNumber);
90+
return (serverVersionNumber >= myver);
9191
}
9292

9393
#pragma mark -

Source/SPDatabaseData.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ - (NSString *)_getSingleVariableValue:(NSString *)variable
471471

472472
[result setReturnDataAsStrings:YES];
473473

474+
if([connection queryErrored])
475+
SPLog(@"server variable lookup failed for '%@': %@ (%lu)",variable,[connection lastErrorMessage],[connection lastErrorID]);
476+
474477
if ([result numberOfRows] != 1)
475478
return nil;
476479

Source/SPDatabaseDocument.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,20 +1729,20 @@ - (void)detectDatabaseEncoding
17291729
{
17301730
_supportsEncoding = YES;
17311731

1732-
NSString *mysqlEncoding = [databaseDataInstance getDatabaseDefaultCharacterSet];
1732+
NSString *mysqlEncoding = [[databaseDataInstance getDatabaseDefaultCharacterSet] retain];
17331733

17341734
SPClear(selectedDatabaseEncoding);
17351735

17361736
// Fallback or older version? -> set encoding to mysql default encoding latin1
17371737
if ( !mysqlEncoding ) {
1738-
NSLog(@"Error: no character encoding found, mysql version is %@", [self mySQLVersion]);
1738+
NSLog(@"Error: no character encoding found for db, mysql version is %@", [self mySQLVersion]);
17391739

17401740
selectedDatabaseEncoding = [[NSString alloc] initWithString:@"latin1"];
17411741

17421742
_supportsEncoding = NO;
17431743
}
17441744
else {
1745-
selectedDatabaseEncoding = [mysqlEncoding retain];
1745+
selectedDatabaseEncoding = mysqlEncoding;
17461746
}
17471747
}
17481748

Source/SPTableContent.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ - (void) setTableDetails:(NSDictionary *)tableDetails
434434
[tableContentView scrollColumnToVisible:0];
435435

436436
// Set the maximum table rows to an estimated count pre-load
437-
maxNumRows = [[tableDataInstance statusValueForKey:@"Rows"] integerValue];
437+
NSString *rows = [tableDataInstance statusValueForKey:@"Rows"];
438+
maxNumRows = (rows && ![rows isNSNull])? [rows integerValue] : 0;
438439
maxNumRowsIsEstimate = YES;
439440
}
440441

@@ -3991,14 +3992,15 @@ - (void)updateNumberOfRows
39913992
[tableDataInstance updateAccurateNumberOfRowsForCurrentTableForcingUpdate:NO];
39923993

39933994
// If the state is now accurate, use it
3995+
NSString *rows = [tableDataInstance statusValueForKey:@"Rows"];
39943996
if ([[tableDataInstance statusValueForKey:@"RowsCountAccurate"] boolValue]) {
3995-
maxNumRows = [[tableDataInstance statusValueForKey:@"Rows"] integerValue];
3997+
maxNumRows = [rows integerValue];
39963998
maxNumRowsIsEstimate = NO;
39973999
checkStatusCount = YES;
3998-
3999-
// Otherwise, use the estimate count
4000-
} else {
4001-
maxNumRows = [[tableDataInstance statusValueForKey:@"Rows"] integerValue];
4000+
}
4001+
// Otherwise, use the estimate count
4002+
else {
4003+
maxNumRows = (rows && ![rows isNSNull])? [rows integerValue] : 0;
40024004
maxNumRowsIsEstimate = YES;
40034005
checkStatusCount = YES;
40044006
}

Source/SPTableData.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ - (BOOL)updateStatusInformationForCurrentTable
10041004
SPOnewayAlertSheet(
10051005
NSLocalizedString(@"Error", @"error"),
10061006
[NSApp mainWindow],
1007-
[NSString stringWithFormat:NSLocalizedString(@"An error occured while retrieving status data.\nMySQL said: %@", @"message of panel when retrieving view information failed"), [mySQLConnection lastErrorMessage]]
1007+
[NSString stringWithFormat:NSLocalizedString(@"An error occured while retrieving status data.\n\nMySQL said: %@", @"message of panel when retrieving view information failed"), [mySQLConnection lastErrorMessage]]
10081008
);
10091009
if (changeEncoding) [mySQLConnection restoreStoredEncoding];
10101010
}
@@ -1041,9 +1041,19 @@ - (BOOL)updateStatusInformationForCurrentTable
10411041
// this happens e.g. for db "information_schema"
10421042
if([[status objectForKey:@"Rows"] isNSNull]) {
10431043
tableStatusResult = [mySQLConnection queryString:[NSString stringWithFormat:@"SELECT COUNT(1) FROM %@", [escapedTableName backtickQuotedString] ]];
1044-
if (![mySQLConnection queryErrored])
1044+
// this query can fail e.g. if a table is damaged
1045+
if (tableStatusResult && ![mySQLConnection queryErrored]) {
10451046
[status setObject:[[tableStatusResult getRowAsArray] objectAtIndex:0] forKey:@"Rows"];
10461047
[status setObject:@"y" forKey:@"RowsCountAccurate"];
1048+
}
1049+
else {
1050+
//FIXME that error should really show only when trying to view the table content, but we don't even try to load that if Rows==NULL
1051+
SPOnewayAlertSheet(
1052+
NSLocalizedString(@"Querying row count failed", @"table status : row count query failed : error title"),
1053+
[NSApp mainWindow],
1054+
[NSString stringWithFormat:NSLocalizedString(@"An error occured while trying to determine the number of rows for “%@”.\nMySQL said: %@ (%lu)", @"table status : row count query failed : error message"),[tableListInstance tableName],[mySQLConnection lastErrorMessage],[mySQLConnection lastErrorID]]
1055+
);
1056+
}
10471057
}
10481058

10491059
}

0 commit comments

Comments
 (0)