JDK-6788138 : leak in Java_sun_awt_X11_XlibWrapper_getStringBytes?
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 7
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2008-12-22
  • Updated: 2014-07-29
  • Resolved: 2014-04-07
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u20Fixed 9 b12Fixed
Description
FULL PRODUCT VERSION :
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b41)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b08, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux lithium 2.6.24-22-generic #1 SMP Mon Nov 24 19:35:06 UTC 2008 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
i just happened to be reading the source and noticed this:

JNIEXPORT jbyteArray JNICALL Java_sun_awt_X11_XlibWrapper_getStringBytes
(JNIEnv *env, jclass clazz, jlong str_ptr)
{
    unsigned char * str = (unsigned char*) jlong_to_ptr(str_ptr);
    long length = strlen((char*)str);
    jbyteArray res = (*env)->NewByteArray(env, length);
!    void * storage = malloc(length+1);
!    memcpy(storage, str, length+1);
    (*env)->SetByteArrayRegion(env, res, 0, length,
                   (const signed char*) storage);
    return res;
}

i don't understand the reason for the two lines i've marked with a leading !, and think the SetByteArrayRegion call should use 'str' instead of 'storage'. if not, then there's a missing call to free(3). so something's wrong here either way.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
i've not seen this bug in action.


REPRODUCIBILITY :
This bug can be reproduced always.

Comments
EVALUATION Should free(strorage) just before returning. Also, better check for malloc() ret code: if (( storage = (void *) malloc(length+1) ) == NULL) { fprintf(stderr, "internal error"); return; }
16-08-2010