Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8275517

Off-by-one error in allocation

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 18
    • 18
    • hotspot
    • jfr
    • b20
    • Not verified

      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

            mgronlun Markus Grönlund
            mgronlun Markus Grönlund
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: