Fixed the Klocwork issue.

BZ: IMINAN-51481
Fixes: 38135184
Test: Netflix, play movies, youtube

Change-Id: Ic28085bd9789839b6b66ad8b8d53dfc017a60905
Signed-off-by: Austin Hu <austin.hu@intel.com>
(cherry picked from commit 9f457f2f828bde58ad5adb3f57d1d67219da4f36)
diff --git a/fw/msvdx/msvdx_bin.c b/fw/msvdx/msvdx_bin.c
index d58e4a3..4f77a56 100644
--- a/fw/msvdx/msvdx_bin.c
+++ b/fw/msvdx/msvdx_bin.c
@@ -123,6 +123,7 @@
         fwrite(&fw_DE3.pui8Data[i*4], 4, 1, ptr);
     }
     fclose(ptr);
+    fclose(fp_ll_dma);
 
     /* Create stitch image of 56k msvdx fw */
     ptr = fopen("unsigned_msvdx_fw_56k.bin", "w");
@@ -158,6 +159,7 @@
         fwrite(&fw_DE3.pui8Data[i*4], 4, 1, ptr);
     }
     fclose(ptr);
+    fclose(fp_ll_dma);
 
     /* Create mrfl unsigned image */
     ptr = fopen("unsigned_msvdx_fw_mrfl.bin", "w");
@@ -201,6 +203,7 @@
         fwrite(&fw_DE3.pui8Data[i*4], 4, 1, ptr);
     }
     fclose(ptr);
+    fclose(fp_ll_dma);
 
     /* Create mrfl unsigned image 56k */
     ptr = fopen("unsigned_msvdx_fw_mrfl_56k.bin", "w");
@@ -237,6 +240,7 @@
         fwrite(&fw_DE3.pui8Data[i*4], 4, 1, ptr);
     }
     fclose(ptr);
+    fclose(fp_ll_dma);
 
     return 0;
 }
diff --git a/src/pnw_H264ES.c b/src/pnw_H264ES.c
index e578340..a65f49d 100644
--- a/src/pnw_H264ES.c
+++ b/src/pnw_H264ES.c
@@ -845,9 +845,10 @@
 static VAStatus pnw__H264ES_process_slice_param(context_ENC_p ctx, object_buffer_p obj_buffer)
 {
     /* Prepare InParams for macros of current slice, insert slice header, insert do slice command */
-    VAEncSliceParameterBuffer *pBuf_per_core, *pBuffer;
+    VAEncSliceParameterBuffer *pBuf_per_core = NULL, *pBuffer = NULL;
     pnw_cmdbuf_p cmdbuf = ctx->obj_context->pnw_cmdbuf;
     PIC_PARAMS *psPicParams = (PIC_PARAMS *)(cmdbuf->pic_params_p);
+    bool pBufferAlloced = false;
     unsigned int i, j, slice_per_core;
     VAStatus vaStatus = VA_STATUS_SUCCESS;
 
@@ -871,6 +872,8 @@
             goto out2;
         }
 
+        pBufferAlloced = true;
+
         pnw__convert_sliceparameter_buffer((VAEncSliceParameterBufferH264 *)obj_buffer->buffer_data,
                                            pBuffer,
                                            ctx->Width / 16,
@@ -905,7 +908,7 @@
             drv_debug_msg(VIDEO_DEBUG_ERROR, "Run out of memory!\n");
 
             /* free the converted VAEncSliceParameterBuffer */
-            if (obj_buffer->size == sizeof(VAEncSliceParameterBufferH264))
+            if (pBufferAlloced)
                 free(pBuffer);
             free(obj_buffer->buffer_data);
             return VA_STATUS_ERROR_ALLOCATION_FAILED;
@@ -964,7 +967,7 @@
     }
 out1:
     /* free the converted VAEncSliceParameterBuffer */
-    if (obj_buffer->size == sizeof(VAEncSliceParameterBufferH264))
+    if (pBufferAlloced)
         free(pBuffer);
 
 out2:
