Skip to content

Commit

Permalink
Replace some malloc(a*b) with the safer calloc(a,b) variant
Browse files Browse the repository at this point in the history
  • Loading branch information
dmoagx committed Jun 30, 2015
1 parent 6d372fa commit 3c49b0f
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
Expand Up @@ -32,6 +32,7 @@
#import "SPMySQL Private APIs.h" #import "SPMySQL Private APIs.h"
#import "SPMySQLArrayAdditions.h" #import "SPMySQLArrayAdditions.h"
#include <pthread.h> #include <pthread.h>
#include <stdlib.h>


static id NSNullPointer; static id NSNullPointer;


Expand Down Expand Up @@ -360,7 +361,7 @@ - (void)_downloadAllData
newRowStore->nextRow = NULL; newRowStore->nextRow = NULL;


// Set up the row data store - a char* - and copy in the data if there is any. // Set up the row data store - a char* - and copy in the data if there is any.
newRowStore->data = malloc(sizeOfChar * rowDataLength); newRowStore->data = calloc(rowDataLength, sizeOfChar);
for (i = 0; i < numberOfFields; i++) { for (i = 0; i < numberOfFields; i++) {
if (theRow[i] != NULL) { if (theRow[i] != NULL) {
memcpy(newRowStore->data+dataCopiedLength, theRow[i], fieldLengths[i]); memcpy(newRowStore->data+dataCopiedLength, theRow[i], fieldLengths[i]);
Expand Down
13 changes: 7 additions & 6 deletions Source/SPCopyTable.m
Expand Up @@ -53,6 +53,7 @@


#import <SPMySQL/SPMySQL.h> #import <SPMySQL/SPMySQL.h>
#import "pthread.h" #import "pthread.h"
#include <stdlib.h>


NSInteger SPEditMenuCopy = 2001; NSInteger SPEditMenuCopy = 2001;
NSInteger SPEditMenuCopyWithColumns = 2002; NSInteger SPEditMenuCopyWithColumns = 2002;
Expand Down Expand Up @@ -171,7 +172,7 @@ - (NSString *)rowsAsTabStringWithHeaders:(BOOL)withHeaders onlySelectedRows:(BOO
id cellData = nil; id cellData = nil;


// Create an array of table column mappings for fast iteration // Create an array of table column mappings for fast iteration
NSUInteger *columnMappings = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnMappings = calloc(numColumns, sizeof(NSUInteger));
for ( c = 0; c < numColumns; c++ ) for ( c = 0; c < numColumns; c++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue]; columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];


Expand Down Expand Up @@ -309,7 +310,7 @@ - (NSString *)rowsAsCsvStringWithHeaders:(BOOL)withHeaders onlySelectedRows:(BOO
id cellData = nil; id cellData = nil;


// Create an array of table column mappings for fast iteration // Create an array of table column mappings for fast iteration
NSUInteger *columnMappings = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnMappings = calloc(numColumns, sizeof(NSUInteger));
for ( c = 0; c < numColumns; c++ ) for ( c = 0; c < numColumns; c++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue]; columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];


Expand Down Expand Up @@ -447,8 +448,8 @@ - (NSString *)rowsAsSqlInsertsOnlySelectedRows:(BOOL)onlySelected
} }


// Create arrays of table column mappings and types for fast iteration // Create arrays of table column mappings and types for fast iteration
NSUInteger *columnMappings = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnMappings = calloc(numColumns, sizeof(NSUInteger));
NSUInteger *columnTypes = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnTypes = calloc(numColumns, sizeof(NSUInteger));


for (c = 0; c < numColumns; c++) for (c = 0; c < numColumns; c++)
{ {
Expand Down Expand Up @@ -628,7 +629,7 @@ - (NSString *) draggedRowsAsTabString
id cellData = nil; id cellData = nil;


// Create an array of table column mappings for fast iteration // Create an array of table column mappings for fast iteration
NSUInteger *columnMappings = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnMappings = calloc(numColumns, sizeof(NSUInteger));
for ( c = 0; c < numColumns; c++ ) for ( c = 0; c < numColumns; c++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue]; columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];


Expand Down Expand Up @@ -1373,7 +1374,7 @@ - (IBAction)executeBundleItemForDataTable:(id)sender
// Create an array of table column mappings for fast iteration // Create an array of table column mappings for fast iteration
NSArray *columns = [self tableColumns]; NSArray *columns = [self tableColumns];
NSUInteger numColumns = [columns count]; NSUInteger numColumns = [columns count];
NSUInteger *columnMappings = malloc(numColumns * sizeof(NSUInteger)); NSUInteger *columnMappings = calloc(numColumns, sizeof(NSUInteger));
NSUInteger c; NSUInteger c;
for ( c = 0; c < numColumns; c++ ) for ( c = 0; c < numColumns; c++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue]; columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];
Expand Down
3 changes: 2 additions & 1 deletion Source/SPDataAdditions.m
Expand Up @@ -37,6 +37,7 @@
#include <zlib.h> #include <zlib.h>
#include <openssl/aes.h> #include <openssl/aes.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <stdlib.h>


@implementation NSData (SPDataAdditions) @implementation NSData (SPDataAdditions)


Expand Down Expand Up @@ -247,7 +248,7 @@ - (NSString *)dataToFormattedHexString
buffLength = totalLength - i; buffLength = totalLength - i;
} }


