Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 19606f0

Browse files
committed
ObjectiveC: Guard against high-surrogate(null)low-surrogate
1 parent 9703fc4 commit 19606f0

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

objectivec/DiffMatchPatch.m

+18-2
Original file line numberDiff line numberDiff line change
@@ -1492,8 +1492,24 @@ - (NSString *)diff_decodeURIWithText:(NSString *)percentEncoded
14921492
@catch (NSException *e) {
14931493
return nil;
14941494
}
1495-
1496-
return [NSString stringWithCharacters:decoded length:output];
1495+
1496+
// some objective-c versions of the library produced patches with
1497+
// (null) in the place where surrogates were split across diff
1498+
// boundaries. if we leave those in we'll be stuck with a
1499+
// high-surrogate (null) low-surrogate pattern that will break
1500+
// deeper in the library or consumping application. we'll "fix"
1501+
// these by dropping the (null) and re-joining the surrogate halves
1502+
NSString *result = [NSString stringWithCharacters:decoded length:output];
1503+
NSRegularExpression *replacer = [NSRegularExpression
1504+
regularExpressionWithPattern:@"([\\x{D800}-\\x{DBFF}])\\(null\\)([\\x{DC00}-\\x{DFFF}])"
1505+
options:0
1506+
error:nil];
1507+
1508+
return [replacer
1509+
stringByReplacingMatchesInString:result
1510+
options:0
1511+
range:NSMakeRange(0, [result length])
1512+
withTemplate:@"$1$2"];
14971513
}
14981514

14991515
/**

objectivec/Tests/DiffMatchPatchTest.m

+4
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,10 @@ - (void)test_diff_deltaTest {
814814
[Diff diffWithOperation:DIFF_INSERT andText:[NSString stringWithFormat:@"%C", 0xdd71]],
815815
nil])]);
816816

817+
// Invalid diff from objective-c with (null) string
818+
XCTAssertEqualObjects([dmp diff_fromDeltaWithText:@"" andDelta:@"+%ED%A0%BC%28null%29%ED%B5%B0" error:nil],
819+
([NSMutableArray arrayWithObjects:[Diff diffWithOperation:DIFF_INSERT andText:@"🅰"],nil]));
820+
817821
// Verify pool of unchanged characters.
818822
diffs = [NSMutableArray arrayWithObject:
819823
[Diff diffWithOperation:DIFF_INSERT andText:@"A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "]];

0 commit comments

Comments
 (0)