diff --git a/src/pnw_hostheader.c b/src/pnw_hostheader.c
index ce392cb..524e733 100644
--- a/src/pnw_hostheader.c
+++ b/src/pnw_hostheader.c
@@ -204,7 +204,7 @@
     OutByteIndex = (size_bits_p[0] / 8);
 
     if (!(size_bits_p[0] & 7)) {
-        if (size_bits_p[0] >= 120) {
+        if (size_bits_p[0] >= 120 && ((mtx_hdr->Elements + 1) < MAXNUMBERELEMENTS)) {
             /* Element maximum bits send to element, time to start a new one */
             mtx_hdr->Elements++; /* Increment element index */
             /* Element pointer set to position of next element (120/8 = 15 bytes) */
@@ -751,7 +751,7 @@
     MTX_HEADER_ELEMENT **aui32ElementPointers,
     H264_SEQUENCE_HEADER_PARAMS *pSHParams, H264_CROP_PARAMS *psCrop)
 {
-    IMG_UINT8 ui8SBP;
+    IMG_UINT8 ui8SBP = 0;
 
     pnw__insert_element_token(pMTX_Header,
                               aui32ElementPointers,
@@ -917,7 +917,11 @@
     // Finally we need to align to the next byte
     // We know the size of the data in the sequence header (no MTX variables)  and start is byte aligned, so it's possible to add this field here rather than MTX ELEMENT_INSERTBYTEALIGN_H264 command.
     pnw__write_upto8bits_elements(pMTX_Header, aui32ElementPointers, 1, 1);
-    ui8SBP = (aui32ElementPointers[pMTX_Header->Elements]->Size) & 7;
+    if (pMTX_Header->Elements < MAXNUMBERELEMENTS) {
+        ui8SBP = (aui32ElementPointers[pMTX_Header->Elements]->Size) & 7;
+    } else {
+        ui8SBP = 0;
+    }
     if (ui8SBP > 0)
         pnw__write_upto8bits_elements(pMTX_Header, aui32ElementPointers, 0, 8 - (ui8SBP));
     return;
diff --git a/src/pnw_rotate.c b/src/pnw_rotate.c
index 8bbb088..f8a1fb7 100755
--- a/src/pnw_rotate.c
+++ b/src/pnw_rotate.c
@@ -581,7 +581,8 @@
 )
 {
     int width, height;
-    psb_surface_p rotate_surface;
+    psb_surface_p rotate_surface = NULL;
+    bool rotate_surfaceAlloc = false;
     VAStatus vaStatus = VA_STATUS_SUCCESS;
     int need_realloc = 0;
     unsigned int flags = 0;
@@ -615,6 +616,7 @@
     } else {
         rotate_surface = (psb_surface_p) calloc(1, sizeof(struct psb_surface_s));
         CHECK_ALLOCATION(rotate_surface);
+        rotate_surfaceAlloc = true;
     }
 
 #ifdef PSBVIDEO_MSVDX_DEC_TILING
@@ -704,6 +706,10 @@
         if (NULL == obj_context) {
             vaStatus = VA_STATUS_ERROR_INVALID_CONTEXT;
             DEBUG_FAILURE;
+            if (rotate_surface != NULL && rotate_surfaceAlloc) {
+                free(rotate_surface);
+                rotate_surface = NULL;
+            }
             return vaStatus;
         }
         unsigned long msvdx_tile = psb__tile_stride_log2_256(obj_surface->width_r);
diff --git a/src/psb_buffer.c b/src/psb_buffer.c
index 64ef263..8c796da 100644
--- a/src/psb_buffer.c
+++ b/src/psb_buffer.c
@@ -266,6 +266,7 @@
     ret = wsbmBODataUB(buf->drm_buf, size, NULL, NULL, 0, vaddr, fd);
     if (ret) {
         drv_debug_msg(VIDEO_DEBUG_ERROR, "Failed to alloc wsbm buffers, buf->drm_buf is 0x%x, size is %d, vaddr is 0x%x, fd=%d\n", buf->drm_buf, size, vaddr, fd);
+        UNLOCK_HARDWARE(driver_data);
         return 1;
     }
 
@@ -276,6 +277,7 @@
     buf->status = psb_bs_ready;
     buf->wsbm_synccpu_flag = 0;
 
+    UNLOCK_HARDWARE(driver_data);
     return VA_STATUS_SUCCESS;
 }
 
diff --git a/src/psb_drv_debug.c b/src/psb_drv_debug.c
index e274994..56a55a5 100644
--- a/src/psb_drv_debug.c
+++ b/src/psb_drv_debug.c
@@ -53,16 +53,25 @@
 #include "hwdefs/dxva_msg.h"
 #include "hwdefs/msvdx_cmds_io2.h"
 
+#define PSB_VIDEO_DEBUG_FILE "/data/mediadrm/log"
+#define PSB_VIDEO_TRACE_FILE "/data/mediadrm/trace"
+#define PSB_VIDEO_DUMP_VABUF_FILE "/data/mediadrm/dump.va"
+#define PSB_VIDEO_DUMP_VABUF_VERBOSE_FILE "/data/mediadrm/dump.va.verbose"
+#define PSB_VIDEO_DUMP_YUVBUF_FILE "/data/mediadrm/dump.yuv"
+
 void psb__open_log(void)
 {
     char log_fn[1024] = {0};
     unsigned int suffix;
+    char env_fn[1024] = {0};//used to get file name from psbvideo.conf, only to check if value is set
 
     if ((psb_video_debug_fp != NULL) && (psb_video_debug_fp != stderr)) {
         debug_fp_count++;
     } else {
         /* psb video info debug */
-        if (psb_parse_config("PSB_VIDEO_DEBUG", &log_fn[0]) == 0) {
+        if (psb_parse_config("PSB_VIDEO_DEBUG", &env_fn[0]) == 0) {
+            strcpy(log_fn, PSB_VIDEO_DEBUG_FILE);
+            drv_debug_msg(VIDEO_DEBUG_GENERAL, "Log file %s , force use %s\n", env_fn, log_fn);
             suffix = 0xffff & ((unsigned int)time(NULL));
             snprintf(log_fn + strnlen(log_fn, 1024),
                      (1024 - 8 - strnlen(log_fn, 1024)),
@@ -84,7 +93,9 @@
         }
     }
 
-    if(psb_parse_config("PSB_VIDEO_TRACE", &log_fn[0]) == 0) {
+    if(psb_parse_config("PSB_VIDEO_TRACE", &env_fn[0]) == 0) {
+        strcpy(log_fn, PSB_VIDEO_TRACE_FILE);
+        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Log file %s , force use %s\n", env_fn, log_fn);
         unsigned int suffix = 0xffff & ((unsigned int)time(NULL));
         time_t curtime;
         
@@ -106,8 +117,8 @@
     }
 
     /* debug level include error, warning, general, init, entry, ...... */
-    if(psb_parse_config("PSB_VIDEO_DEBUG_LEVEL", &log_fn[0]) == 0) {
-        psb_video_debug_level = atoi(log_fn);
+    if(psb_parse_config("PSB_VIDEO_DEBUG_LEVEL", &env_fn[0]) == 0) {
+        psb_video_debug_level = atoi(env_fn);
 #ifdef ANDROID
         ALOGD("psb_video_debug_level is %d parsed.\n", psb_video_debug_level);
 #endif
@@ -116,8 +127,8 @@
     }
 
     /* control debug output option, logcat output or print to file */
-    if(psb_parse_config("PSB_VIDEO_DEBUG_OPTION", &log_fn[0]) == 0) {
-        psb_video_debug_option = atoi(log_fn);
+    if(psb_parse_config("PSB_VIDEO_DEBUG_OPTION", &env_fn[0]) == 0) {
+        psb_video_debug_option = atoi(env_fn);
 #ifdef ANDROID
         ALOGD("psb_video_debug_option is %d parsed.\n", psb_video_debug_option);
 #endif
@@ -126,8 +137,8 @@
     }
 
     /* trace level include vabuf, cmdmsg buf, aux buffer, lldma */
-    if(psb_parse_config("PSB_VIDEO_TRACE_LEVEL", &log_fn[0]) == 0) {
-        psb_video_trace_level = atoi(log_fn);
+    if(psb_parse_config("PSB_VIDEO_TRACE_LEVEL", &env_fn[0]) == 0) {
+        psb_video_trace_level = atoi(env_fn);
 #ifdef ANDROID
         ALOGD("psb_video_trace_level is %d parsed.\n", psb_video_trace_level);
 #endif
@@ -136,8 +147,8 @@
     }
 
     /* control trace output option, logcat output or print to file */
-    if(psb_parse_config("PSB_VIDEO_TRACE_OPTION", &log_fn[0]) == 0) {
-        psb_video_trace_option = atoi(log_fn);
+    if(psb_parse_config("PSB_VIDEO_TRACE_OPTION", &env_fn[0]) == 0) {
+        psb_video_trace_option = atoi(env_fn);
 #ifdef ANDROID
         ALOGD("psb_video_debug_option is %d parsed.\n", psb_video_trace_option);
 #endif
@@ -146,8 +157,8 @@
     }
 
     /* cmdbuf dump, every frame decoded cmdbuf dump to /data/ctrlAlloc%i.txt */
-    if(psb_parse_config("PSB_VIDEO_DUMP_CMDBUF", &log_fn[0]) == 0) {
-        if(strstr(log_fn, "true") != NULL)
+    if(psb_parse_config("PSB_VIDEO_DUMP_CMDBUF", &env_fn[0]) == 0) {
+        if(strstr(env_fn, "true") != NULL)
             psb_video_dump_cmdbuf = TRUE;
         else
             psb_video_dump_cmdbuf = FALSE;
@@ -159,7 +170,9 @@
     }
 
     /* psb video va buffers dump */
-    if(psb_parse_config("PSB_VIDEO_DUMP_VABUF", &log_fn[0]) == 0) {
+    if(psb_parse_config("PSB_VIDEO_DUMP_VABUF", &env_fn[0]) == 0) {
+        strcpy(log_fn, PSB_VIDEO_DUMP_VABUF_FILE);
+        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Log file %s , force use %s\n", env_fn, log_fn);
         unsigned int suffix = 0xffff & ((unsigned int)time(NULL));
         /* Make sure there is space left for suffix */
         log_fn[1024 - 12] = '\0';
@@ -174,7 +187,9 @@
     }
 
     /* psb video va buffer verbose dump */
-    if(psb_parse_config("PSB_VIDEO_DUMP_VABUF_VERBOSE", &log_fn[0]) == 0) {
+    if(psb_parse_config("PSB_VIDEO_DUMP_VABUF_VERBOSE", &env_fn[0]) == 0) {
+        strcpy(log_fn, PSB_VIDEO_DUMP_VABUF_VERBOSE_FILE);
+        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Log file %s , force use %s\n", env_fn, log_fn);
         unsigned int suffix = 0xffff & ((unsigned int)time(NULL));
         /* Make sure there is space left for suffix */
         log_fn[1024 - 12] = '\0';
@@ -189,7 +204,9 @@
     }
 
     /* dump decoded surface to a yuv file */
-    if(psb_parse_config("PSB_VIDEO_DUMP_YUVBUF", &log_fn[0]) == 0) {
+    if(psb_parse_config("PSB_VIDEO_DUMP_YUVBUF", &env_fn[0]) == 0) {
+        strcpy(log_fn, PSB_VIDEO_DUMP_YUVBUF_FILE);
+        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Log file %s , force use %s\n", env_fn, log_fn);
         unsigned int suffix = 0xffff & ((unsigned int)time(NULL));
         /* Make sure there is space left for suffix */
         log_fn[1024 - 12] = '\0';
diff --git a/src/psb_drv_video.c b/src/psb_drv_video.c
index 7417b25..9f5b465 100755
--- a/src/psb_drv_video.c
+++ b/src/psb_drv_video.c
@@ -742,24 +742,36 @@
     unsigned int initalized_info_flag = 1;
     VASurfaceAttribExternalBuffers  *pExternalBufDesc = NULL;
     PsbSurfaceAttributeTPI attribute_tpi;
+    attribute_tpi.buffers = NULL;
+    bool attribute_tpi_buffersAlloced = false;
 
     CHECK_INVALID_PARAM(num_surfaces <= 0);
     CHECK_SURFACE(surface_list);
 
     if ((attrib_list != NULL) && (num_attribs > 0)) {
         for (i = 0; i < num_attribs; i++, attrib_list++) {
-            if (!attrib_list)
+            if (!attrib_list) {
+                if(attribute_tpi.buffers != NULL)
+                    free(attribute_tpi.buffers);
                 return VA_STATUS_ERROR_INVALID_PARAMETER;
+            }
             switch (attrib_list->type) {
             case VASurfaceAttribExternalBufferDescriptor:
                 {
                     pExternalBufDesc = (VASurfaceAttribExternalBuffers *)attrib_list->value.value.p;
                     if (pExternalBufDesc == NULL) {
+                        if(attribute_tpi.buffers != NULL)
+                            free(attribute_tpi.buffers);
                         drv_debug_msg(VIDEO_DEBUG_ERROR, "Invalid VASurfaceAttribExternalBuffers.\n");
                         return VA_STATUS_ERROR_INVALID_PARAMETER;
                     }
                     attribute_tpi.type = memory_type;
+                    if (attribute_tpi_buffersAlloced == true && attribute_tpi.buffers != NULL) {
+                        free(attribute_tpi.buffers);
+                        attribute_tpi.buffers = NULL;
+                    }
                     attribute_tpi.buffers = malloc(sizeof(long) * pExternalBufDesc->num_buffers);
+                    attribute_tpi_buffersAlloced = true;
                     attribute_tpi.width = pExternalBufDesc->width;
                     attribute_tpi.height = pExternalBufDesc->height;
                     attribute_tpi.count = pExternalBufDesc->num_buffers;
@@ -800,6 +812,8 @@
                             memory_type = VAExternalMemoryNULL;
                             break;
                         default:
+                            if (attribute_tpi.buffers != NULL)
+                                free(attribute_tpi.buffers);
                             drv_debug_msg(VIDEO_DEBUG_ERROR, "Unsupported memory type.\n");
                             return VA_STATUS_ERROR_INVALID_PARAMETER;
 
@@ -820,6 +834,8 @@
                 }
                 break;
             default:
+                if (attribute_tpi.buffers != NULL)
+                    free(attribute_tpi.buffers);
                 drv_debug_msg(VIDEO_DEBUG_ERROR, "Unsupported attribute.\n");
                 return VA_STATUS_ERROR_INVALID_PARAMETER;
             }
@@ -1263,89 +1279,126 @@
     }
 
     for (i = 0; i < cmdbuf_num; i++) {
-        void  *cmdbuf = NULL;
 #ifndef BAYTRAIL
         if (encode) { /* Topaz encode context */
 #ifdef PSBVIDEO_MRFL
-            if (IS_MRFL(obj_context->driver_data))
-                cmdbuf = calloc(1, sizeof(struct tng_cmdbuf_s));
+            if (IS_MRFL(obj_context->driver_data)) {
+                obj_context->tng_cmdbuf_list[i] = calloc(1, sizeof(struct tng_cmdbuf_s));
+                if (NULL == obj_context->tng_cmdbuf_list[i]) {
+                    vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
 #ifdef PSBVIDEO_MFLD
-            if (IS_MFLD(obj_context->driver_data))
-                cmdbuf = calloc(1, sizeof(struct pnw_cmdbuf_s));
+            if (IS_MFLD(obj_context->driver_data)) {
+                obj_context->pnw_cmdbuf_list[i] = calloc(1, sizeof(struct pnw_cmdbuf_s));
+                if (NULL == obj_context->pnw_cmdbuf_list[i]) {
+                    vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
         } else if (proc) { /* VSP VPP context */
             /* VED two pass rotation under VPP API */
-            if (driver_data->ved_vpp)
-                cmdbuf =  calloc(1, sizeof(struct psb_cmdbuf_s));
+            if (driver_data->ved_vpp) {
+                obj_context->cmdbuf_list[i] = calloc(1, sizeof(struct psb_cmdbuf_s));
+                if (NULL == obj_context->cmdbuf_list[i]) {
+                    vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #ifdef PSBVIDEO_MRFL_VPP
-            else if (IS_MRFL(obj_context->driver_data))
-                cmdbuf = calloc(1, sizeof(struct vsp_cmdbuf_s));
+            else if (IS_MRFL(obj_context->driver_data)) {
+                obj_context->vsp_cmdbuf_list[i] = calloc(1, sizeof(struct vsp_cmdbuf_s));
+                if (NULL == obj_context->vsp_cmdbuf_list[i]) {
+                    vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
-        } else /* MSVDX decode context */
+        } else /* MSVDX decode context */ {
 #endif
-            cmdbuf =  calloc(1, sizeof(struct psb_cmdbuf_s));
-
-        if (NULL == cmdbuf) {
-            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
-            DEBUG_FAILURE;
-            break;
+            obj_context->cmdbuf_list[i] = calloc(1, sizeof(struct psb_cmdbuf_s));
+            if (NULL == obj_context->cmdbuf_list[i]) {
+                vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+                DEBUG_FAILURE;
+                break;
+            }
         }
 
 #ifndef BAYTRAIL
         if (encode) { /* Topaz encode context */
 
 #ifdef PSBVIDEO_MRFL
-            if (IS_MRFL(obj_context->driver_data))
-                vaStatus = tng_cmdbuf_create(obj_context, driver_data, (tng_cmdbuf_p)cmdbuf);
+            if (IS_MRFL(obj_context->driver_data)) {
+                vaStatus = tng_cmdbuf_create(obj_context, driver_data, (tng_cmdbuf_p)obj_context->tng_cmdbuf_list[i]);
+                if (VA_STATUS_SUCCESS != vaStatus) {
+                    free(obj_context->tng_cmdbuf_list[i]);
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
 #ifdef PSBVIDEO_MFLD
-            if (IS_MFLD(obj_context->driver_data))
-                vaStatus = pnw_cmdbuf_create(obj_context, driver_data, (pnw_cmdbuf_p)cmdbuf);
+            if (IS_MFLD(obj_context->driver_data)) {
+                vaStatus = pnw_cmdbuf_create(obj_context, driver_data, (pnw_cmdbuf_p)obj_context->pnw_cmdbuf_list[i]);
+                if (VA_STATUS_SUCCESS != vaStatus) {
+                    free(obj_context->pnw_cmdbuf_list[i]);
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
         } else if (proc) { /* VSP VPP context */
-            if (driver_data->ved_vpp)
-                vaStatus = psb_cmdbuf_create(obj_context, driver_data, (psb_cmdbuf_p)cmdbuf);
+            if (driver_data->ved_vpp) {
+                vaStatus = psb_cmdbuf_create(obj_context, driver_data, (psb_cmdbuf_p)obj_context->cmdbuf_list[i]);
+                if (VA_STATUS_SUCCESS != vaStatus) {
+                    free(obj_context->cmdbuf_list[i]);
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #ifdef PSBVIDEO_MRFL_VPP
-            else if (IS_MRFL(obj_context->driver_data))
-                vaStatus = vsp_cmdbuf_create(obj_context, driver_data, (vsp_cmdbuf_p)cmdbuf);
+            else if (IS_MRFL(obj_context->driver_data)) {
+                vaStatus = vsp_cmdbuf_create(obj_context, driver_data, (vsp_cmdbuf_p)obj_context->vsp_cmdbuf_list[i]);
+                if (VA_STATUS_SUCCESS != vaStatus) {
+                    free(obj_context->vsp_cmdbuf_list[i]);
+                    DEBUG_FAILURE;
+                    break;
+                }
+            }
 #endif
-        } else /* MSVDX decode context */
+        } else /* MSVDX decode context */ {
 #endif
-            vaStatus = psb_cmdbuf_create(obj_context, driver_data, (psb_cmdbuf_p)cmdbuf);
-
-        if (VA_STATUS_SUCCESS != vaStatus) {
-            free(cmdbuf);
-            DEBUG_FAILURE;
-            break;
+            vaStatus = psb_cmdbuf_create(obj_context, driver_data, (psb_cmdbuf_p)obj_context->cmdbuf_list[i]);
+            if (VA_STATUS_SUCCESS != vaStatus) {
+                free(obj_context->cmdbuf_list[i]);
+                DEBUG_FAILURE;
+                break;
+            }
         }
 
 #ifndef BAYTRAIL
         if (encode) { /* Topaz encode context */
             if (i >= LNC_MAX_CMDBUFS_ENCODE) {
-                free(cmdbuf);
+#ifdef PSBVIDEO_MRFL
+                tng_cmdbuf_destroy((tng_cmdbuf_p)obj_context->tng_cmdbuf_list[i]);
+                free(obj_context->tng_cmdbuf_list[i]);
+#endif
+#ifdef PSBVIDEO_MFLD
+                pnw_cmdbuf_destroy((pnw_cmdbuf_p)obj_context->pnw_cmdbuf_list[i]);
+                free(obj_context->pnw_cmdbuf_list[i]);
+#endif
                 DEBUG_FAILURE;
                 break;
             }
-
-#ifdef PSBVIDEO_MRFL
-            if (IS_MRFL(obj_context->driver_data))
-                obj_context->tng_cmdbuf_list[i] = (tng_cmdbuf_p)cmdbuf;
+        }
 #endif
-#ifdef PSBVIDEO_MFLD
-            if (IS_MFLD(obj_context->driver_data))
-                obj_context->pnw_cmdbuf_list[i] = (pnw_cmdbuf_p)cmdbuf;
-#endif
-        } else if (proc) { /* VSP VPP context */
-            if (driver_data->ved_vpp)
-                obj_context->cmdbuf_list[i] = (psb_cmdbuf_p)cmdbuf;
-#ifdef PSBVIDEO_MRFL_VPP
-            else if (IS_MRFL(obj_context->driver_data))
-                obj_context->vsp_cmdbuf_list[i] = (vsp_cmdbuf_p)cmdbuf;
-#endif
-        } else /* MSVDX decode context */
-#endif
-            obj_context->cmdbuf_list[i] = (psb_cmdbuf_p)cmdbuf;
     }
 
     obj_context->cmdbuf_current = -1;
@@ -2756,6 +2809,8 @@
 
     if (i > *num_attribs) {
         *num_attribs = i;
+        if (attribs != NULL)
+            free(attribs);
         return VA_STATUS_ERROR_MAX_NUM_EXCEEDED;
     }
 
diff --git a/src/tng_yuv_processor.c b/src/tng_yuv_processor.c
index 06c5b5e..8313bf3 100755
--- a/src/tng_yuv_processor.c
+++ b/src/tng_yuv_processor.c
@@ -73,7 +73,12 @@
 
     if (!dec_ctx) {
         dec_ctx = (context_DEC_p) malloc(sizeof(struct context_DEC_s));
-        CHECK_ALLOCATION(dec_ctx);
+        if(dec_ctx == NULL) {
+            free(ctx);
+            vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
+            drv_debug_msg(VIDEO_DEBUG_ERROR, "%s fails with '%d' at %s:%d\n", __FUNCTION__, vaStatus, __FILE__, __LINE__);
+            return vaStatus;
+        }
         obj_context->format_data = (void *)dec_ctx;
         ctx->has_dec_ctx = 1;
         vaStatus = vld_dec_CreateContext(dec_ctx, obj_context);
diff --git a/src/vsp_VPP.c b/src/vsp_VPP.c
index b3706de..ba2596a 100644
--- a/src/vsp_VPP.c
+++ b/src/vsp_VPP.c
@@ -362,8 +362,12 @@
 out:
 	vsp_VPP_DestroyContext(obj_context);
 
-	if (ctx)
-		free(ctx);
+    if (ctx) {
+        if(ctx->intermediate_buf != NULL)
+            free(ctx->intermediate_buf);
+        free(ctx);
+        ctx = NULL;
+    }
 
 	return vaStatus;
 }
diff --git a/src/vsp_vp8.c b/src/vsp_vp8.c
index 1e6bb9b..582032e 100644
--- a/src/vsp_vp8.c
+++ b/src/vsp_vp8.c
@@ -259,8 +259,12 @@
 out:
     vsp_VP8_DestroyContext(obj_context);
 
-    if (ctx)
+    if (ctx) {
+        if(ctx->context_buf != NULL)
+            free(ctx->context_buf);
         free(ctx);
+        ctx = NULL;
+    }
 
     return vaStatus;
 }