Skip to content

Commit b5adfd3

Browse files
Kunal MalhotraAndroid Build Coastguard Worker
authored andcommitted
Fixing DatabaseUtils to detect malformed UTF-16 strings
Test: tested with POC in bug, also using atest Bug: 224771621 (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:fb4a72e3943d166088407e61aa4439ac349f3f12) Merged-In: Ide65205b83063801971c5778af3154bcf3f0e530 Change-Id: Ide65205b83063801971c5778af3154bcf3f0e530
1 parent f4a8752 commit b5adfd3

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

core/java/android/database/DatabaseUtils.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,17 +511,31 @@ public static void cursorFillWindow(final Cursor cursor,
511511
*/
512512
public static void appendEscapedSQLString(StringBuilder sb, String sqlString) {
513513
sb.append('\'');
514-
if (sqlString.indexOf('\'') != -1) {
515-
int length = sqlString.length();
516-
for (int i = 0; i < length; i++) {
517-
char c = sqlString.charAt(i);
518-
if (c == '\'') {
519-
sb.append('\'');
514+
int length = sqlString.length();
515+
for (int i = 0; i < length; i++) {
516+
char c = sqlString.charAt(i);
517+
if (Character.isHighSurrogate(c)) {
518+
if (i == length - 1) {
519+
continue;
520+
}
521+
if (Character.isLowSurrogate(sqlString.charAt(i + 1))) {
522+
// add them both
523+
sb.append(c);
524+
sb.append(sqlString.charAt(i + 1));
525+
continue;
526+
} else {
527+
// this is a lone surrogate, skip it
528+
continue;
520529
}
521-
sb.append(c);
522530
}
523-
} else
524-
sb.append(sqlString);
531+
if (Character.isLowSurrogate(c)) {
532+
continue;
533+
}
534+
if (c == '\'') {
535+
sb.append('\'');
536+
}
537+
sb.append(c);
538+
}
525539
sb.append('\'');
526540
}
527541

0 commit comments

Comments
 (0)