8212694: Using Raw String Literals with align() and Integer.MIN_VALUE causes out of memory error
authorjlaskey
Mon, 29 Oct 2018 12:31:49 -0300
changeset 52312 5b9c8d77a9fe
parent 52311 274ba8fbd96d
child 52313 f300b4ca2637
8212694: Using Raw String Literals with align() and Integer.MIN_VALUE causes out of memory error Reviewed-by: smarks, sherman
src/java.base/share/classes/java/lang/String.java
test/jdk/java/lang/String/AlignIndent.java
--- a/src/java.base/share/classes/java/lang/String.java	Mon Oct 29 10:21:34 2018 -0400
+++ b/src/java.base/share/classes/java/lang/String.java	Mon Oct 29 12:31:49 2018 -0300
@@ -2967,7 +2967,9 @@
                              .mapToInt(String::indexOfNonWhitespace)
                              .min()
                              .orElse(0);
-        return indent(n - outdent, true);
+        // overflow-conscious code
+        int indent = n - outdent;
+        return indent(indent > n ? Integer.MIN_VALUE : indent, true);
     }
 
     /**
--- a/test/jdk/java/lang/String/AlignIndent.java	Mon Oct 29 10:21:34 2018 -0400
+++ b/test/jdk/java/lang/String/AlignIndent.java	Mon Oct 29 12:31:49 2018 -0300
@@ -53,6 +53,7 @@
         test1();
         test2();
         test3();
+        test4();
     }
 
     /*
@@ -154,6 +155,18 @@
         }
     }
 
+    /*
+     * JDK-8212694: Using Raw String Literals with align() and Integer.MIN_VALUE causes out of memory error
+     */
+    static void test4() {
+        try {
+            String str = "\n    A\n".align(Integer.MIN_VALUE);
+        } catch (OutOfMemoryError ex) {
+            System.err.println("align(Integer.MIN_VALUE) not clipping indentation");
+            throw new RuntimeException();
+        }
+    }
+
     public static int indexOfNonWhitespace(String s) {
         int left = 0;
         while (left < s.length()) {