buffer = (unsigned char*) malloc( sizeof(unsigned char) * buffLength + 1); buffer = (unsigned char*) calloc(buffLength + 1, sizeof(unsigned char));


[self getBytes:buffer range:NSMakeRange(i, buffLength)]; [self getBytes:buffer range:NSMakeRange(i, buffLength)];


Expand Down
3 changes: 2 additions & 1 deletion Source/SPEncodingPopupAccessory.m
Expand Up @@ -29,6 +29,7 @@
// More info at <https://github.com/sequelpro/sequelpro> // More info at <https://github.com/sequelpro/sequelpro>


#import "SPEncodingPopupAccessory.h" #import "SPEncodingPopupAccessory.h"
#include <stdlib.h>


@implementation SPEncodingPopupAccessory @implementation SPEncodingPopupAccessory


Expand Down Expand Up @@ -92,7 +93,7 @@ + (NSArray *)enabledEncodings


while (cfEncodings[num] != kCFStringEncodingInvalidId) num++; while (cfEncodings[num] != kCFStringEncodingInvalidId) num++;


tmp = malloc(sizeof(CFStringEncoding) * num); tmp = calloc(num, sizeof(CFStringEncoding));


memcpy(tmp, cfEncodings, sizeof(CFStringEncoding) * num); memcpy(tmp, cfEncodings, sizeof(CFStringEncoding) * num);


Expand Down
5 changes: 3 additions & 2 deletions Source/SPSQLExporter.m
Expand Up @@ -37,6 +37,7 @@
#import "RegexKitLite.h" #import "RegexKitLite.h"


#import <SPMySQL/SPMySQL.h> #import <SPMySQL/SPMySQL.h>
#include <stdlib.h>


@interface SPSQLExporter () @interface SPSQLExporter ()


Expand Down Expand Up @@ -293,8 +294,8 @@ - (void)main
NSMutableArray *rawColumnNames = [NSMutableArray arrayWithCapacity:colCount]; NSMutableArray *rawColumnNames = [NSMutableArray arrayWithCapacity:colCount];
NSMutableArray *queryColumnDetails = [NSMutableArray arrayWithCapacity:colCount]; NSMutableArray *queryColumnDetails = [NSMutableArray arrayWithCapacity:colCount];


useRawDataForColumnAtIndex = malloc(sizeof(BOOL) * colCount); useRawDataForColumnAtIndex = calloc(colCount, sizeof(BOOL));
useRawHexDataForColumnAtIndex = malloc(sizeof(BOOL) * colCount); useRawHexDataForColumnAtIndex = calloc(colCount, sizeof(BOOL));


// Determine whether raw data can be used for each column during processing - safe numbers and hex-encoded data. // Determine whether raw data can be used for each column during processing - safe numbers and hex-encoded data.
for (j = 0; j < colCount; j++) for (j = 0; j < colCount; j++)
Expand Down
11 changes: 6 additions & 5 deletions Source/SPSplitView.m
Expand Up @@ -30,6 +30,7 @@


#import "SPSplitView.h" #import "SPSplitView.h"
#import "SPDateAdditions.h" #import "SPDateAdditions.h"
#include <stdlib.h>


@interface SPSplitView (Private_API) @interface SPSplitView (Private_API)


Expand Down Expand Up @@ -797,9 +798,9 @@ - (NSArray *)_suggestedSizesForTargetSize:(CGFloat)targetSize respectingSpringsA
float viewLength, sizeDifference, totalGive, changedLength; float viewLength, sizeDifference, totalGive, changedLength;
float totalCurrentSize = 0; float totalCurrentSize = 0;
float resizeProportionTotal = 1.f; float resizeProportionTotal = 1.f;
float *originalSizes = malloc(subviewCount * sizeof(float)); float *originalSizes = calloc(subviewCount, sizeof(float));
float *minSizes = malloc(subviewCount * sizeof(float)); float *minSizes = calloc(subviewCount, sizeof(float));
float *maxSizes = malloc(subviewCount * sizeof(float)); float *maxSizes = calloc(subviewCount, sizeof(float));
BOOL *sizesCalculated; BOOL *sizesCalculated;
float *resizeProportions; float *resizeProportions;
NSMutableArray *outputSizes = [NSMutableArray arrayWithCapacity:subviewCount]; NSMutableArray *outputSizes = [NSMutableArray arrayWithCapacity:subviewCount];
Expand Down Expand Up @@ -906,8 +907,8 @@ - (NSArray *)_suggestedSizesForTargetSize:(CGFloat)targetSize respectingSpringsA
} }


// Set up some arrays for fast lookups // Set up some arrays for fast lookups
sizesCalculated = malloc(subviewCount * sizeof(BOOL)); sizesCalculated = calloc(subviewCount, sizeof(BOOL));
resizeProportions = malloc(subviewCount * sizeof(float)); resizeProportions = calloc(subviewCount, sizeof(float));


