From da2528cbe259d4b98f99199a0277c4f92afef2b2 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Fri, 19 Apr 2019 02:06:38 +0100 Subject: [PATCH] Add defensive checks to MacOSStringCompare --- src/os/macosx/string_osx.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/os/macosx/string_osx.cpp b/src/os/macosx/string_osx.cpp index 6d90b91a8e..ec9753685d 100644 --- a/src/os/macosx/string_osx.cpp +++ b/src/os/macosx/string_osx.cpp @@ -15,6 +15,7 @@ #include "../../strings_func.h" #include "../../table/control_codes.h" #include "../../fontcache.h" +#include "../../scope.h" #include "macos.h" #include @@ -301,16 +302,23 @@ int MacOSStringCompare(const char *s1, const char *s2) static bool supported = MacOSVersionIsAtLeast(10, 5, 0); if (!supported) return 0; + if (_osx_locale == nullptr) return 0; + CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; CFStringRef cf1 = CFStringCreateWithCString(kCFAllocatorDefault, s1, kCFStringEncodingUTF8); + if (cf1 == nullptr) return 0; + auto guard1 = scope_guard([&]() { + CFRelease(cf1); + }); CFStringRef cf2 = CFStringCreateWithCString(kCFAllocatorDefault, s2, kCFStringEncodingUTF8); + if (cf2 == nullptr) return 0; + auto guard2 = scope_guard([&]() { + CFRelease(cf2); + }); CFComparisonResult res = CFStringCompareWithOptionsAndLocale(cf1, cf2, CFRangeMake(0, CFStringGetLength(cf1)), flags, _osx_locale); - CFRelease(cf1); - CFRelease(cf2); - return (int)res + 2; }