Skip to content

Commit bd46462

Browse files
committed
remove usages of unsafe retain count access in CFAttributedString
1 parent 9a15921 commit bd46462

File tree

1 file changed

+40
-61
lines changed

1 file changed

+40
-61
lines changed

CoreFoundation/String.subproj/CFAttributedString.c

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -491,36 +491,29 @@ void CFAttributedStringSetAttributes(CFMutableAttributedStringRef attrStr, CFRan
491491
CFIndex numAdditionalItems = CFDictionaryGetCount(replacementAttrs);
492492
if (numAdditionalItems) {
493493
// Extract the new keys and values so we don't do it over and over for each range
494-
createLocalArray(additionalKeys, numAdditionalItems);
495-
createLocalArray(additionalValues, numAdditionalItems);
494+
createLocalArray(additionalKeys, numAdditionalItems);
495+
createLocalArray(additionalValues, numAdditionalItems);
496496
CFDictionaryGetKeysAndValues(replacementAttrs, additionalKeys, additionalValues);
497-
498-
// CFAttributedStringBeginEditing(attrStr);
497+
498+
// CFAttributedStringBeginEditing(attrStr);
499499
while (range.length) {
500500
CFRange effectiveRange;
501501
CFMutableDictionaryRef attrs = (CFMutableDictionaryRef)CFRunArrayGetValueAtIndex(attrStr->attributeArray, range.location, &effectiveRange, NULL);
502-
if (effectiveRange.location == range.location && effectiveRange.length <= range.length && CFGetRetainCount(attrs) == 1) {
503-
// !!! Retain count check cheesy; need to keep our own reference count
504-
// The attributes dictionary can just be modified in place
505-
// Here we are assuming that CFRunArray is not under-reporting the effectiveRange for a given item!
506-
__CFDictionaryAddMultiple(attrs, additionalKeys, additionalValues, numAdditionalItems);
507-
} else {
508-
// Intersect effectiveRange and range
509-
if (effectiveRange.location < range.location) {
510-
effectiveRange.length -= (range.location - effectiveRange.location);
511-
effectiveRange.location = range.location;
512-
}
513-
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
514-
// We need to make a new copy
515-
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
516-
__CFDictionaryAddMultiple(attrs, additionalKeys, additionalValues, numAdditionalItems);
517-
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
518-
CFRelease(attrs);
502+
// Intersect effectiveRange and range
503+
if (effectiveRange.location < range.location) {
504+
effectiveRange.length -= (range.location - effectiveRange.location);
505+
effectiveRange.location = range.location;
519506
}
507+
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
508+
// We need to make a new copy
509+
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
510+
__CFDictionaryAddMultiple(attrs, additionalKeys, additionalValues, numAdditionalItems);
511+
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
512+
CFRelease(attrs);
520513
range.length -= effectiveRange.length;
521514
range.location += effectiveRange.length;
522515
}
523-
// CFAttributedStringEndEditing(attrStr);
516+
// CFAttributedStringEndEditing(attrStr);
524517

525518
freeLocalArray(additionalKeys);
526519
freeLocalArray(additionalValues);
@@ -538,27 +531,20 @@ void CFAttributedStringSetAttribute(CFMutableAttributedStringRef attrStr, CFRang
538531
CFRange effectiveRange;
539532
// effectiveRange.location returned here may be equal to or smaller than range.location
540533
CFMutableDictionaryRef attrs = (CFMutableDictionaryRef)CFRunArrayGetValueAtIndex(attrStr->attributeArray, range.location, &effectiveRange, NULL);
541-
if (effectiveRange.location == range.location && effectiveRange.length <= range.length && CFGetRetainCount(attrs) == 1) {
542-
// !!! Retain count check cheesy; need to keep our own reference count)
543-
// The attributes dictionary can just be modified in place
544-
// Here we are assuming that CFRunArray is not under-reporting the effectiveRange for a given item!
534+
// Intersect effectiveRange and range
535+
if (effectiveRange.location < range.location) {
536+
effectiveRange.length -= (range.location - effectiveRange.location);
537+
effectiveRange.location = range.location;
538+
}
539+
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
540+
// First check to see if the same value already exists; this will avoid a copy
541+
CFTypeRef existingValue = CFDictionaryGetValue(attrs, attrName);
542+
if (!existingValue || !CFEqual(existingValue, value)) {
543+
// We need to make a new copy
544+
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
545545
CFDictionarySetValue(attrs, attrName, value);
546-
} else {
547-
// Intersect effectiveRange and range
548-
if (effectiveRange.location < range.location) {
549-
effectiveRange.length -= (range.location - effectiveRange.location);
550-
effectiveRange.location = range.location;
551-
}
552-
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
553-
// First check to see if the same value already exists; this will avoid a copy
554-
CFTypeRef existingValue = CFDictionaryGetValue(attrs, attrName);
555-
if (!existingValue || !CFEqual(existingValue, value)) {
556-
// We need to make a new copy
557-
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
558-
CFDictionarySetValue(attrs, attrName, value);
559-
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
560-
CFRelease(attrs);
561-
}
546+
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
547+
CFRelease(attrs);
562548
}
563549
range.length -= effectiveRange.length;
564550
range.location += effectiveRange.length;
@@ -575,26 +561,19 @@ void CFAttributedStringRemoveAttribute(CFMutableAttributedStringRef attrStr, CFR
575561
while (range.length) {
576562
CFRange effectiveRange;
577563
CFMutableDictionaryRef attrs = (CFMutableDictionaryRef)CFRunArrayGetValueAtIndex(attrStr->attributeArray, range.location, &effectiveRange, NULL);
578-
if (effectiveRange.location == range.location && effectiveRange.length <= range.length && CFGetRetainCount(attrs) == 1) {
579-
// !!! Retain count check cheesy; need to keep our own reference count
580-
// The attributes dictionary can just be modified in place
581-
// Here we are assuming that CFRunArray is not under-reporting the effectiveRange for a given item!
564+
// Intersect effectiveRange and range
565+
if (effectiveRange.location < range.location) {
566+
effectiveRange.length -= (range.location - effectiveRange.location);
567+
effectiveRange.location = range.location;
568+
}
569+
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
570+
// First check to see if the value is not there; this will avoid a copy
571+
if (CFDictionaryContainsKey(attrs, attrName)) {
572+
// We need to make a new copy
573+
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
582574
CFDictionaryRemoveValue(attrs, attrName);
583-
} else {
584-
// Intersect effectiveRange and range
585-
if (effectiveRange.location < range.location) {
586-
effectiveRange.length -= (range.location - effectiveRange.location);
587-
effectiveRange.location = range.location;
588-
}
589-
if (effectiveRange.length > range.length) effectiveRange.length = range.length;
590-
// First check to see if the value is not there; this will avoid a copy
591-
if (CFDictionaryContainsKey(attrs, attrName)) {
592-
// We need to make a new copy
593-
attrs = __CFAttributedStringCreateAttributesDictionary(CFGetAllocator(attrStr), attrs);
594-
CFDictionaryRemoveValue(attrs, attrName);
595-
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
596-
CFRelease(attrs);
597-
}
575+
CFRunArrayReplace(attrStr->attributeArray, effectiveRange, attrs, effectiveRange.length);
576+
CFRelease(attrs);
598577
}
599578
range.length -= effectiveRange.length;
600579
range.location += effectiveRange.length;

0 commit comments

Comments
 (0)