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".
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".
- csr for
-
JDK-8352192 JNI Specification: Clarify how to correctly size the buffer for GetStringUTFRegion
-
- Closed
-