Fix KW issues

Signed-off-by: Tianhao Liu <tianhao.liu@intel.com>
diff --git a/encode/hevcencode.c b/encode/hevcencode.c
index 681e34e..968b355 100644
--- a/encode/hevcencode.c
+++ b/encode/hevcencode.c
@@ -2051,7 +2051,9 @@
     }
 
     /* store coded data into a file */
-    coded_fp = fopen(coded_fn,"w+");
+    if (coded_fn) {
+        coded_fp = fopen(coded_fn,"w+");
+    }
     if (coded_fp == NULL) {
         printf("Open file %s failed, exit\n", coded_fn);
         exit(1);
@@ -2298,8 +2300,10 @@
     CHECK_VASTATUS(va_status, "vaCreateSurfaces");
 
     tmp_surfaceid = calloc(2 * SURFACE_NUM, sizeof(VASurfaceID));
-    memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
-    memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
+    if (tmp_surfaceid) {
+        memcpy(tmp_surfaceid, src_surface, SURFACE_NUM * sizeof(VASurfaceID));
+        memcpy(tmp_surfaceid + SURFACE_NUM, ref_surface, SURFACE_NUM * sizeof(VASurfaceID));
+    }
 
     /* Create a context for this encode pipe */
     va_status = vaCreateContext(va_dpy, config_id,
@@ -3098,8 +3102,10 @@
     struct storage_task_t *tmp;
 
     tmp = calloc(1, sizeof(struct storage_task_t));
-    tmp->display_order = display_order;
-    tmp->encode_order = encode_order;
+    if (tmp) {
+        tmp->display_order = display_order;
+        tmp->encode_order = encode_order;
+    }
 
     pthread_mutex_lock(&encode_mutex);
 
diff --git a/encode/jpegenc.c b/encode/jpegenc.c
index a17a583..5e1baa6 100644
--- a/encode/jpegenc.c
+++ b/encode/jpegenc.c
@@ -931,15 +931,6 @@
     yuv_type = atoi(argv[5]);
     quality = atoi(argv[6]);
     
-    yuv_fp = fopen(argv[3],"rb");
-    if ( yuv_fp == NULL){
-        printf("Can't open input YUV file\n");
-        return -1;
-    }
-    
-    fseeko(yuv_fp, (off_t)0, SEEK_END);
-    file_size = ftello(yuv_fp);
-    
     //<input file type: 0(I420)/1(NV12)/2(UYVY)/3(YUY2)/4(Y8)/5(RGBA)>
     switch(yuv_type)
     {
@@ -970,8 +961,16 @@
             show_help();
             return -1;
         }
-        
     }
+
+    yuv_fp = fopen(argv[3],"rb");
+    if ( yuv_fp == NULL){
+        printf("Can't open input YUV file\n");
+        return -1;
+    }
+
+    fseeko(yuv_fp, (off_t)0, SEEK_END);
+    file_size = ftello(yuv_fp);
     
     if ( (file_size < frame_size) || (file_size % frame_size) ) {
         fclose(yuv_fp);
diff --git a/encode/mpeg2vaenc.c b/encode/mpeg2vaenc.c
index fb31351..67fc5fd 100644
--- a/encode/mpeg2vaenc.c
+++ b/encode/mpeg2vaenc.c
@@ -1533,7 +1533,9 @@
     gettimeofday(&tpstart, NULL);
 
     memset(&ctx, 0, sizeof(ctx));
-    parse_args(&ctx, argc, argv);
+    if (argv) {
+        parse_args(&ctx, argc, argv);
+    }
     mpeg2enc_init(&ctx);
     mpeg2enc_run(&ctx);
     mpeg2enc_end(&ctx);
diff --git a/encode/vp8enc.c b/encode/vp8enc.c
index fce0a46..e8de122 100644
--- a/encode/vp8enc.c
+++ b/encode/vp8enc.c
@@ -1095,7 +1095,8 @@
 int main(int argc, char *argv[])
 {
   int current_frame, frame_type;
-  FILE *fp_vp8_output, *fp_yuv_input;
+  FILE *fp_vp8_output = NULL;
+  FILE *fp_yuv_input = NULL;
   uint64_t timestamp;
   struct timeval t1,t2;
   double fps, elapsed_time;
@@ -1118,7 +1119,15 @@
   settings.height = atoi(argv[2]);
   parameter_check("Height", settings.height, 16, MAX_XY_RESOLUTION);
 
-  fp_yuv_input = fopen(argv[3],"rb");
+  if( settings.rc_mode == VA_RC_VBR && settings.max_variable_bitrate < settings.frame_bitrate)
+  {
+      fprintf(stderr,"Error: max. variable bitrate should be greater than frame bitrate (--vbr_max >= --fb)\n");
+      return VP8ENC_FAIL;
+  }
+
+  if(argv[3]) {
+    fp_yuv_input = fopen(argv[3],"rb");
+  }
   if(fp_yuv_input == NULL)
   {
     fprintf(stderr,"Error: Couldn't open input file.\n");
@@ -1133,12 +1142,6 @@
     return VP8ENC_FAIL;
   }
 
-  if( settings.rc_mode == VA_RC_VBR && settings.max_variable_bitrate < settings.frame_bitrate)
-  {
-      fprintf(stderr,"Error: max. variable bitrate should be greater than frame bitrate (--vbr_max >= --fb)\n");
-      return VP8ENC_FAIL;
-  }
-
   if( settings.temporal_svc_layers == 2 && settings.intra_period % 2)
       fprintf(stderr,"Warning: Choose Key-Frame interval (--intra_period) to be integer mutliply of 2 to match temporal layer pattern");
 
diff --git a/putsurface/putsurface_common.c b/putsurface/putsurface_common.c
index 997b944..5e94322 100755
--- a/putsurface/putsurface_common.c
+++ b/putsurface/putsurface_common.c
@@ -591,7 +591,7 @@
                     printf("The validate input for -f is: 1(top field)/2(bottom field)\n");
                 break;
             case '1':
-                sscanf(optarg, "%s", str_src_fmt);
+                sscanf(optarg, "%5s", str_src_fmt);
                 csc_src_fourcc = map_str_to_vafourcc (str_src_fmt);
                 
 				if (!csc_src_fourcc) {
@@ -600,7 +600,7 @@
                 }
                 break;
             case '2':
-                sscanf(optarg, "%s", str_dst_fmt);
+                sscanf(optarg, "%5s", str_dst_fmt);
                 csc_dst_fourcc = map_str_to_vafourcc (str_dst_fmt);
                 
 				if (!csc_dst_fourcc) {
diff --git a/vendor/intel/sfcsample/VDecAccelVA.cpp b/vendor/intel/sfcsample/VDecAccelVA.cpp
index 578d423..d8cf758 100755
--- a/vendor/intel/sfcsample/VDecAccelVA.cpp
+++ b/vendor/intel/sfcsample/VDecAccelVA.cpp
@@ -76,6 +76,9 @@
 
 mvaccel::VDecAccelVAImpl::~VDecAccelVAImpl()
 {
+    if (drm_fd != -1) {
+        close(drm_fd);
+    }
 }
 
 
@@ -85,7 +88,6 @@
 
     //get display device
     int MajorVer, MinorVer;
-    int drm_fd = -1;
 
 	if (vaStatus != VA_STATUS_SUCCESS) {
         drm_fd = open("/dev/dri/renderD128", O_RDWR);
@@ -595,8 +597,10 @@
     //write to yuv file
     FILE* sfc_stream = fopen("sfc_sample_176_144_argb.yuv", "wb");
     uint32_t file_size = m_DecodeDesc.sfc_widht * m_DecodeDesc.sfc_height * 4; //ARGB format
-    fwrite(gfx_surface_buf, file_size, 1, sfc_stream);
-    fclose(sfc_stream);
+    if (sfc_stream) {
+        fwrite(gfx_surface_buf, file_size, 1, sfc_stream);
+        fclose(sfc_stream);
+    }
  
     //unlock surface and clear
     unlock_surface(m_sfcIDs[0]);
diff --git a/vendor/intel/sfcsample/VDecAccelVA.h b/vendor/intel/sfcsample/VDecAccelVA.h
index 6522eac..2a47a54 100755
--- a/vendor/intel/sfcsample/VDecAccelVA.h
+++ b/vendor/intel/sfcsample/VDecAccelVA.h
@@ -99,6 +99,7 @@
     int         create_resources();
 
 protected:
+    int drm_fd = -1;
     // VA ID & Handles
     VADisplay       m_vaDisplay;    /**< @brief VA hardware device */
     VAProfile       m_vaProfile;    /**< @brief Video decoder profile */
@@ -129,14 +130,14 @@
         NEW_H,
     };
 
-    DecodeDesc                    m_DecodeDesc;       /**< @brief decode discription */
-    VARectangle                   m_rectSrc;          /**< @brief Rectangle for source input */
-    VARectangle                   m_rectSFC;          /**< @brief Rectangle for SFC output */
-    std::vector<uint32_t>         m_in4CC;            /**< @brief input FOURCC */
-    std::vector<uint32_t>         m_out4CC;           /**< @brief output FOURCC */
-    std::map<SFC,uint32_t>        m_sfcSize;          /**< @brief SFC sizes */
-    std::vector<VASurfaceID>      m_sfcIDs;           /**< @brief sfc surfaces */
-    VAProcPipelineParameterBuffer m_vaProcBuffer;     /**< @brief sfc pipeline buffer */
+    DecodeDesc                    m_DecodeDesc{};     /**< @brief decode discription */
+    VARectangle                   m_rectSrc{};        /**< @brief Rectangle for source input */
+    VARectangle                   m_rectSFC{};        /**< @brief Rectangle for SFC output */
+    std::vector<uint32_t>         m_in4CC{};          /**< @brief input FOURCC */
+    std::vector<uint32_t>         m_out4CC{};         /**< @brief output FOURCC */
+    std::map<SFC,uint32_t>        m_sfcSize{};        /**< @brief SFC sizes */
+    std::vector<VASurfaceID>      m_sfcIDs{};         /**< @brief sfc surfaces */
+    VAProcPipelineParameterBuffer m_vaProcBuffer{};   /**< @brief sfc pipeline buffer */
 };
 } // namespace mvaccel
 #endif // MV_ACCELERATOR_VAAPI_DECODE_H
diff --git a/videoprocess/vppscaling_n_out_usrptr.cpp b/videoprocess/vppscaling_n_out_usrptr.cpp
index a853442..d41320d 100644
--- a/videoprocess/vppscaling_n_out_usrptr.cpp
+++ b/videoprocess/vppscaling_n_out_usrptr.cpp
@@ -180,7 +180,7 @@
 static VAStatus
 create_surface(VPP_ImageInfo &img_info,VASurfaceID * p_surface_id)
 {
-    VAStatus va_status;
+    VAStatus va_status = VA_STATUS_SUCCESS;
     if(img_info.memtype == VA_SURFACE_ATTRIB_MEM_TYPE_VA){
         VASurfaceAttrib    surface_attrib;
         surface_attrib.type =  VASurfaceAttribPixelFormat;