From f4c9324adbd503dca610988db65900d417981999 Mon Sep 17 00:00:00 2001 From: DRC Date: Wed, 1 May 2024 13:00:58 -0400 Subject: [PATCH] Viewer: Truncate overly long conn. history strings Java Preferences values cannot be more than Preferences.MAX_VALUE_LENGTH (usually 8192) characters in length. Attempting to store a longer string into a Preferences node results in an IllegalArgumentException. Fixes #410 --- java/com/turbovnc/vncviewer/ServerDialog.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/java/com/turbovnc/vncviewer/ServerDialog.java b/java/com/turbovnc/vncviewer/ServerDialog.java index 8a5c873c1..f0dad580c 100644 --- a/java/com/turbovnc/vncviewer/ServerDialog.java +++ b/java/com/turbovnc/vncviewer/ServerDialog.java @@ -1,4 +1,5 @@ -/* Copyright (C) 2012-2015, 2018, 2020 D. R. Commander. All Rights Reserved. +/* Copyright (C) 2012-2015, 2018, 2020, 2024 D. R. Commander. + * All Rights Reserved. * Copyright (C) 2011-2013 Brian P. Hinz * Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. * @@ -26,6 +27,7 @@ import javax.swing.border.*; import javax.swing.WindowConstants.*; import java.util.*; +import java.util.prefs.Preferences; import com.turbovnc.rdr.*; import com.turbovnc.rfb.*; @@ -246,7 +248,16 @@ private boolean commit() { sb.append(str); } } - UserPreferences.set("ServerDialog", "history", sb.toString()); + String history = sb.toString(); + if (history.length() > Preferences.MAX_VALUE_LENGTH) { + int lastComma = + history.lastIndexOf(',', Preferences.MAX_VALUE_LENGTH); + if (lastComma <= 0) + history = ""; + else + history = history.substring(0, lastComma); + } + UserPreferences.set("ServerDialog", "history", history); UserPreferences.save("ServerDialog"); } catch (Exception e) {