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

JNI GetStringUTFChars should return NULL on allocation failure not abort the VM

XMLWordPrintable

    • b48

        Discovered by code inspection as part of JDK-8022584.

        According to the JNI spec:

        GetStringUTFChars

        const char * GetStringUTFChars(JNIEnv *env, jstring string,
        jboolean *isCopy);

        Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding. This array is valid until it is released by ReleaseStringUTFChars().

        If isCopy is not NULL, then *isCopy is set to JNI_TRUE if a copy is made; or it is set to JNI_FALSE if no copy is made.

        LINKAGE:
        Index 169 in the JNIEnv interface function table.
        PARAMETERS:

        env: the JNI interface pointer.

        string: a Java string object.

        isCopy: a pointer to a boolean.
        RETURNS:

        Returns a pointer to a modified UTF-8 string, or NULL if the operation fails.

        ---

        The JNI book goes further and states that an OutOfMemoryException will be posted if the system runs out of memory.

        ---

        The actual code is somewhat different:

          oop java_string = JNIHandles::resolve_non_null(string);
          size_t length = java_lang_String::utf8_length(java_string);
          char* result = AllocateHeap(length + 1, mtInternal);
          java_lang_String::as_utf8_string(java_string, result, (int) length + 1);
          if (isCopy != NULL) *isCopy = JNI_TRUE;
          return result;

        To return NULL, AllocateHeap must return NULL, but the form of AllocateHeap used will actually abort the VM on an out-of-memory condition. We should switch to using the "return NULL" variant of AllocateHeap thus allowing this function to return NULL. I also think we should post an OOME - though that may be difficult if we have already hit an OOM condition.

              dsimms David Simms
              dholmes David Holmes
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: