< prev index next >

src/hotspot/share/utilities/stringUtils.cpp

Print this page

        

@@ -21,10 +21,11 @@
  * questions.
  *
  */
 
 #include "precompiled.hpp"
+#include "utilities/debug.hpp"
 #include "utilities/stringUtils.hpp"
 
 int StringUtils::replace_no_expand(char* string, const char* from, const char* to) {
   int replace_count = 0;
   size_t from_len = strlen(from);

@@ -41,13 +42,20 @@
 
   return replace_count;
 }
 
 double StringUtils::similarity(const char* str1, size_t len1, const char* str2, size_t len2) {
-  size_t total = len1 + len2;
+  assert(str1 != NULL && str2 != NULL, "sanity");
 
+  size_t total = len1 + len2;
   size_t hit = 0;
+
+  // filter out zero-length strings else we will underflow on len-1 below
+  if (len1 == 0 || len2 == 0) {
+    return 0.0;
+  }
+
   for (size_t i = 0; i < len1 - 1; i++) {
     for (size_t j = 0; j < len2 - 1; j++) {
       if ((str1[i] == str2[j]) && (str1[i+1] == str2[j+1])) {
         ++hit;
         break;
< prev index next >