src/share/vm/prims/jni.cpp

Print this page
rev 6283 : redundant memcpy

@@ -3148,15 +3148,13 @@
   if (start < 0 || len < 0 || start + len > s_len) {
     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
   } else {
     //%note jni_7
     if (len > 0) {
-      ResourceMark rm(THREAD);
-      char *utf_region = java_lang_String::as_utf8_string(s, start, len);
-      int utf_len = (int)strlen(utf_region);
-      memcpy(buf, utf_region, utf_len);
-      buf[utf_len] = 0;
+      // Assume the buffer is large enough as the JNI spec. does not require user error checking
+      java_lang_String::as_utf8_string(s, start, len, buf, INT_MAX);
+      // as_utf8_string null-terminates the result string
     } else {
       // JDK null-terminates the buffer even in len is zero
       if (buf != NULL) {
         buf[0] = 0;
       }

@@ -3878,10 +3876,11 @@
 void TestVirtualSpaceNode_test();
 void TestNewSize_test();
 void TestOldSize_test();
 void TestKlass_test();
 void TestBitMap_test();
+void TestAsUtf8();
 #if INCLUDE_ALL_GCS
 void TestOldFreeSpaceCalculation_test();
 void TestG1BiasedArray_test();
 void TestBufferingOopClosure_test();
 void TestCodeCacheRemSet_test();

@@ -3905,10 +3904,11 @@
     run_unit_test(test_loggc_filename());
     run_unit_test(TestNewSize_test());
     run_unit_test(TestOldSize_test());
     run_unit_test(TestKlass_test());
     run_unit_test(TestBitMap_test());
+    run_unit_test(TestAsUtf8());
 #if INCLUDE_VM_STRUCTS
     run_unit_test(VMStructs::test());
 #endif
 #if INCLUDE_ALL_GCS
     run_unit_test(TestOldFreeSpaceCalculation_test());