7004728: [parfait] potential memory leaks in XRender code
authorprr
Mon, 28 Feb 2011 10:25:17 -0800
changeset 8506 04d00feba488
parent 8505 1f704178bb53
child 8507 78ea3ec4877b
7004728: [parfait] potential memory leaks in XRender code Reviewed-by: bae, ceisserer
jdk/src/solaris/native/sun/java2d/x11/XRBackendNative.c
--- a/jdk/src/solaris/native/sun/java2d/x11/XRBackendNative.c	Mon Feb 21 19:43:56 2011 -0800
+++ b/jdk/src/solaris/native/sun/java2d/x11/XRBackendNative.c	Mon Feb 28 10:25:17 2011 -0800
@@ -304,23 +304,27 @@
     color.blue = blue;
 
     if (rectCnt <= 256) {
-      xRects = &sRects[0];
+        xRects = &sRects[0];
     } else {
-      xRects = (XRectangle *) malloc(sizeof(XRectangle) * rectCnt);
-      if (xRects == NULL) {
-        return;
-      }
+        xRects = (XRectangle *) malloc(sizeof(XRectangle) * rectCnt);
+        if (xRects == NULL) {
+            return;
+        }
     }
 
-    if ((rects = (jint *) (*env)->GetPrimitiveArrayCritical(env, rectArray, NULL)) == NULL) {
-      return;
+    if ((rects = (jint *)
+         (*env)->GetPrimitiveArrayCritical(env, rectArray, NULL)) == NULL) {
+        if (xRects != &sRects[0]) {
+            free(xRects);
+        }
+        return;
     }
 
     for (i=0; i < rectCnt; i++) {
-      xRects[i].x = rects[i*4 + 0];
-      xRects[i].y = rects[i*4 + 1];
-      xRects[i].width = rects[i*4 + 2];
-      xRects[i].height = rects[i*4 + 3];
+        xRects[i].x = rects[i*4 + 0];
+        xRects[i].y = rects[i*4 + 1];
+        xRects[i].width = rects[i*4 + 2];
+        xRects[i].height = rects[i*4 + 3];
     }
 
     XRenderFillRectangles(awt_display, op,
@@ -328,7 +332,7 @@
 
     (*env)->ReleasePrimitiveArrayCritical(env, rectArray, rects, JNI_ABORT);
     if (xRects != &sRects[0]) {
-      free(xRects);
+        free(xRects);
     }
 }
 
@@ -610,17 +614,30 @@
     Glyph *gid = (Glyph *) malloc(sizeof(Glyph) * glyphCnt);
 
     if (xginfo == NULL || gid == NULL) {
-      return;
+        if (xginfo != NULL) {
+            free(xginfo);
+        }
+        if (gid != NULL) {
+            free(gid);
+        }
+        return;
     }
 
-    if ((glyphInfoPtrs = (jlong *) (*env)->GetPrimitiveArrayCritical(env, glyphInfoPtrsArray, NULL)) == NULL) {
+    if ((glyphInfoPtrs = (jlong *)(*env)->
+        GetPrimitiveArrayCritical(env, glyphInfoPtrsArray, NULL)) == NULL)
+    {
+        free(xginfo);
+        free(gid);
         return;
     }
 
     if ((pixelData = (unsigned char *)
-        (*env)->GetPrimitiveArrayCritical(env, pixelDataArray, NULL)) == NULL) {
+        (*env)->GetPrimitiveArrayCritical(env, pixelDataArray, NULL)) == NULL)
+    {
         (*env)->ReleasePrimitiveArrayCritical(env,
-                                  glyphInfoPtrsArray, glyphInfoPtrs, JNI_ABORT);
+                                glyphInfoPtrsArray, glyphInfoPtrs, JNI_ABORT);
+        free(xginfo);
+        free(gid);
         return;
     }
 
@@ -637,7 +654,7 @@
     }
 
     XRenderAddGlyphs(awt_display, glyphSet, &gid[0], &xginfo[0], glyphCnt,
-                     pixelData, pixelDataLength);
+                     (const char*)pixelData, pixelDataLength);
 
     (*env)->ReleasePrimitiveArrayCritical(env, glyphInfoPtrsArray, glyphInfoPtrs, JNI_ABORT);
     (*env)->ReleasePrimitiveArrayCritical(env, pixelDataArray, pixelData, JNI_ABORT);
@@ -684,22 +701,44 @@
       xelts = &selts[0];
     }else {
       xelts = (XGlyphElt32 *) malloc(sizeof(XGlyphElt32) * eltCnt);
+      if (xelts == NULL) {
+          return;
+      }
     }
 
     if (glyphCnt <= 256) {
       xids = &sids[0];
-    }else {
+    } else {
       xids = (Glyph *) malloc(sizeof(Glyph) * glyphCnt);
+      if (xids == NULL) {
+          if (xelts != &selts[0]) {
+            free(xelts);
+          }
+          return;
+      }
     }
 
-    if ((ids = (jint *) (*env)->GetPrimitiveArrayCritical(env, glyphIDArray, NULL)) == NULL) {
-      return;
+    if ((ids = (jint *)
+         (*env)->GetPrimitiveArrayCritical(env, glyphIDArray, NULL)) == NULL) {
+        if (xelts != &selts[0]) {
+            free(xelts);
+        }
+        if (xids != &sids[0]) {
+            free(xids);
+        }
+        return;
     }
     if ((elts = (jint *)
           (*env)->GetPrimitiveArrayCritical(env, eltArray, NULL)) == NULL) {
         (*env)->ReleasePrimitiveArrayCritical(env,
                                               glyphIDArray, ids, JNI_ABORT);
-      return;
+        if (xelts != &selts[0]) {
+            free(xelts);
+        }
+        if (xids != &sids[0]) {
+            free(xids);
+        }
+        return;
     }
 
     for (i=0; i < glyphCnt; i++) {
@@ -724,11 +763,11 @@
     (*env)->ReleasePrimitiveArrayCritical(env, eltArray, elts, JNI_ABORT);
 
     if (xelts != &selts[0]) {
-      free(xelts);
+        free(xelts);
     }
 
     if (xids != &sids[0]) {
-      free(xids);
+        free(xids);
     }
 }
 
@@ -762,8 +801,12 @@
       }
     }
 
-    if ((rects = (jint *) (*env)->GetPrimitiveArrayCritical(env, rectArray, NULL)) == NULL) {
-      return;
+    if ((rects = (jint*)
+         (*env)->GetPrimitiveArrayCritical(env, rectArray, NULL)) == NULL) {
+        if (xRects != &sRects[0]) {
+            free(xRects);
+        }
+        return;
     }
 
     for (i=0; i < rectCnt; i++) {