<AWT Dev> [PATCH resend2] Fix memory leak in wcstombsdmp

Alex Henrie alexhenrie24 at gmail.com
Tue Dec 15 20:43:37 UTC 2015


# HG changeset patch
# User Alex Henrie <alexhenrie24 at gmail.com>
# Date 1448316746 25200
#      Mon Nov 23 15:12:26 2015 -0700
# Node ID c12ad61a6e05f4c6af14812a9049f46ddac542d2
# Parent  6dca8affef8a4566dfaa36faeb7a14fc4086843a
Fix memory leak in wcstombsdmp

If a character cannot be represented in the current locale, wcstombs can
set errno to EILSEQ and return -1. In this case, the char buffer is not
returned because it contains garbage, so it needs to be freed.

diff --git a/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c b/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
--- a/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
+++ b/src/java.desktop/unix/native/libawt_xawt/awt/awt_InputMethod.c
@@ -216,18 +216,20 @@ wcstombsdmp(wchar_t *wcs, int len)
 
     mbs = (char *) malloc(n * sizeof(char));
     if (mbs == NULL) {
         THROW_OUT_OF_MEMORY_ERROR();
         return NULL;
     }
 
     /* TODO: check return values... Handle invalid characters properly...  */
-    if (wcstombs(mbs, wcs, n) == (size_t)-1)
+    if (wcstombs(mbs, wcs, n) == (size_t)-1) {
+        free(mbs);
         return NULL;
+    }
 
     return mbs;
 }
 
 /*
  * Returns True if the global reference is still in the list,
  * otherwise False.
  */


More information about the awt-dev mailing list