Skip to content

Commit ec6a57d

Browse files
committed
Use correct type for receivers that best matches its data.
For example, when not passing the data to other CF functions, the length of a C string can be size_t as opposed to CFIndex. Additionally, where appropriate, I replaced a for loop with memcpy to safely copy data between two variables of different data types.
1 parent 01f6389 commit ec6a57d

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

CoreFoundation/Base.subproj/CFSortFunctions.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ static void __CFSortIndexesNMerge(VALUE_TYPE listp1[], INDEX_TYPE cnt1, VALUE_TY
223223
/* Merging algorithm based on
224224
"A New Parallel Sorting Algorithm based on Odd-Even Mergesort", Ezequiel Herruzo, et al
225225
*/
226-
static void __CFSortIndexesN(VALUE_TYPE listp[], INDEX_TYPE count, int32_t ncores, CMP_RESULT_TYPE (^cmp)(INDEX_TYPE, INDEX_TYPE)) {
226+
static void __CFSortIndexesN(VALUE_TYPE listp[], INDEX_TYPE count, INDEX_TYPE ncores, CMP_RESULT_TYPE (^cmp)(INDEX_TYPE, INDEX_TYPE)) {
227227
/* Divide the array up into up to ncores, multiple-of-16-sized, chunks */
228-
INDEX_TYPE sz = ((((count + ncores - 1) / ncores) + 15) / 16) * 16;
228+
INDEX_TYPE sz = (((count + ncores - 1) / ncores) + 15) & ~15;
229229
INDEX_TYPE num_sect = (count + sz - 1) / sz;
230230
INDEX_TYPE last_sect_len = count + sz - sz * num_sect;
231231

@@ -260,7 +260,7 @@ static void __CFSortIndexesN(VALUE_TYPE listp[], INDEX_TYPE count, int32_t ncore
260260
INDEX_TYPE sect2_len = (sect + 1 + (right ? 1 : 2) == num_sect) ? last_sect_len : sz;
261261
__CFSortIndexesNMerge(left_base, sz, right_base, sect2_len, listp + sect * sz + sz, right, cmp);
262262
});
263-
memmove(listp + 0 * sz, tmps[0], sz * sizeof(VALUE_TYPE));
263+
memmove(listp, tmps[0], sz * sizeof(VALUE_TYPE));
264264
if (!(num_sect & 0x1)) {
265265
memmove(listp + (num_sect - 1) * sz, tmps[num_sect - 1], last_sect_len * sizeof(VALUE_TYPE));
266266
}
@@ -285,7 +285,7 @@ _CF_SORT_INDEXES_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count,
285285
CRSetCrashLogMessage("Size of array to be sorted is too big");
286286
HALT;
287287
}
288-
int32_t ncores = 0;
288+
CFIndex ncores = 0;
289289
if (opts & kCFSortConcurrent) {
290290
ncores = __CFActiveProcessorCount();
291291
if (count < 160 || ncores < 2) {
@@ -307,7 +307,7 @@ _CF_SORT_INDEXES_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count,
307307
} else {
308308
/* Specifically hard-coded to 8; the count has to be very large before more chunks and/or cores is worthwhile. */
309309
dispatch_queue_t q = dispatch_queue_create(__CF_QUEUE_NAME("NSSortIndexes"), DISPATCH_QUEUE_CONCURRENT);
310-
CFIndex sz = ((((size_t)count + 15) / 16) * 16) / 8;
310+
CFIndex sz = ((count + 15) >> 3) & ~1;
311311
dispatch_apply(8, DISPATCH_APPLY_AUTO, ^(size_t n) {
312312
CFIndex idx = n * sz, lim = __CFMin(idx + sz, count);
313313
for (; idx < lim; idx++) indexBuffer[idx] = idx;

CoreFoundation/Base.subproj/CFUUID.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ CFUUIDRef CFUUIDCreateWithBytes(CFAllocatorRef alloc, uint8_t byte0, uint8_t byt
198198
return __CFUUIDCreateWithBytesPrimitive(alloc, bytes, false);
199199
}
200200

201-
static void _intToHexChars(UInt32 in, UniChar *out, int digits) {
202-
int shift;
201+
static void _intToHexChars(UInt32 in, UniChar *out, unsigned int digits) {
202+
unsigned int shift;
203203
UInt32 d;
204-
205-
while (--digits >= 0) {
204+
205+
while (digits--) {
206206
shift = digits << 2;
207207
d = 0x0FL & (in >> shift);
208208
if (d <= 9) {

CoreFoundation/PlugIn.subproj/CFBundle_Binary.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ static char *_cleanedPathForPath(const char *curName) {
110110
char *thePath = strdup(curName);
111111
if (thePath) {
112112
// We are going to process the buffer replacing all "/./" and "//" with "/"
113-
CFIndex srcIndex = 0, dstIndex = 0;
114-
CFIndex len = strlen(thePath);
113+
size_t srcIndex = 0, dstIndex = 0;
114+
size_t len = strlen(thePath);
115115
for (srcIndex=0; srcIndex<len; srcIndex++) {
116116
thePath[dstIndex] = thePath[srcIndex];
117117
dstIndex++;
118-
while (srcIndex < len-1 && thePath[srcIndex] == '/' && (thePath[srcIndex+1] == '/' || (thePath[srcIndex+1] == '.' && srcIndex < len-2 && thePath[srcIndex+2] == '/'))) srcIndex += (thePath[srcIndex+1] == '/' ? 1 : 2);
118+
while (srcIndex + 1 < len && thePath[srcIndex] == '/' && (thePath[srcIndex+1] == '/' || (thePath[srcIndex+1] == '.' && srcIndex + 2 < len && thePath[srcIndex+2] == '/'))) srcIndex += (thePath[srcIndex+1] == '/' ? 1 : 2);
119119
}
120120
thePath[dstIndex] = 0;
121121
}

CoreFoundation/String.subproj/CFString.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,16 +1815,17 @@ static Boolean __cStrEqual(const void *ptr1, const void *ptr2) {
18151815
static CFHashCode __cStrHash(const void *ptr) {
18161816
// It doesn't quite matter if we convert to Unicode correctly, as long as we do it consistently
18171817
const char *cStr = (const char *)ptr;
1818-
CFIndex len = strlen(cStr);
1818+
size_t len = strlen(cStr);
18191819
CFHashCode result = 0;
18201820
if (len <= 4) { // All chars
1821-
unsigned cnt = len;
1822-
while (cnt--) result += (result << 8) + *cStr++;
1821+
for (size_t cnt = len; cnt; cnt--) {
1822+
result += (result << 8) | *cStr++;
1823+
}
18231824
} else { // First and last 2 chars
1824-
result += (result << 8) + cStr[0];
1825-
result += (result << 8) + cStr[1];
1826-
result += (result << 8) + cStr[len-2];
1827-
result += (result << 8) + cStr[len-1];
1825+
result += (result << 8) | cStr[0];
1826+
result += (result << 8) | cStr[1];
1827+
result += (result << 8) | cStr[len-2];
1828+
result += (result << 8) | cStr[len-1];
18281829
}
18291830
result += (result << (len & 31));
18301831
return result;

CoreFoundation/String.subproj/CFStringTransform.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ __CFStringTransformCreate(CFStringRef identifier, bool reverse) {
8383
if (known) {
8484
UniChar buff[kCFStringTransformStackBufferSize];
8585
CFIndex len = strlen(known);
86-
CFIndex idx;
87-
for (idx = 0; idx < len; idx++) {
88-
buff[idx] = known[idx];
89-
}
86+
memcpy(buff, known, len);
9087
made = utrans_openU((const UChar *)buff, len, reverse?UTRANS_REVERSE:UTRANS_FORWARD, NULL, 0, NULL, &icuStatus);
9188
} else {
9289
CFIndex len = CFStringGetLength(identifier);

0 commit comments

Comments
 (0)