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

Commit fa122d3

Browse files
committed
Java: Use StringBuildler in decodeURI and guard against H(null)L pattern
1 parent 477b5a6 commit fa122d3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

java/src/name/fraser/neil/plaintext/diff_match_patch.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ private int digit16(char b) throws IllegalArgumentException {
15071507

15081508
private String decodeURI(String text) throws IllegalArgumentException {
15091509
int i = 0;
1510-
StringBuffer decoded = new StringBuffer("");
1510+
StringBuilder decoded = new StringBuilder(text.length());
15111511

15121512
while (i < text.length()) {
15131513
if (text.charAt(i) != '%') {
@@ -1576,7 +1576,16 @@ private String decodeURI(String text) throws IllegalArgumentException {
15761576
throw new IllegalArgumentException();
15771577
}
15781578

1579-
return decoded.toString();
1579+
// some objective-c versions of the library produced patches with
1580+
// (null) in the place where surrogates were split across diff
1581+
// boundaries. if we leave those in we'll be stuck with a
1582+
// high-surrogate (null) low-surrogate pattern that will break
1583+
// deeper in the library or consuming application. we'll "fix"
1584+
// these by dropping the (null) and re-joining the surrogate halves
1585+
return decoded.toString().replaceAll(
1586+
"([\\uD800-\\uDBFF])\\(null\\)([\\uDC00-\\uDFFF])",
1587+
"$1$2"
1588+
);
15801589
}
15811590

15821591
/**

java/tests/name/fraser/neil/plaintext/diff_match_patch_test.java

+6
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,12 @@ public static void testDiffDelta() {
460460
dmp.diff_toDelta(dmp.diff_fromDelta("\ud83c\udd70", "=1\t-1\t+%ED%B5%B1"))
461461
);
462462

463+
assertEquals(
464+
"diff_fromDelta: Invalid diff from objective-c with (null) string",
465+
diffList(new Diff(INSERT, "\ud83c\udd70")),
466+
dmp.diff_fromDelta("", "+%ED%A0%BC%28null%29%ED%B5%B0")
467+
);
468+
463469
// Verify pool of unchanged characters.
464470
diffs = diffList(new Diff(INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "));
465471
String text2 = dmp.diff_text2(diffs);

0 commit comments

Comments
 (0)