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

JNI Specification: Clarify how to correctly size the buffer for GetStringUTFRegion

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 25
    • None
    • hotspot
    • b16

      The specification currently states:

      void GetStringUTFRegion(JNIEnv *env, jstring str, jsize start, jsize len, char *buf);

      Translates len number of Unicode characters beginning at offset start into modified UTF-8 encoding and place the result in the given buffer buf.

      The len argument specifies the number of unicode characters. The resulting number modified UTF-8 encoding characters may be greater than the given len argument. GetStringUTFLengthAsLong() may be used to determine the maximum size of the required character buffer.

      Since this specification does not require the resulting string copy be NULL terminated, it is advisable to clear the given character buffer (e.g. "memset()") before using this function, in order to safely perform strlen().

      ---

      This text leads programmers to believe that they only need to allocate a buffer of size GetStringUTFLengthAsLong(). However, if an implementation does NULL-terminate "the resulting string copy" then such a buffer will be too small.

      We need to clarify the text that to accommodate implementations (like Hotspot) that do NULL-terminate, the buffer must be one larger than currently suggested.

      The specification also states:

      "len: the number of Unicode characters to copy. Must be greater than zero, and "start + len" must be less than string length ("GetStringLength()")."

      but the correct formulation would be "must be less than or equal to the string length".

            dholmes David Holmes
            dholmes David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: