1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-23 14:02:18 +00:00

Fix out-of-bounds when pasting too-long text

Used a `<=` instead of a `<`! How did I mess this up!?

Fixes #2209
This commit is contained in:
Jonathan Coates 2025-06-02 08:58:43 +01:00
parent 876fd8ddb8
commit b5c0c6e104
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
3 changed files with 39 additions and 2 deletions

View File

@ -26,7 +26,8 @@ public class KeyConverter {
var name = GLFW.glfwGetKeyName(key, scanCode);
if (name == null || name.length() != 1) return key;
// If we've got a single character key name, try to translate it to a
// If we've got a single character as the key name, treat that as the ASCII value of the key,
// and map that back to a key code.
var character = name.charAt(0);
// 0-9 and A-Z map directly to their GLFW key (they're the same ASCII code).

View File

@ -120,7 +120,7 @@ public final class StringUtil {
var idx = 0;
var iterator = clipboard.codePoints().iterator();
while (iterator.hasNext() && idx <= output.length) {
while (iterator.hasNext() && idx < output.length) {
var chr = unicodeToTerminal(iterator.next());
if (chr < 0) continue; // Strip out unconvertible characters
if (!isTypableChar(chr)) break; // Stop at untypable ones.

View File

@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2025 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.core.util;
import dan200.computercraft.api.lua.LuaValues;
import dan200.computercraft.test.core.ReplaceUnderscoresDisplayNameGenerator;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
@DisplayNameGeneration(ReplaceUnderscoresDisplayNameGenerator.class)
class StringUtilTest {
@ParameterizedTest
@ValueSource(strings = { "hello\nworld", "hello\n\rworld", "hello\rworld" })
public void getClipboardString_returns_a_single_line(String input) {
var result = StringUtil.getClipboardString(input);
assertEquals(LuaValues.encode("hello"), result);
}
@Test
public void getClipboardString_limits_length() {
var input = "abcdefghijklmnop".repeat(50);
var result = StringUtil.getClipboardString(input);
assertEquals(StringUtil.MAX_PASTE_LENGTH, result.limit());
assertEquals(
LuaValues.encode(input.substring(0, StringUtil.MAX_PASTE_LENGTH)),
result
);
}
}