8193234: When using -Xcheck:jni an internally allocated buffer can leak
authordholmes
Fri, 08 Feb 2019 20:51:55 -0500
changeset 53712 e7d81a810546
parent 53711 8041cefba76b
child 53713 50c1b0a0f1e8
8193234: When using -Xcheck:jni an internally allocated buffer can leak Reviewed-by: shade, hseigel
src/hotspot/share/prims/jniCheck.cpp
--- a/src/hotspot/share/prims/jniCheck.cpp	Fri Feb 08 05:46:11 2019 -0900
+++ b/src/hotspot/share/prims/jniCheck.cpp	Fri Feb 08 20:51:55 2019 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -418,21 +418,20 @@
   size_t sz;
   void* orig_result = check_wrapped_array(thr, fn_name, obj, carray, &sz);
   switch (mode) {
+  // As we never make copies, mode 0 and JNI_COMMIT are the same.
   case 0:
-    memcpy(orig_result, carray, sz);
-    GuardedMemory::free_copy(carray);
-    break;
   case JNI_COMMIT:
     memcpy(orig_result, carray, sz);
     break;
   case JNI_ABORT:
-    GuardedMemory::free_copy(carray);
     break;
   default:
     tty->print_cr("%s: Unrecognized mode %i releasing array "
         PTR_FORMAT " elements " PTR_FORMAT, fn_name, mode, p2i(obj), p2i(carray));
     NativeReportJNIFatalError(thr, "Unrecognized array release mode");
   }
+  // We always need to release the copy we made with GuardedMemory
+  GuardedMemory::free_copy(carray);
   return orig_result;
 }