From 4ca34dab32efe23ccec00a10b141f29c969e17cc Mon Sep 17 00:00:00 2001 From: mchung Date: Mon, 8 Feb 2010 23:02:28 -0800 Subject: [PATCH] 6924497: HotSpotDiagnosticsMXBean.getDiagnosticOptions throws NPE Summary: Check if the element in the flags array is non-null to filter unsupported flags Reviewed-by: dcubed --- src/share/classes/sun/management/Flag.java | 31 ++++++++++++++++------ src/share/native/sun/management/Flag.c | 5 +++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/share/classes/sun/management/Flag.java b/src/share/classes/sun/management/Flag.java index 44b4139e3..772b56886 100644 --- a/src/share/classes/sun/management/Flag.java +++ b/src/share/classes/sun/management/Flag.java @@ -68,27 +68,42 @@ class Flag { } 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]; - } else { + + List flags = getFlags(names, 1); + if (flags.isEmpty()) { return null; + } else { + // flags should have only one element + return flags.get(0); } } static List 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 getFlags(String[] names, int numFlags) { + Flag[] flags = new Flag[numFlags]; + int count = getFlags(names, flags, numFlags); + + List result = new ArrayList(); + 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(); diff --git a/src/share/native/sun/management/Flag.c b/src/share/native/sun/management/Flag.c index b642c5e05..e488d13e8 100644 --- a/src/share/native/sun/management/Flag.c +++ b/src/share/native/sun/management/Flag.c @@ -84,6 +84,7 @@ Java_sun_management_Flag_getFlags 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 @@ Java_sun_management_Flag_getFlags 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); -- GitLab