diff --git a/src/hotspot/share/classfile/classFileParser.cpp b/src/hotspot/share/classfile/classFileParser.cpp
index 996dde685b2..8508e45b810 100644
--- a/src/hotspot/share/classfile/classFileParser.cpp
+++ b/src/hotspot/share/classfile/classFileParser.cpp
@@ -5020,7 +5020,8 @@ bool ClassFileParser::verify_unqualified_name(const char* name,
   return true;
 }
 
-// Take pointer to a string. Skip over the longest part of the string that could
+// Take pointer to a UTF8 byte string (not NUL-terminated).
+// Skip over the longest part of the string that could
 // be taken as a fieldname. Allow '/' if slash_ok is true.
 // Return a pointer to just past the fieldname.
 // Return NULL if no fieldname at all was found, or in the case of slash_ok
@@ -5098,7 +5099,8 @@ static const char* skip_over_field_name(const char* const name,
   return (not_first_ch) ? p : NULL;
 }
 
-// Take pointer to a string. Skip over the longest part of the string that could
+// Take pointer to a UTF8 byte string (not NUL-terminated).
+// Skip over the longest part of the string that could
 // be taken as a field signature. Allow "void" if void_ok.
 // Return a pointer to just past the signature.
 // Return NULL if no legal signature is found.
@@ -5132,7 +5134,7 @@ const char* ClassFileParser::skip_over_field_signature(const char* signature,
       else {
         // Skip leading 'L' and ignore first appearance of ';'
         signature++;
-        char* c = strchr((char*) signature, ';');
+        const char* c = (const char*) memchr(signature, ';', length - 1);
         // Format check signature
         if (c != NULL) {
           int newlen = c - (char*) signature;
@@ -5199,7 +5201,7 @@ void ClassFileParser::verify_legal_class_name(const Symbol* name, TRAPS) const {
     Exceptions::fthrow(
       THREAD_AND_LOCATION,
       vmSymbols::java_lang_ClassFormatError(),
-      "Illegal class name \"%s\" in class file %s", bytes,
+      "Illegal class name \"%.*s\" in class file %s", length, bytes,
       _class_name->as_C_string()
     );
     return;
@@ -5232,7 +5234,7 @@ void ClassFileParser::verify_legal_field_name(const Symbol* name, TRAPS) const {
     Exceptions::fthrow(
       THREAD_AND_LOCATION,
       vmSymbols::java_lang_ClassFormatError(),
-      "Illegal field name \"%s\" in class %s", bytes,
+      "Illegal field name \"%.*s\" in class %s", length, bytes,
       _class_name->as_C_string()
     );
     return;
@@ -5269,7 +5271,7 @@ void ClassFileParser::verify_legal_method_name(const Symbol* name, TRAPS) const
     Exceptions::fthrow(
       THREAD_AND_LOCATION,
       vmSymbols::java_lang_ClassFormatError(),
-      "Illegal method name \"%s\" in class %s", bytes,
+      "Illegal method name \"%.*s\" in class %s", length, bytes,
       _class_name->as_C_string()
     );
     return;
diff --git a/test/hotspot/jtreg/runtime/classFileParserBug/TestBadClassName.java b/test/hotspot/jtreg/runtime/classFileParserBug/TestBadClassName.java
index 970d4a54eb6..f369f50557a 100644
--- a/test/hotspot/jtreg/runtime/classFileParserBug/TestBadClassName.java
+++ b/test/hotspot/jtreg/runtime/classFileParserBug/TestBadClassName.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8158297
+ * @bug 8158297 8218939
  * @summary Constant pool utf8 entry for class name cannot have empty qualified name '//'
  * @compile p1/BadInterface1.jcod
  * @compile p1/BadInterface2.jcod
@@ -37,20 +37,32 @@ public class TestBadClassName {
 
         System.out.println("Regression test for bug 8042660");
 
-        // Test class name with p1//BadInterface2
+        // Test class name with p1//BadInterface1
+        String expected = "Illegal class name \"p1//BadInterface1\" in class file UseBadInterface1";
         try {
             Class newClass = Class.forName("UseBadInterface1");
             throw new RuntimeException("Expected ClassFormatError exception not thrown");
         } catch (java.lang.ClassFormatError e) {
+            check(e, expected);
             System.out.println("Test UseBadInterface1 passed test case with illegal class name");
         }
 
         // Test class name with p1/BadInterface2/
+        expected = "Illegal class name \"p1/BadInterface2/\" in class file UseBadInterface2";
         try {
             Class newClass = Class.forName("UseBadInterface2");
             throw new RuntimeException("Expected ClassFormatError exception not thrown");
         } catch (java.lang.ClassFormatError e) {
-            System.out.println("Test UseBadInterface1 passed test case with illegal class name");
+            check(e, expected);
+            System.out.println("Test UseBadInterface2 passed test case with illegal class name");
+        }
+    }
+
+    static void check(ClassFormatError c, String expected) {
+        if (!c.getMessage().equals(expected)) {
+            throw new RuntimeException("Wrong ClassFormatError - expected: \"" +
+                                       expected + "\", got \"" +
+                                       c.getMessage() + "\"");
         }
     }
 }