Browse Source

Add defensive checks to Styliser.

Issue DMDirc/Plugins#47
pull/63/head
Chris Smith 9 years ago
parent
commit
168c1c5e71
1 changed files with 8 additions and 0 deletions
  1. 8
    0
      src/com/dmdirc/ui/messages/Styliser.java

+ 8
- 0
src/com/dmdirc/ui/messages/Styliser.java View File

@@ -41,6 +41,8 @@ import javax.swing.text.SimpleAttributeSet;
41 41
 import javax.swing.text.StyleConstants;
42 42
 import javax.swing.text.StyledDocument;
43 43
 
44
+import static com.google.common.base.Preconditions.checkArgument;
45
+
44 46
 /**
45 47
  * The styliser applies IRC styles to text. Styles are indicated by various control codes which are
46 48
  * a de-facto IRC standard.
@@ -255,7 +257,13 @@ public class Styliser implements ConfigChangeListener {
255 257
      * @since 0.6.3
256 258
      */
257 259
     public static String getStyledText(final String styled, final int from, final int to) {
260
+        checkArgument(from < to, "'from' must be less than 'to'");
261
+        checkArgument(from >= 0, "'from' must be non-negative");
262
+
258 263
         final String unstyled = stipControlCodes(styled);
264
+
265
+        checkArgument(to < unstyled.length(), "'to' must be less than the unstyled length");
266
+
259 267
         final String startBit = unstyled.substring(0, from);
260 268
         final String middleBit = unstyled.substring(from, to);
261 269
         final String sanitised = stipInternalControlCodes(styled);

Loading…
Cancel
Save