Tue, 26 Aug 2014 15:04:48 +0200
8056052: Source.getContent() does excess Object.clone()
Reviewed-by: jlaskey, sundar
src/jdk/nashorn/internal/runtime/Source.java | file | annotate | diff | comparison | revisions | |
test/src/jdk/nashorn/internal/runtime/SourceTest.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/jdk/nashorn/internal/runtime/Source.java Tue Aug 26 15:04:20 2014 +0200 1.2 +++ b/src/jdk/nashorn/internal/runtime/Source.java Tue Aug 26 15:04:48 2014 +0200 1.3 @@ -711,11 +711,16 @@ 1.4 } 1.5 1.6 /** 1.7 - * Get the content of this source as a char array 1.8 - * @return content 1.9 + * Get the content of this source as a char array. Note that the underlying array is returned instead of a 1.10 + * clone; modifying the char array will cause modification to the source; this should not be done. While 1.11 + * there is an apparent danger that we allow unfettered access to an underlying mutable array, the 1.12 + * {@code Source} class is in a restricted {@code jdk.nashorn.internal.*} package and as such it is 1.13 + * inaccessible by external actors in an environment with a security manager. Returning a clone would be 1.14 + * detrimental to performance. 1.15 + * @return content the content of this source as a char array 1.16 */ 1.17 public char[] getContent() { 1.18 - return data().clone(); 1.19 + return data(); 1.20 } 1.21 1.22 /**
2.1 --- a/test/src/jdk/nashorn/internal/runtime/SourceTest.java Tue Aug 26 15:04:20 2014 +0200 2.2 +++ b/test/src/jdk/nashorn/internal/runtime/SourceTest.java Tue Aug 26 15:04:48 2014 +0200 2.3 @@ -117,9 +117,6 @@ 2.4 assertEquals(str1, str2); 2.5 assertEquals(source1.hashCode(), source2.hashCode()); 2.6 assertTrue(source1.equals(source2)); 2.7 - // Test for immutability 2.8 - Arrays.fill(source1.getContent(), (char)0); 2.9 - Arrays.fill(source2.getContent(), (char)1); 2.10 assertTrue(Arrays.equals(source1.getContent(), str1.toCharArray())); 2.11 assertTrue(Arrays.equals(source1.getContent(), chars1)); 2.12 assertTrue(Arrays.equals(source1.getContent(), source2.getContent()));