8056052: Source.getContent() does excess Object.clone()
authorattila
Tue, 26 Aug 2014 15:04:48 +0200
changeset 26249 5fbbd38ebc5b
parent 26248 9e9455565f77
child 26250 84bbd0e8b2b2
8056052: Source.getContent() does excess Object.clone() Reviewed-by: jlaskey, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java
nashorn/test/src/jdk/nashorn/internal/runtime/SourceTest.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java	Tue Aug 26 15:04:20 2014 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/Source.java	Tue Aug 26 15:04:48 2014 +0200
@@ -711,11 +711,16 @@
     }
 
     /**
-     * Get the content of this source as a char array
-     * @return content
+     * Get the content of this source as a char array. Note that the underlying array is returned instead of a
+     * clone; modifying the char array will cause modification to the source; this should not be done. While
+     * there is an apparent danger that we allow unfettered access to an underlying mutable array, the
+     * {@code Source} class is in a restricted {@code jdk.nashorn.internal.*} package and as such it is
+     * inaccessible by external actors in an environment with a security manager. Returning a clone would be
+     * detrimental to performance.
+     * @return content the content of this source as a char array
      */
     public char[] getContent() {
-        return data().clone();
+        return data();
     }
 
     /**
--- a/nashorn/test/src/jdk/nashorn/internal/runtime/SourceTest.java	Tue Aug 26 15:04:20 2014 +0200
+++ b/nashorn/test/src/jdk/nashorn/internal/runtime/SourceTest.java	Tue Aug 26 15:04:48 2014 +0200
@@ -117,9 +117,6 @@
         assertEquals(str1, str2);
         assertEquals(source1.hashCode(), source2.hashCode());
         assertTrue(source1.equals(source2));
-        // Test for immutability
-        Arrays.fill(source1.getContent(), (char)0);
-        Arrays.fill(source2.getContent(), (char)1);
         assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray()));
         assertTrue(Arrays.equals(source1.getContent(), chars1));
         assertTrue(Arrays.equals(source1.getContent(), source2.getContent()));