8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc
authorjgodinez
Fri, 08 Feb 2013 11:25:42 -0800
changeset 15628 228422512f97
parent 15627 46054ae31dfc
child 15629 a041f5885d90
8005129: [parfait] #1122 - #1130 native/sun/awt/medialib/mlib_Image*.c Memory leak of pointer 'k' allocated with mlib_malloc Reviewed-by: prr, vadim Contributed-by: jia-hong.chen@oracle.com
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv.h
jdk/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c
jdk/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
jdk/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv.h	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv.h	Fri Feb 08 11:25:42 2013 -0800
@@ -31,6 +31,16 @@
 extern "C" {
 #endif /* __cplusplus */
 
+// Shared macro defined for cleanup of allocated memory.
+#ifndef FREE_AND_RETURN_STATUS
+#define FREE_AND_RETURN_STATUS \
+{ \
+if (pbuff != buff) mlib_free(pbuff); \
+if (k != akernel) mlib_free(k); \
+return status; \
+}
+#endif /* FREE_AND_RETURN_STATUS */
+
 void mlib_ImageXor80_aa(mlib_u8  *dl,
                         mlib_s32 wid,
                         mlib_s32 hgt,
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c	Fri Feb 08 11:25:42 2013 -0800
@@ -253,8 +253,10 @@
   if (mn > 256) {
     dkernel = mlib_malloc(mn * sizeof(mlib_d64));
 
-    if (dkernel == NULL)
+    if (dkernel == NULL) {
+      if (dsa != dspace) mlib_free(dsa);
       return MLIB_FAILURE;
+    }
   }
 
   while (scale > 30) {
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1886,6 +1886,8 @@
   d64_2x32 dd;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1905,7 +1907,10 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   swid = wid + (m - 1);
 
@@ -1914,7 +1919,10 @@
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2318,9 +2326,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1653,6 +1653,8 @@
   mlib_s32 *buffo, *buffi;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1672,14 +1674,20 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   bsize = (n + 3)*wid;
 
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2033,9 +2041,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1160,6 +1160,8 @@
   DEF_VARS_MxN(mlib_s32);
   mlib_s32 chan2 = chan1 + chan1;
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1179,14 +1181,20 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   bsize = (n + 2)*wid;
 
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(mlib_d64)*bsize + sizeof(mlib_d64*)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (mlib_d64**)(pbuff + bsize);
   }
 
@@ -1531,9 +1539,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1886,6 +1886,8 @@
   d64_2x32 dd;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1905,7 +1907,10 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   swid = wid + (m - 1);
 
@@ -1914,7 +1919,10 @@
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2318,9 +2326,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1654,6 +1654,8 @@
   mlib_s32 *buffo, *buffi;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1673,14 +1675,20 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   bsize = (n + 3)*wid;
 
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2034,9 +2042,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1886,6 +1886,8 @@
   d64_2x32 dd;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1905,7 +1907,10 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN_ext(dst, src, k, n, dy_t, dy_b, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   swid = wid + (m - 1);
 
@@ -1914,7 +1919,10 @@
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2318,9 +2326,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c	Fri Feb 08 11:25:42 2013 -0800
@@ -1653,6 +1653,8 @@
   mlib_s32 *buffo, *buffi;
   GET_SRC_DST_PARAMETERS(DTYPE);
 
+  mlib_status status = MLIB_SUCCESS;
+
   if (scale > 30) {
     fscale *= 1.0/(1 << 30);
     scale -= 30;
@@ -1672,14 +1674,20 @@
     k[i] = kernel[i]*fscale;
   }
 
-  if (m == 1) return mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+  if (m == 1) {
+    status = mlib_ImageConv1xN(dst, src, k, n, dn, cmask);
+    FREE_AND_RETURN_STATUS;
+  }
 
   bsize = (n + 3)*wid;
 
   if ((bsize > BUFF_SIZE) || (n > MAX_N)) {
     pbuff = mlib_malloc(sizeof(FTYPE)*bsize + sizeof(FTYPE *)*2*(n + 1));
 
-    if (pbuff == NULL) return MLIB_FAILURE;
+    if (pbuff == NULL) {
+      status = MLIB_FAILURE;
+      FREE_AND_RETURN_STATUS;
+    }
     buffs = (FTYPE   **)(pbuff + bsize);
   }
 
@@ -2033,9 +2041,7 @@
     }
   }
 
-  if (pbuff != buff) mlib_free(pbuff);
-
-  return MLIB_SUCCESS;
+  FREE_AND_RETURN_STATUS;
 }
 
 /***************************************************************/
--- a/jdk/src/share/native/sun/awt/medialib/mlib_ImageCreate.c	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_ImageCreate.c	Fri Feb 08 11:25:42 2013 -0800
@@ -477,9 +477,9 @@
   im_height = mlib_ImageGetHeight(img);
   im_stride = mlib_ImageGetStride(img);
   tline     = mlib_ImageGetData(img);
+  if (tline == NULL) return NULL;
   rtable    = mlib_malloc((3 + im_height)*sizeof(mlib_u8 *));
-
-  if (rtable == NULL || tline == NULL) return NULL;
+  if (rtable == NULL) return NULL;
 
   rtable[0] = 0;
   rtable[1] = (mlib_u8*)((void **)rtable + 1);
--- a/jdk/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h	Wed Feb 06 14:45:02 2013 -0800
+++ b/jdk/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h	Fri Feb 08 11:25:42 2013 -0800
@@ -31,6 +31,16 @@
 extern "C" {
 #endif /* __cplusplus */
 
+// Shared macro defined for cleanup of allocated memory.
+#ifndef FREE_AND_RETURN_STATUS
+#define FREE_AND_RETURN_STATUS \
+{ \
+if (pbuff != buff) mlib_free(pbuff); \
+if (k != akernel) mlib_free(k); \
+return status; \
+}
+#endif /* FREE_AND_RETURN_STATUS */
+
 mlib_status mlib_c_conv2x2ext_s16(mlib_image       *dst,
                                   const mlib_image *src,
                                   mlib_s32         dx_l,