// Prepopulate them // Prepopulate them
for (i = 0; i < subviewCount; i++) { for (i = 0; i < subviewCount; i++) {
Expand Down
3 changes: 2 additions & 1 deletion Source/SPStringAdditions.m
Expand Up @@ -30,6 +30,7 @@


#import "SPStringAdditions.h" #import "SPStringAdditions.h"
#import "RegexKitLite.h" #import "RegexKitLite.h"
#include <stdlib.h>


static NSInteger _smallestOf(NSInteger a, NSInteger b, NSInteger c); static NSInteger _smallestOf(NSInteger a, NSInteger b, NSInteger c);


Expand Down Expand Up @@ -405,7 +406,7 @@ - (CGFloat)levenshteinDistanceWithWord:(NSString *)stringB


if (n++ != 0 && m++ != 0) if (n++ != 0 && m++ != 0)
{ {
d = malloc(sizeof(NSInteger) * m * n); d = calloc(m * n, sizeof(NSInteger));


for (k = 0; k < n; k++) for (k = 0; k < n; k++)
{ {
Expand Down
7 changes: 4 additions & 3 deletions Source/SPTableContent.m
Expand Up @@ -65,6 +65,7 @@


#import <pthread.h> #import <pthread.h>
#import <SPMySQL/SPMySQL.h> #import <SPMySQL/SPMySQL.h>
#include <stdlib.h>


#ifndef SP_CODA #ifndef SP_CODA
static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOperator"; static NSString *SPTableFilterSetDefaultOperator = @"SPTableFilterSetDefaultOperator";
Expand Down Expand Up @@ -908,7 +909,7 @@ - (void) loadTableValues
BOOL columnsFound = YES; BOOL columnsFound = YES;
NSArray *primaryKeyFieldNames = [selectionToRestore objectForKey:@"keys"]; NSArray *primaryKeyFieldNames = [selectionToRestore objectForKey:@"keys"];
NSUInteger primaryKeyFieldCount = [primaryKeyFieldNames count]; NSUInteger primaryKeyFieldCount = [primaryKeyFieldNames count];
NSUInteger *primaryKeyFieldIndexes = malloc(primaryKeyFieldCount * sizeof(NSUInteger)); NSUInteger *primaryKeyFieldIndexes = calloc(primaryKeyFieldCount, sizeof(NSUInteger));
for (NSUInteger i = 0; i < primaryKeyFieldCount; i++) { for (NSUInteger i = 0; i < primaryKeyFieldCount; i++) {
primaryKeyFieldIndexes[i] = [[tableDataInstance columnNames] indexOfObject:[primaryKeyFieldNames objectAtIndex:i]]; primaryKeyFieldIndexes[i] = [[tableDataInstance columnNames] indexOfObject:[primaryKeyFieldNames objectAtIndex:i]];
if (primaryKeyFieldIndexes[i] == NSNotFound) { if (primaryKeyFieldIndexes[i] == NSNotFound) {
Expand Down Expand Up @@ -3696,7 +3697,7 @@ - (NSDictionary *)selectionDetailsAllowingIndexSelection:(BOOL)allowIndexFallbac


// Set up an array of the column indexes to store // Set up an array of the column indexes to store
NSUInteger primaryKeyFieldCount = [primaryKeyFieldNames count]; NSUInteger primaryKeyFieldCount = [primaryKeyFieldNames count];
NSUInteger *primaryKeyFieldIndexes = malloc(sizeof(NSUInteger) * primaryKeyFieldCount); NSUInteger *primaryKeyFieldIndexes = calloc(primaryKeyFieldCount, sizeof(NSUInteger));
BOOL problemColumns = NO; BOOL problemColumns = NO;
for (NSUInteger i = 0; i < primaryKeyFieldCount; i++) { for (NSUInteger i = 0; i < primaryKeyFieldCount; i++) {
primaryKeyFieldIndexes[i] = [[tableDataInstance columnNames] indexOfObject:[primaryKeyFieldNames objectAtIndex:i]]; primaryKeyFieldIndexes[i] = [[tableDataInstance columnNames] indexOfObject:[primaryKeyFieldNames objectAtIndex:i]];
Expand All @@ -3716,7 +3717,7 @@ - (NSDictionary *)selectionDetailsAllowingIndexSelection:(BOOL)allowIndexFallbac
// Only proceed with key-based selection if there were no problem columns // Only proceed with key-based selection if there were no problem columns
if (!problemColumns) { if (!problemColumns) {
NSIndexSet *selectedRowIndexes = [tableContentView selectedRowIndexes]; NSIndexSet *selectedRowIndexes = [tableContentView selectedRowIndexes];
NSUInteger *indexBuffer = malloc(sizeof(NSUInteger) * [selectedRowIndexes count]); NSUInteger *indexBuffer = calloc([selectedRowIndexes count], sizeof(NSUInteger));
NSUInteger indexCount = [selectedRowIndexes getIndexes:indexBuffer maxCount:[selectedRowIndexes count] inIndexRange:NULL]; NSUInteger indexCount = [selectedRowIndexes getIndexes:indexBuffer maxCount:[selectedRowIndexes count] inIndexRange:NULL];


NSMutableDictionary *selectedRowLookupTable = [NSMutableDictionary dictionaryWithCapacity:indexCount]; NSMutableDictionary *selectedRowLookupTable = [NSMutableDictionary dictionaryWithCapacity:indexCount];
Expand Down

0 comments on commit 3c49b0f

Please sign in to comment.