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

Commit

Permalink
Java: Use StringBuildler in decodeURI and guard against H(null)L pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Dec 19, 2019
1 parent 477b5a6 commit fa122d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions java/src/name/fraser/neil/plaintext/diff_match_patch.java
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ private int digit16(char b) throws IllegalArgumentException {

private String decodeURI(String text) throws IllegalArgumentException {
int i = 0;
StringBuffer decoded = new StringBuffer("");
StringBuilder decoded = new StringBuilder(text.length());

while (i < text.length()) {
if (text.charAt(i) != '%') {
Expand Down Expand Up @@ -1576,7 +1576,16 @@ private String decodeURI(String text) throws IllegalArgumentException {
throw new IllegalArgumentException();
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ public static void testDiffDelta() {
dmp.diff_toDelta(dmp.diff_fromDelta("\ud83c\udd70", "=1\t-1\t+%ED%B5%B1"))
);

assertEquals(
"diff_fromDelta: Invalid diff from objective-c with (null) string",
diffList(new Diff(INSERT, "\ud83c\udd70")),
dmp.diff_fromDelta("", "+%ED%A0%BC%28null%29%ED%B5%B0")
);

// Verify pool of unchanged characters.
diffs = diffList(new Diff(INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "));
String text2 = dmp.diff_text2(diffs);
Expand Down

0 comments on commit fa122d3

Please sign in to comment.