changeset 2282:83c34a6b1458

6924497: HotSpotDiagnosticsMXBean.getDiagnosticOptions throws NPE Summary: Check if the element in the flags array is non-null to filter unsupported flags Reviewed-by: dcubed
author mchung
date Mon, 08 Feb 2010 23:02:28 -0800
parents 445b9928fb70
children ec438f2b6886
files src/share/classes/sun/management/Flag.java src/share/native/sun/management/Flag.c
diffstat 2 files changed, 27 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/management/Flag.java	Sat Feb 06 09:26:57 2010 -0800
+++ b/src/share/classes/sun/management/Flag.java	Mon Feb 08 23:02:28 2010 -0800
@@ -68,27 +68,42 @@
     }
 
     static Flag getFlag(String name) {
-        Flag[] fs = new Flag[1];
         String[] names = new String[1];
         names[0] = name;
-        int count = getFlags(names, fs, 1);
-        if (count == 1) {
-            return fs[0];
+
+        List<Flag> flags = getFlags(names, 1);
+        if (flags.isEmpty()) {
+            return null;
         } else {
-            return null;
+            // flags should have only one element
+            return flags.get(0);
         }
     }
 
     static List<Flag> getAllFlags() {
         int numFlags = getInternalFlagCount();
-        Flag[] fs = new Flag[numFlags];
 
         // Get all internal flags with names = null
-        int count = getFlags(null, fs, numFlags);
-        return Arrays.asList(fs);
+        return getFlags(null, numFlags);
+    }
+
+    private static List<Flag> getFlags(String[] names, int numFlags) {
+        Flag[] flags = new Flag[numFlags];
+        int count = getFlags(names, flags, numFlags);
+
+        List<Flag> result = new ArrayList<Flag>();
+        for (Flag f : flags) {
+            if (f != null) {
+                result.add(f);
+            }
+        }
+        return result;
     }
 
     private static native String[] getAllFlagNames();
+    // getFlags sets each element in the given flags array
+    // with a Flag object only if the name is valid and the
+    // type is supported. The flags array may contain null elements.
     private static native int getFlags(String[] names, Flag[] flags, int count);
     private static native int getInternalFlagCount();
 
--- a/src/share/native/sun/management/Flag.c	Sat Feb 06 09:26:57 2010 -0800
+++ b/src/share/native/sun/management/Flag.c	Mon Feb 08 23:02:28 2010 -0800
@@ -84,6 +84,7 @@
 
     jint num_flags, i, index;
     jmmVMGlobal* globals;
+    size_t gsize;
     const char* class_name = "sun/management/Flag";
     const char* signature = "(Ljava/lang/String;Ljava/lang/Object;ZZLcom/sun/management/VMOption$Origin;)V";
     jobject origin;
@@ -100,12 +101,14 @@
         return 0;
     }
 
-    globals = (jmmVMGlobal*) malloc(count * sizeof(jmmVMGlobal));
+    gsize = count * sizeof(jmmVMGlobal);
+    globals = (jmmVMGlobal*) malloc(gsize);
     if (globals == NULL) {
         JNU_ThrowOutOfMemoryError(env, 0);
         return 0;
     }
 
+    memset(globals, 0, gsize);
     num_flags = jmm_interface->GetVMGlobals(env, names, globals, count);
     if (num_flags == 0) {
         free(globals);