Skip to content

Commit

Permalink
Fixing DatabaseUtils to detect malformed UTF-16 strings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Kunal Malhotra authored and thestinger committed Oct 3, 2023
1 parent e7a1aa9 commit 922a786
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions core/java/android/database/DatabaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,31 @@ public static void cursorFillWindow(final Cursor cursor,
*/
public static void appendEscapedSQLString(StringBuilder sb, String sqlString) {
sb.append('\'');
if (sqlString.indexOf('\'') != -1) {
int length = sqlString.length();
for (int i = 0; i < length; i++) {
char c = sqlString.charAt(i);
if (c == '\'') {
sb.append('\'');
int length = sqlString.length();
for (int i = 0; i < length; i++) {
char c = sqlString.charAt(i);
if (Character.isHighSurrogate(c)) {
if (i == length - 1) {
continue;
}
if (Character.isLowSurrogate(sqlString.charAt(i + 1))) {
// add them both
sb.append(c);
sb.append(sqlString.charAt(i + 1));
continue;
} else {
// this is a lone surrogate, skip it
continue;
}
sb.append(c);
}
} else
sb.append(sqlString);
if (Character.isLowSurrogate(c)) {
continue;
}
if (c == '\'') {
sb.append('\'');
}
sb.append(c);
}
sb.append('\'');
}

Expand Down

0 comments on commit 922a786

Please sign in to comment.