Skip to content
Permalink
Browse files

Replace some malloc(a*b) with the safer calloc(a,b) variant

  • Loading branch information...
dmoagx committed Jun 30, 2015
1 parent 6d372fa commit 3c49b0f2bc16777f83cf57825448b6689ed344e1
@@ -32,6 +32,7 @@
#import "SPMySQL Private APIs.h"
#import "SPMySQLArrayAdditions.h"
#include <pthread.h>
#include <stdlib.h>

static id NSNullPointer;

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

// 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++) {
if (theRow[i] != NULL) {
memcpy(newRowStore->data+dataCopiedLength, theRow[i], fieldLengths[i]);
@@ -53,6 +53,7 @@

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

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

// 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++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];

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

// 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++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];

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

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

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

// 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++ )
columnMappings[c] = (NSUInteger)[[NSArrayObjectAtIndex(columns, c) identifier] integerValue];

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

@implementation NSData (SPDataAdditions)

@@ -247,7 +248,7 @@ - (NSString *)dataToFormattedHexString
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)];

@@ -29,6 +29,7 @@
// More info at <https://github.com/sequelpro/sequelpro>

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

@implementation SPEncodingPopupAccessory

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

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

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

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

@@ -37,6 +37,7 @@
#import "RegexKitLite.h"

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

@interface SPSQLExporter ()

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

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

// Determine whether raw data can be used for each column during processing - safe numbers and hex-encoded data.
for (j = 0; j < colCount; j++)
@@ -30,6 +30,7 @@

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

@interface SPSplitView (Private_API)

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

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

// Prepopulate them
for (i = 0; i < subviewCount; i++) {
@@ -30,6 +30,7 @@

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

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

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

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

for (k = 0; k < n; k++)
{
@@ -65,6 +65,7 @@

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

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

// Set up an array of the column indexes to store
NSUInteger primaryKeyFieldCount = [primaryKeyFieldNames count];
NSUInteger *primaryKeyFieldIndexes = malloc(sizeof(NSUInteger) * primaryKeyFieldCount);
NSUInteger *primaryKeyFieldIndexes = calloc(primaryKeyFieldCount, sizeof(NSUInteger));
BOOL problemColumns = NO;
for (NSUInteger i = 0; i < primaryKeyFieldCount; i++) {
primaryKeyFieldIndexes[i] = [[tableDataInstance columnNames] indexOfObject:[primaryKeyFieldNames objectAtIndex:i]];
@@ -3716,7 +3717,7 @@ - (NSDictionary *)selectionDetailsAllowingIndexSelection:(BOOL)allowIndexFallbac
// Only proceed with key-based selection if there were no problem columns
if (!problemColumns) {
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];

NSMutableDictionary *selectedRowLookupTable = [NSMutableDictionary dictionaryWithCapacity:indexCount];

0 comments on commit 3c49b0f

Please sign in to comment.
You can’t perform that action at this time.