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
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv.h b/src/share/native/sun/awt/medialib/mlib_ImageConv.h
index 82243b9..24d99ce 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv.h
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv.h
@@ -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,
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c b/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c
index e489f57..88ade07 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConvMxN_ext.c
@@ -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) {
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c
index 5ddcbb3..845f9a5 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_16ext.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c
index 8c5ada3..57dbca4 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_16nw.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c
index eb70373..9d80ddd 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_32nw.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c
index 3a006ed..961ade9 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_8ext.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c
index ce33753..1f94eab 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_8nw.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c
index f412112..9fb835c 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_u16ext.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c b/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c
index eb68aef..68578a2 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageConv_u16nw.c
@@ -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;
 }
 
 /***************************************************************/
diff --git a/src/share/native/sun/awt/medialib/mlib_ImageCreate.c b/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
index fe065b3..40662d6 100644
--- a/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
+++ b/src/share/native/sun/awt/medialib/mlib_ImageCreate.c
@@ -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);
diff --git a/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h b/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h
index 8b94af0..ba1cfa3 100644
--- a/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h
+++ b/src/share/native/sun/awt/medialib/mlib_c_ImageConv.h
@@ -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,