From 8604f16b30892e7dc2482d221f611757aefb0d87 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 7 Jun 2019 17:45:03 +0200 Subject: [PATCH] Truncate device name at UTF-8 code point boundary Just in case. --- .../src/main/java/com/genymobile/scrcpy/DesktopConnection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/DesktopConnection.java b/server/src/main/java/com/genymobile/scrcpy/DesktopConnection.java index a26c74cd..a725d83d 100644 --- a/server/src/main/java/com/genymobile/scrcpy/DesktopConnection.java +++ b/server/src/main/java/com/genymobile/scrcpy/DesktopConnection.java @@ -89,7 +89,7 @@ public final class DesktopConnection implements Closeable { byte[] buffer = new byte[DEVICE_NAME_FIELD_LENGTH + 4]; byte[] deviceNameBytes = deviceName.getBytes(StandardCharsets.UTF_8); - int len = Math.min(DEVICE_NAME_FIELD_LENGTH - 1, deviceNameBytes.length); + int len = StringUtils.getUtf8TruncationIndex(deviceNameBytes, DEVICE_NAME_FIELD_LENGTH - 1); System.arraycopy(deviceNameBytes, 0, buffer, 0, len); // byte[] are always 0-initialized in java, no need to set '\0' explicitly