Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Avoid crash in NSOrthography category method (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
macdrevx authored Aug 20, 2021
1 parent 1f0d816 commit de911fd
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions platform/darwin/src/NSString+MGLAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ NS_ASSUME_NONNULL_BEGIN

/**
Returns a transliterated representation of the receiver using the specified
script. If transliteration fails, the receiver will be returned.
script. If transliteration fails or script is nil, the receiver will be returned.
Only supports scripts for languages used by Mapbox Streets.
@param script The four-letter code representing the name of the script, as
specified by ISO 15924.
*/
- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script;
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script;

@end

Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/NSString+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (NSString *)mgl_titleCasedStringWithLocale:(NSLocale *)locale {
return string;
}

- (NSString *)mgl_stringByTransliteratingIntoScript:(NSString *)script {
- (NSString *)mgl_stringByTransliteratingIntoScript:(nullable NSString *)script {
NSMutableString *string = self.mutableCopy;
NSStringTransform transform;
if ([script isEqualToString:@"Latn"]) {
Expand Down
2 changes: 2 additions & 0 deletions platform/darwin/test/MGLNSStringAdditionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ - (void)testTransliteratedString {
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Jpan"], @"ロンドン");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Kore"], @"론돈");
XCTAssertEqualObjects([@"ロンドン" mgl_stringByTransliteratingIntoScript:@"Fake"], @"ロンドン");

XCTAssertEqualObjects([@"" mgl_stringByTransliteratingIntoScript:nil], @"");
}

@end
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

* Fixed a CocoaPods warning when integrating this SDK and the Mapbox Navigation SDK for iOS into the same application. ([#549](https://github.com/mapbox/mapbox-gl-native-ios/pull/549))
* Fixed an issue where offline packs were not invalidated before being deallocated, resulting in a crash. ([#620](https://github.com/mapbox/mapbox-gl-native-ios/pull/620))
* Fixed a crash in `-[NSOrthography mgl_dominantScriptForMapboxStreetsLanguage]`. ([#619](https://github.com/mapbox/mapbox-gl-native-ios/pull/619))

## 6.3.0 - November 10, 2020

Expand Down
11 changes: 7 additions & 4 deletions platform/ios/src/MGLMapAccessibilityElement.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
_feature = feature;

NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
NSString *name = [feature attributeForKey:nameAttribute];
NSString *name;
if (languageCode != nil) {
NSString *nameAttribute = [NSString stringWithFormat:@"name_%@", languageCode];
name = [feature attributeForKey:nameAttribute];
}

NSString *dominantScript;
if (name == nil && [feature attributeForKey:@"name"] != nil) {
Expand All @@ -62,7 +65,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// may be in the local language, which may be written in another script.
// Attempt to transform to the script of the preferred language, keeping
// the original string if no transform exists or if transformation fails.
if (!dominantScript) {
if (dominantScript == nil && languageCode != nil) {
dominantScript = [NSOrthography mgl_dominantScriptForMapboxStreetsLanguage:languageCode];
}
name = [name mgl_stringByTransliteratingIntoScript:dominantScript];
Expand All @@ -88,7 +91,7 @@ - (instancetype)initWithAccessibilityContainer:(id)container feature:(id<MGLFeat
// Announce the kind of place or POI.
NSString *languageCode = [MGLVectorTileSource preferredMapboxStreetsLanguage];
NSString *categoryAttribute = [NSString stringWithFormat:@"category_%@", languageCode];
if (attributes[categoryAttribute]) {
if (languageCode != nil && attributes[categoryAttribute]) {
[facts addObject:attributes[categoryAttribute]];
} else if (attributes[@"type"]) {
// FIXME: Unfortunately, these types aren’t a closed set that can be
Expand Down
8 changes: 6 additions & 2 deletions platform/ios/src/NSOrthography+MGLAdditions.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSOrthography (NSOrthography_MGLAdditions)

/**
Returns a four-letter ISO 15924 code representing the name of the dominant
script for a given language.
script for a given language, or nil if language is nil.
On iOS 11 or newer, this method wraps
`+[NSOrthography defaultOrthographyForLanguage:]` and supports any language.
Expand All @@ -13,6 +15,8 @@
@param language The ISO-639 code representing a language.
*/
+ (NSString *)mgl_dominantScriptForMapboxStreetsLanguage:(NSString *)language;
+ (nullable NSString *)mgl_dominantScriptForMapboxStreetsLanguage:(nullable NSString *)language;

@end

NS_ASSUME_NONNULL_END
6 changes: 5 additions & 1 deletion platform/ios/src/NSOrthography+MGLAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

@implementation NSOrthography (MGLAdditions)

+ (NSString *)mgl_dominantScriptForMapboxStreetsLanguage:(NSString *)language {
+ (nullable NSString *)mgl_dominantScriptForMapboxStreetsLanguage:(nullable NSString *)language {
if (language == nil) {
return nil;
}

if (@available(iOS 11.0, *)) {
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:language];
NSOrthography *orthography = [NSOrthography defaultOrthographyForLanguage:locale.localeIdentifier];
Expand Down
4 changes: 4 additions & 0 deletions platform/ios/test/MGLNSOrthographyAdditionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ - (void)testStreetsLanguages {
}
}

- (void)testDominantScriptWhenLanguageIsNil {
XCTAssertNil([NSOrthography mgl_dominantScriptForMapboxStreetsLanguage:nil]);
}

@end

0 comments on commit de911fd

Please sign in to comment.