< prev index next >

src/os/solaris/dtrace/libjvm_db.c

Print this page
rev 8113 : 8076475: Misuses of strncpy/strncat
Summary: Various small fixes around strncpy and strncat
Reviewed-by: dsamersoff

@@ -580,17 +580,18 @@
   klassString = (char*)calloc(klassSymbolLength + 1, 1);
   err = ps_pread(J->P, klassSymbol + OFFSET_Symbol_body, klassString, klassSymbolLength);
   CHECK_FAIL(err);
 
   result[0] = '\0';
-  strncat(result, klassString, size);
-  size -= strlen(klassString);
-  strncat(result, ".", size);
-  size -= 1;
-  strncat(result, nameString, size);
-  size -= strlen(nameString);
-  strncat(result, signatureString, size);
+  if (snprintf(result, size,
+    "%s.%s%s",
+    klassString,
+    nameString,
+    signatureString) >= size) {
+    // truncation
+    goto fail;
+  }
 
   if (nameString != NULL) free(nameString);
   if (klassString != NULL) free(klassString);
   if (signatureString != NULL) free(signatureString);
 

@@ -1093,13 +1094,13 @@
   } else {
       err = name_for_methodPtr(J, method, result+1, size-1);
       CHECK_FAIL(err);
   }
   if (deoptimized) {
-    strncat(result + 1, " [deoptimized frame]; ", size-1);
+    strncat(result, " [deoptimized frame]; ", size - strlen(result) - 1);
   } else {
-    strncat(result + 1, " [compiled] ", size-1);
+    strncat(result, " [compiled] ", size - strlen(result) - 1);
   }
   if (debug)
       fprintf(stderr, "name_for_nmethod: END: method name: %s, vf_cnt: %d\n\n",
                       result, N->vf_cnt);
   return PS_OK;
< prev index next >