Skip to content

Move the size check to be the first thing checked in CFAllocator functions #4641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions CoreFoundation/Base.subproj/CFBase.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags
CFAllocatorAllocateCallBack allocateFunc;
void *newptr = NULL;

if (0 < size) {
if (NULL == allocator) {
allocator = __CFGetDefaultAllocator();
}
Expand All @@ -594,16 +595,14 @@ void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags
#else
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
#endif
if (0 == size) return NULL;
#if TARGET_OS_MAC
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
return malloc_zone_malloc((malloc_zone_t *)allocator, size);
}
#endif
newptr = NULL;
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
if (allocateFunc) {
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
if (NULL == allocateFunc) return NULL;
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
}
return newptr;
}
Expand Down Expand Up @@ -631,14 +630,12 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
return malloc_zone_malloc((malloc_zone_t *)allocator, newsize);
}
#endif
newptr = NULL;
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
if (allocateFunc) {
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
}
if (NULL == allocateFunc) return NULL;
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
return newptr;
}
if (NULL != ptr && 0 == newsize) {
if (NULL != ptr && 0 >= newsize) {
#if TARGET_OS_MAC
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
#if defined(DEBUG)
Expand All @@ -655,7 +652,7 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
}
return NULL;
}
if (NULL == ptr && 0 == newsize) return NULL;
if (NULL == ptr && 0 >= newsize) return NULL;
#if TARGET_OS_MAC
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
return malloc_zone_realloc((malloc_zone_t *)allocator, ptr, newsize);
Expand All @@ -670,6 +667,7 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) {
CFAllocatorDeallocateCallBack deallocateFunc;

if (NULL != ptr) {
if (NULL == allocator) {
allocator = __CFGetDefaultAllocator();
}
Expand All @@ -691,35 +689,38 @@ void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) {
}
#endif
deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context);
if (NULL != ptr && NULL != deallocateFunc) {
if (NULL != deallocateFunc) {
INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info);
}
}
}

CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) {
CFAllocatorPreferredSizeCallBack prefFunc;
CFIndex newsize = 0;
CFIndex newsize;

#if !TARGET_OS_MAC
if (0 >= size) {
return 0;
}
#endif
if (NULL == allocator) {
allocator = __CFGetDefaultAllocator();
}

#if TARGET_OS_MAC
if (_CFTypeGetClass(allocator) == __CFISAForCFAllocator()) {
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
}
#else
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
#endif
#if TARGET_OS_MAC
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
return malloc_good_size(size);
}
if (0 >= size) {
return 0;
}
#endif
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
prefFunc = __CFAllocatorGetPreferredSizeFunction(&allocator->_context);
if (0 < size && NULL != prefFunc) {
newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info));
if (NULL == prefFunc) {
return size;
}
newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info));
if (newsize < size) newsize = size;
return newsize;
}
Expand Down