Skip to content

Commit b70418c

Browse files
authoredMay 30, 2023
Move the size check to be the first thing checked in CFAllocator functions (#4641)
This saves us a lot of work is size is 0
1 parent c710bcf commit b70418c

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed
 

‎CoreFoundation/Base.subproj/CFBase.c

+23-22
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags
583583
CFAllocatorAllocateCallBack allocateFunc;
584584
void *newptr = NULL;
585585

586+
if (0 < size) {
586587
if (NULL == allocator) {
587588
allocator = __CFGetDefaultAllocator();
588589
}
@@ -594,16 +595,14 @@ void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags
594595
#else
595596
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
596597
#endif
597-
if (0 == size) return NULL;
598598
#if TARGET_OS_MAC
599599
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
600600
return malloc_zone_malloc((malloc_zone_t *)allocator, size);
601601
}
602602
#endif
603-
newptr = NULL;
604603
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
605-
if (allocateFunc) {
606-
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
604+
if (NULL == allocateFunc) return NULL;
605+
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
607606
}
608607
return newptr;
609608
}
@@ -631,14 +630,12 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
631630
return malloc_zone_malloc((malloc_zone_t *)allocator, newsize);
632631
}
633632
#endif
634-
newptr = NULL;
635633
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
636-
if (allocateFunc) {
637-
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
638-
}
634+
if (NULL == allocateFunc) return NULL;
635+
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
639636
return newptr;
640637
}
641-
if (NULL != ptr && 0 == newsize) {
638+
if (NULL != ptr && 0 >= newsize) {
642639
#if TARGET_OS_MAC
643640
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
644641
#if defined(DEBUG)
@@ -655,7 +652,7 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
655652
}
656653
return NULL;
657654
}
658-
if (NULL == ptr && 0 == newsize) return NULL;
655+
if (NULL == ptr && 0 >= newsize) return NULL;
659656
#if TARGET_OS_MAC
660657
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
661658
return malloc_zone_realloc((malloc_zone_t *)allocator, ptr, newsize);
@@ -670,6 +667,7 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
670667
void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) {
671668
CFAllocatorDeallocateCallBack deallocateFunc;
672669

670+
if (NULL != ptr) {
673671
if (NULL == allocator) {
674672
allocator = __CFGetDefaultAllocator();
675673
}
@@ -691,35 +689,38 @@ void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) {
691689
}
692690
#endif
693691
deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context);
694-
if (NULL != ptr && NULL != deallocateFunc) {
692+
if (NULL != deallocateFunc) {
695693
INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info);
696694
}
695+
}
697696
}
698697

699698
CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) {
700699
CFAllocatorPreferredSizeCallBack prefFunc;
701-
CFIndex newsize = 0;
700+
CFIndex newsize;
702701

702+
#if !TARGET_OS_MAC
703+
if (0 >= size) {
704+
return 0;
705+
}
706+
#endif
703707
if (NULL == allocator) {
704708
allocator = __CFGetDefaultAllocator();
705709
}
706-
707-
#if TARGET_OS_MAC
708-
if (_CFTypeGetClass(allocator) == __CFISAForCFAllocator()) {
709-
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
710-
}
711-
#else
712-
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
713-
#endif
714710
#if TARGET_OS_MAC
715711
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
716712
return malloc_good_size(size);
717713
}
714+
if (0 >= size) {
715+
return 0;
716+
}
718717
#endif
718+
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
719719
prefFunc = __CFAllocatorGetPreferredSizeFunction(&allocator->_context);
720-
if (0 < size && NULL != prefFunc) {
721-
newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info));
720+
if (NULL == prefFunc) {
721+
return size;
722722
}
723+
newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info));
723724
if (newsize < size) newsize = size;
724725
return newsize;
725726
}

0 commit comments

Comments
 (0)