JDK-8275517 : Off-by-one error in allocation
  • Type: Bug
  • Component: hotspot
  • Sub-Component: jfr
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-10-19
  • Updated: 2021-10-26
  • Resolved: 2021-10-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 18
18 b20Fixed
Related Reports
Duplicate :  
Relates :  
Description
Copy-paste error in JDK-8275445 trying to resolve original issue JDK-8266936

Moving from the JFR abstraction:

static char* allocate_string(bool c_heap, int length, JavaThread* jt) {
  return c_heap ? NEW_C_HEAP_ARRAY(char, length, mtTracing) :
                  NEW_RESOURCE_ARRAY_IN_THREAD(jt, char, length);
}

const char* JfrJavaSupport::c_str(oop string, JavaThread* t, bool c_heap /* false */) {
  DEBUG_ONLY(check_java_thread_in_vm(t));
  char* str = NULL;
  const typeArrayOop value = java_lang_String::value(string);
  if (value != NULL) {
    const int length = java_lang_String::utf8_length(string, value);
    str = allocate_string(c_heap, length + 1, t);
    if (str == NULL) {
      JfrJavaSupport::throw_out_of_memory_error("Unable to allocate native memory", t);
      return NULL;
    }
    java_lang_String::as_utf8_string(string, value, str, length + 1);
  }
  return str;
}

To the non-JFR code:

static const char* allocate(oop string) {
  char* str = nullptr;
  const typeArrayOop value = java_lang_String::value(string);
  if (value != nullptr) {
    const int length = java_lang_String::utf8_length(string, value);
    str = NEW_C_HEAP_ARRAY(char, length, mtServiceability);
    java_lang_String::as_utf8_string(string, value, str, length + 1);
  }
  return str;
}

allocate_string() takes the length+1 to the  NEW_C_HEAP_ARRAY, but the new code lost the +1

Comments
Changeset: 99bf7dd8 Author: Markus Grönlund <mgronlun@openjdk.org> Date: 2021-10-19 16:20:45 +0000 URL: https://git.openjdk.java.net/jdk/commit/99bf7dd8ddac1b5870534af50c97bec554004248
19-10-2021