8157548: JVM crashes sometimes while starting
Behavior of strncmp may be unexpected if char buffers[s] is[are] not null terminated and buffer size is smaller than the length n. Added check to avoid this scenario.
Reviewed-by: dholmes, iklam
diff --git a/hotspot/src/share/vm/classfile/systemDictionary.cpp b/hotspot/src/share/vm/classfile/systemDictionary.cpp
index 31be14b..1dfe3b6 100644
--- a/hotspot/src/share/vm/classfile/systemDictionary.cpp
+++ b/hotspot/src/share/vm/classfile/systemDictionary.cpp
@@ -1084,15 +1084,18 @@
THREAD);
const char* pkg = "java/";
+ size_t pkglen = strlen(pkg);
if (!HAS_PENDING_EXCEPTION &&
!class_loader.is_null() &&
parsed_name != NULL &&
- !strncmp((const char*)parsed_name->bytes(), pkg, strlen(pkg))) {
+ parsed_name->utf8_length() >= (int)pkglen &&
+ !strncmp((const char*)parsed_name->bytes(), pkg, pkglen)) {
// It is illegal to define classes in the "java." package from
// JVM_DefineClass or jni_DefineClass unless you're the bootclassloader
ResourceMark rm(THREAD);
char* name = parsed_name->as_C_string();
char* index = strrchr(name, '/');
+ assert(index != NULL, "must be");
*index = '\0'; // chop to just the package name
while ((index = strchr(name, '/')) != NULL) {
*index = '.'; // replace '/' with '.' in package name