Skip to content

Commit bedc9a5

Browse files
committed
[misc] better unexcepected character when malformed String (wrong surrogate)
(cherry picked from commit db8da1f)
1 parent 0abceec commit bedc9a5

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/org/mariadb/jdbc/internal/io/output/AbstractPacketOutputStream.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,12 @@ public void write(String str, boolean escape, boolean noBackslashEscapes) throws
545545
charsOffset++;
546546
} else {
547547
//must have low surrogate
548-
buf[pos++] = (byte) 0x63;
548+
buf[pos++] = (byte) 0x3f;
549549
}
550550
}
551551
} else {
552552
//low surrogate without high surrogate before
553-
buf[pos++] = (byte) 0x63;
553+
buf[pos++] = (byte) 0x3f;
554554
}
555555
} else {
556556
buf[pos++] = (byte) (0xe0 | ((currChar >> 12)));

src/test/java/org/mariadb/jdbc/CollationTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,26 @@ public void languageCasing() throws SQLException {
265265
}
266266
}
267267

268+
@Test
269+
public void wrongSurrogate() throws Throwable {
270+
byte[] bb = "a\ud800b".getBytes("utf8");
271+
Connection conn = null;
272+
try {
273+
conn = setConnection();
274+
Statement stmt = conn.createStatement();
275+
stmt.execute("CREATE TEMPORARY TABLE wrong_utf8_string(tt text) CHARSET utf8mb4");
276+
String wrongString = "a\ud800b";
277+
278+
PreparedStatement preparedStatement = conn.prepareStatement("INSERT INTO wrong_utf8_string values (?)");
279+
preparedStatement.setString(1, wrongString);
280+
preparedStatement.execute();
281+
preparedStatement.close();
282+
ResultSet rs = stmt.executeQuery("SELECT * from wrong_utf8_string");
283+
assertTrue(rs.next());
284+
assertEquals("a?b", rs.getString(1));
285+
} finally {
286+
if (conn != null) conn.close();
287+
}
288+
}
289+
268290
}

0 commit comments

Comments
 (0)