Bug 40769 - A JNI memory leak in the Java_java_lang_VMSystem_getenv native method
Summary: A JNI memory leak in the Java_java_lang_VMSystem_getenv native method
Status: UNCONFIRMED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.98
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-15 23:48 UTC by Byeogncheol Lee
Modified: 2012-02-22 12:43 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Byeogncheol Lee 2009-07-15 23:48:16 UTC
The Java_java_lang_VMSystem_getenv native method may leak a memory when the "envname" is NULL and the JVM returns a new memory for GetStringUTFChars. The memory cell pointed by the "cname" is a JVM resource that must be deallocated by calling ReleaseStringUTFChars.

-------------------------
JNIEXPORT jstring JNICALL
Java_java_lang_VMSystem_getenv (JNIEnv * env,
				jclass klass __attribute__ ((__unused__)),
				jstring jname)
{
  const char *cname;
  const char *envname;

  cname = JCL_jstring_to_cstring (env, jname);
  if (cname == NULL)
    return NULL;

  envname = getenv (cname);
  if (envname == NULL)
    return NULL;

  JCL_free_cstring (env, jname, cname);
  return (*env)->NewStringUTF (env, envname);
}
-------------------------

I propose the following patch of freeing the JVM resource as early as possible:

Index: java_lang_VMSystem.c
===================================================================
RCS file: /sources/classpath/classpath/native/jni/java-lang/java_lang_VMSystem.c,v
retrieving revision 1.15
diff -r1.15 java_lang_VMSystem.c
151a152
>   JCL_free_cstring (env, jname, cname);
155d155
<   JCL_free_cstring (env, jname, cname);
Comment 1 xiaoyuanbo 2012-02-22 12:43:45 UTC
decimal it