diff options
author | Oleg Oshmyan <chortos@inbox.lv> | 2021-03-27 05:08:55 +0200 |
---|---|---|
committer | Oleg Oshmyan <chortos@inbox.lv> | 2021-04-29 03:18:15 +0300 |
commit | aa4c9bfefb3b2d652aeac342fdf9f33987507f72 (patch) | |
tree | 818b78979e6240b679b18838e76aed863403ba53 | |
parent | db85ef4736108c12bf3e12939447840815c246ec (diff) | |
download | libass-aa4c9bfefb3b2d652aeac342fdf9f33987507f72.tar.bz2 libass-aa4c9bfefb3b2d652aeac342fdf9f33987507f72.tar.xz |
coretext: check all allocation failures and release sooner
All Create and Copy functions create new objects, so all of them may fail.
Keep using SAFE_CFRelease only in shared cleanup clauses.
-rw-r--r-- | libass/ass_coretext.c | 98 |
1 files changed, 66 insertions, 32 deletions
diff --git a/libass/ass_coretext.c b/libass/ass_coretext.c index f1063ba..64ec8ee 100644 --- a/libass/ass_coretext.c +++ b/libass/ass_coretext.c @@ -49,7 +49,8 @@ static char *cfstr2buf(CFStringRef string) size_t len = CFStringGetLength(string); CFIndex buf_len = CFStringGetMaximumSizeForEncoding(len, encoding); char *buf = malloc(buf_len); - CFStringGetCString(string, buf, buf_len, encoding); + if (buf) + CFStringGetCString(string, buf, buf_len, encoding); return buf; } } @@ -57,7 +58,7 @@ static char *cfstr2buf(CFStringRef string) static void destroy_font(void *priv) { CTFontDescriptorRef fontd = priv; - SAFE_CFRelease(fontd); + CFRelease(fontd); } static bool is_postscript_font_format(CFNumberRef cfformat) @@ -76,8 +77,10 @@ static bool check_postscript(void *priv) CTFontDescriptorRef fontd = priv; CFNumberRef cfformat = CTFontDescriptorCopyAttribute(fontd, kCTFontFormatAttribute); + if (!cfformat) + return false; bool ret = is_postscript_font_format(cfformat); - SAFE_CFRelease(cfformat); + CFRelease(cfformat); return ret; } @@ -94,7 +97,7 @@ static bool check_glyph(void *priv, uint32_t code) return true; bool result = CFCharacterSetIsLongCharacterMember(set, code); - SAFE_CFRelease(set); + CFRelease(set); return result; } @@ -104,13 +107,11 @@ static char *get_font_file(CTFontDescriptorRef fontd) if (!url) return NULL; CFStringRef path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle); - if (!path) { - SAFE_CFRelease(url); + CFRelease(url); + if (!path) return NULL; - } char *buffer = cfstr2buf(path); - SAFE_CFRelease(path); - SAFE_CFRelease(url); + CFRelease(path); return buffer; } @@ -120,7 +121,7 @@ static char *get_name(CTFontDescriptorRef fontd, CFStringRef attr) CFStringRef name = CTFontDescriptorCopyAttribute(fontd, attr); if (name) { ret = cfstr2buf(name); - SAFE_CFRelease(name); + CFRelease(name); } return ret; } @@ -205,45 +206,61 @@ static void match_fonts(void *priv, ASS_Library *lib, ASS_FontProvider *provider { FT_Library ftlib = priv; + CFStringRef cfname = + CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8); + if (!cfname) + return; + enum { attributes_n = 3 }; - CTFontDescriptorRef ctdescrs[attributes_n]; - CFMutableDictionaryRef cfattrs[attributes_n]; + CTFontDescriptorRef ctdescrs[attributes_n] = {0}; CFStringRef attributes[attributes_n] = { kCTFontFamilyNameAttribute, kCTFontDisplayNameAttribute, kCTFontNameAttribute, }; - CFStringRef cfname = - CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8); + CFArrayRef descriptors = NULL; + CTFontCollectionRef ctcoll = NULL; + CFArrayRef fontsd = NULL; for (int i = 0; i < attributes_n; i++) { - cfattrs[i] = CFDictionaryCreateMutable(NULL, 0, 0, 0); - CFDictionaryAddValue(cfattrs[i], attributes[i], cfname); - ctdescrs[i] = CTFontDescriptorCreateWithAttributes(cfattrs[i]); + CFDictionaryRef cfattrs = + CFDictionaryCreate(NULL, + (const void **)&attributes[i], + (const void **)&cfname, + 1, NULL, NULL); + if (!cfattrs) + goto cleanup; + ctdescrs[i] = CTFontDescriptorCreateWithAttributes(cfattrs); + CFRelease(cfattrs); + if (!ctdescrs[i]) + goto cleanup; } - CFArrayRef descriptors = + descriptors = CFArrayCreate(NULL, (const void **)&ctdescrs, attributes_n, NULL); + if (!descriptors) + goto cleanup; - CTFontCollectionRef ctcoll = - CTFontCollectionCreateWithFontDescriptors(descriptors, 0); + ctcoll = CTFontCollectionCreateWithFontDescriptors(descriptors, 0); + if (!ctcoll) + goto cleanup; - CFArrayRef fontsd = - CTFontCollectionCreateMatchingFontDescriptors(ctcoll); + fontsd = CTFontCollectionCreateMatchingFontDescriptors(ctcoll); + if (!fontsd) + goto cleanup; process_descriptors(lib, ftlib, provider, fontsd); +cleanup: SAFE_CFRelease(fontsd); SAFE_CFRelease(ctcoll); - for (int i = 0; i < attributes_n; i++) { - SAFE_CFRelease(cfattrs[i]); + for (int i = 0; i < attributes_n; i++) SAFE_CFRelease(ctdescrs[i]); - } SAFE_CFRelease(descriptors); - SAFE_CFRelease(cfname); + CFRelease(cfname); } static char *get_fallback(void *priv, ASS_Library *lib, @@ -253,13 +270,34 @@ static char *get_fallback(void *priv, ASS_Library *lib, CFStringRef name = CFStringCreateWithBytes( 0, (UInt8 *)family, strlen(family), kCFStringEncodingUTF8, false); + if (!name) + return NULL; + CTFontRef font = CTFontCreateWithName(name, 0, NULL); + CFRelease(name); + if (!font) + return NULL; + uint32_t codepointle = OSSwapHostToLittleInt32(codepoint); + CFStringRef r = CFStringCreateWithBytes( 0, (UInt8*)&codepointle, sizeof(codepointle), kCFStringEncodingUTF32LE, false); + if (!r) { + CFRelease(font); + return NULL; + } + CTFontRef fb = CTFontCreateForString(font, r, CFRangeMake(0, 1)); + CFRelease(font); + CFRelease(r); + if (!fb) + return NULL; + CTFontDescriptorRef fontd = CTFontCopyFontDescriptor(fb); + CFRelease(fb); + if (!fontd) + return NULL; char *res_name = NULL; char *path = NULL; @@ -267,6 +305,8 @@ static char *get_fallback(void *priv, ASS_Library *lib, if (get_font_info_ct(lib, ftlib, fontd, &path, &meta)) res_name = meta.families[0]; + CFRelease(fontd); + for (int i = 1 /* skip res_name */; i < meta.n_family; i++) free(meta.families[i]); @@ -280,12 +320,6 @@ static char *get_fallback(void *priv, ASS_Library *lib, free(path); - SAFE_CFRelease(name); - SAFE_CFRelease(font); - SAFE_CFRelease(r); - SAFE_CFRelease(fb); - SAFE_CFRelease(fontd); - return res_name; } |