On Thu, 4 Jun 2026 17:55:36 GMT, Kevin Walls <[email protected]> wrote:
> Native Attach API code e.g.
> src/jdk.attach/linux/native/libattach/VirtualMachineImpl.c
> uses strdup before calling JNU_ThrowIOException
>
> char* msg = strdup(strerror(res));
> JNU_ThrowIOException(env, msg);
> if (msg != NULL) {
> free(msg);
> }
>
> This gets passed along and into e.g.
> java_lang_String::create_from_str(message, thread)
> which creates a new String object, into which the bytes are copied, so this
> strdup/malloc and free are unnecessary.
>
> We do this twice in this file, and in macos and aix versions. I've changed
> Linux and MacOs here. The same thing is done in AIX but I have left
> unchanged as I don't build/test it. Can update that also if I see any
> confirmation it's OK.
>
>
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK
> Interim AI Policy](https://openjdk.org/legal/ai).
Perhaps there was some paranoia that the string might be modified. From the man
page:
This string must not be modified by the application, but may be modified by
a subsequent call to strerror() or strerror_l().
So if there was another strerror() call during the JNU_ThrowIOException call,
that would be problematic, as would JNU_ThrowIOException modifying the string.
-------------
PR Review: https://git.openjdk.org/jdk/pull/31384#pullrequestreview-4438304283