libdvr: Linux Yocto SDK Coverity Check [1/1]

PD#SWPL-172651

Problem:
There are Coverity issues.

Solution:
1. Fix UNUSED_VALUE issues.
2. Fix UNINIT issues.
3. Fix DC.STRING_BUFFER issues.
4. Fix REVERSE_INULL issues.
5. Fix NO_EFFECT issues.

Verify:
Tested pass on ohm

Signed-off-by: Wentao.MA <wentao.ma@amlogic.com>
Change-Id: I9c21100e5b33704059713eed01f47ea584275f79
diff --git a/include/list.h b/include/list.h
index 7bc982a..6af8627 100644
--- a/include/list.h
+++ b/include/list.h
@@ -552,7 +552,7 @@
 #define list_for_each_entry_safe(pos, n, head, member)            \
     for (pos = list_entry((head)->c_next, typeof(*pos), member),    \
         n = list_entry(pos->member.c_next, typeof(*pos), member);    \
-         &pos->member != (head);                     \
+         (pos > 0) && &pos->member != (head);                     \
          pos = n, n = list_entry(n->member.c_next, typeof(*n), member))
 
 /**
diff --git a/src/dvr_record.c b/src/dvr_record.c
index f013ee6..4099578 100644
--- a/src/dvr_record.c
+++ b/src/dvr_record.c
@@ -310,6 +310,7 @@
     DVR_INFO("%s, malloc failed", __func__);
     return NULL;
   }
+  memset(buf,0,block_size);
 
   if (p_ctx->is_secure_mode) {
     buf_out = (uint8_t *)malloc(p_ctx->secbuf_size + 188);
@@ -349,7 +350,7 @@
       if (p_ctx->is_new_dmx) {
         /* We resolve the below invoke for dvbcore to be under safety status */
         memset(&new_dmx_secure_buf, 0, sizeof(new_dmx_secure_buf));
-        len = record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf,
+        record_device_read(p_ctx->dev_handle, &new_dmx_secure_buf,
             sizeof(new_dmx_secure_buf), 10);
 
         /* Read data from secure demux TA */
@@ -420,6 +421,7 @@
       gettimeofday(&t3, NULL);
       /* Out buffer length may not equal in buffer length */
       if (crypto_params.output_size > 0) {
+        // coverity[self_assign]
         SEG_CALL_RET(write, (p_ctx->segment_handle, buf_out, crypto_params.output_size), ret);
         len = crypto_params.output_size;
       } else {
@@ -431,6 +433,7 @@
       am_crypt_des_crypt(p_ctx->cryptor, buf_out, buf, &crypt_len, 0);
       len = crypt_len;
       gettimeofday(&t3, NULL);
+      // coverity[self_assign]
       SEG_CALL_RET(write, (p_ctx->segment_handle, buf_out, len), ret);
     } else {
       if (first_read == 0) {
@@ -438,6 +441,7 @@
         DVR_INFO("%s:%d,first read ts", __func__,__LINE__);
       }
       gettimeofday(&t3, NULL);
+      // coverity[self_assign]
       SEG_CALL_RET(write, (p_ctx->segment_handle, buf, len), ret);
     }
     gettimeofday(&t4, NULL);
@@ -457,6 +461,7 @@
     if (len > 0 && SEG_CALL_IS_VALID(tell_position)) {
       /* Do time index */
       uint8_t *index_buf = (p_ctx->enc_func || p_ctx->cryptor)? buf_out : buf;
+      // coverity[self_assign]
       SEG_CALL_RET(tell_position, (p_ctx->segment_handle), pos);
       has_pcr = record_do_pcr_index(p_ctx, index_buf, len);
       if (has_pcr == 0 && p_ctx->index_type == DVR_INDEX_TYPE_INVALID) {
@@ -492,6 +497,7 @@
 
       /*Duration need use pcr to calculate, todo...*/
       if (p_ctx->index_type == DVR_INDEX_TYPE_PCR) {
+        // coverity[self_assign]
         SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), p_ctx->segment_info.duration);
         if (pre_time == 0)
           pre_time = p_ctx->segment_info.duration;
@@ -509,10 +515,12 @@
           p_ctx->check_pts_count == CHECK_PTS_MAX_COUNT) {
         DVR_INFO("%s change time from pcr to local time", __func__);
         if (pcr_rec_len == 0) {
+          // coverity[self_assign]
           SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), pcr_rec_len);
         }
         p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
         if (pcr_rec_len == 0) {
+          // coverity[self_assign]
           SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), pcr_rec_len);
         }
         clock_gettime(CLOCK_MONOTONIC, &start_ts);
@@ -525,6 +533,7 @@
          if (diff > 3000) {
             DVR_INFO("%s no pcr change time from pcr to local time diff[%d]", __func__, diff);
             if (pcr_rec_len == 0) {
+              // coverity[self_assign]
               SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), pcr_rec_len);
             }
             p_ctx->index_type = DVR_INDEX_TYPE_LOCAL_CLOCK;
@@ -536,6 +545,7 @@
         pre_time = p_ctx->segment_info.duration + DVR_STORE_INFO_TIME;
         time_t duration = p_ctx->segment_info.duration;
         if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
+          // coverity[self_assign]
           SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), p_ctx->segment_info.duration);
         }
         SEG_CALL(store_info, (p_ctx->segment_handle, &p_ctx->segment_info));
@@ -564,6 +574,7 @@
       record_status.state = p_ctx->state;
       record_status.info.id = p_ctx->segment_info.id;
       if (p_ctx->index_type == DVR_INDEX_TYPE_LOCAL_CLOCK) {
+        // coverity[self_assign]
         SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), record_status.info.duration);
       } else
         record_status.info.duration = p_ctx->segment_info.duration;
@@ -813,12 +824,14 @@
     open_params.mode = SEGMENT_MODE_WRITE;
     open_params.force_sysclock = p_ctx->force_sysclock;
 
+    // coverity[self_assign]
     SEG_CALL_RET(open, (&open_params, &p_ctx->segment_handle), ret);
     DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
     if (SEG_CALL_IS_VALID(ioctl)) {
       DVR_Control_t *pc;
       list_for_each_entry(pc, &p_ctx->segment_ctrls, head) {
+        // coverity[self_assign]
         SEG_CALL_RET(ioctl, (p_ctx->segment_handle, pc->cmd, pc->data, pc->size), ret);
         DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
       }
@@ -847,6 +860,7 @@
     DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
   }
 
+  // coverity[self_assign]
   SEG_CALL_RET(store_info, (p_ctx->segment_handle, &p_ctx->segment_info), ret);
   DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
@@ -895,11 +909,13 @@
     }
   }
 
+  // coverity[self_assign]
   SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), p_ctx->segment_info.duration);
 
   /*Update segment info*/
   memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
 
+  // coverity[self_assign]
   SEG_CALL_RET(store_info, (p_ctx->segment_handle, p_info), ret);
   DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
@@ -909,6 +925,7 @@
       __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets, params->segment.nb_pids);
 
   /*Close current segment*/
+  // coverity[self_assign]
   SEG_CALL_RET(close, (p_ctx->segment_handle), ret);
   DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
@@ -925,6 +942,7 @@
     open_params.force_sysclock = p_ctx->force_sysclock;
 
     DVR_INFO("%s: p_ctx->location:%s  params->location:%s", __func__, p_ctx->location,params->location);
+    // coverity[self_assign]
     SEG_CALL_RET(open, (&open_params, &p_ctx->segment_handle), ret);
     DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
@@ -932,6 +950,7 @@
       DVR_Control_t *pc;
       list_for_each_entry(pc, &p_ctx->segment_ctrls, head) {
         DVR_INFO("%s, replay ctrl[cmd:%d]", __func__, pc->cmd);
+        // coverity[self_assign]
         SEG_CALL_RET(ioctl, (p_ctx->segment_handle, pc->cmd, pc->data, pc->size), ret);
         DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
       }
@@ -976,6 +995,7 @@
   //DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
   /*Update segment info*/
+  // coverity[self_assign]
   SEG_CALL_RET(store_info, (p_ctx->segment_handle, &p_ctx->segment_info), ret);
   DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
 
@@ -1035,11 +1055,13 @@
     }
   }
 
+  // coverity[self_assign]
   SEG_CALL_RET(tell_total_time, (p_ctx->segment_handle), p_ctx->segment_info.duration);
 
   /*Update segment info*/
   memcpy(p_info, &p_ctx->segment_info, sizeof(p_ctx->segment_info));
 
+  // coverity[self_assign]
   SEG_CALL_RET(store_info, (p_ctx->segment_handle, p_info), ret);
   if (ret != DVR_SUCCESS)
     goto end;
@@ -1050,6 +1072,7 @@
       __func__, p_info->id, p_info->nb_pids, p_info->duration, p_info->size, p_info->nb_packets);
 
 end:
+  // coverity[self_assign]
   SEG_CALL_RET(close, (p_ctx->segment_handle), ret);
   p_ctx->segment_handle = NULL;
   DVR_RETURN_IF_FALSE(ret == DVR_SUCCESS);
@@ -1127,6 +1150,7 @@
     /* Pull VOD record should use PCR time index */
     DVR_INFO("%s has no pcr, can NOT do time index", __func__);
   }
+  // coverity[self_assign]
   SEG_CALL_RET(write, (p_ctx->segment_handle, buffer, len), ret);
   if (ret != len) {
     DVR_INFO("%s write error ret:%d len:%d", __func__, ret, len);
@@ -1248,6 +1272,7 @@
 
   if (SEG_CALL_IS_VALID(ioctl)) {
     if (p_ctx->segment_handle) {
+      // coverity[self_assign]
       SEG_CALL_RET(ioctl, (p_ctx->segment_handle, cmd, data, size), ret);
     }
 
diff --git a/src/dvr_segment.c b/src/dvr_segment.c
index 3d2cf78..dca7bbd 100644
--- a/src/dvr_segment.c
+++ b/src/dvr_segment.c
@@ -156,7 +156,7 @@
   DVR_RETURN_IF_FALSE(pp_segment_ids);
 
   memset(fpath, 0, sizeof(fpath));
-  sprintf(fpath, "%s.list", location);
+  snprintf(fpath, DVR_MAX_LOCATION_SIZE-5, "%s.list", location);
 
   fp = fopen(fpath, "r");
   if (fp != NULL) { /*the list file exists*/
@@ -181,7 +181,7 @@
   } else { /*the list file does not exist*/
     uint32_t id = 0;
     memset(cmd, 0, sizeof(cmd));
-    sprintf(cmd, "ls -l \"%s\"-*.ts | wc -l", location);
+    snprintf(cmd, DVR_MAX_LOCATION_SIZE+44, "ls -l \"%s\"-*.ts | wc -l", location);
     fp = popen(cmd, "r");
     DVR_RETURN_IF_FALSE(fp);
     memset(buf, 0, sizeof(buf));
@@ -198,7 +198,7 @@
 
     /*try to get the 1st segment id*/
     memset(cmd, 0, sizeof(cmd));
-    sprintf(cmd, "ls \"%s\"-*.ts", location);
+    snprintf(cmd, DVR_MAX_LOCATION_SIZE+55,"ls \"%s\"-*.ts", location);
     fp = popen(cmd, "r");
     DVR_RETURN_IF_FALSE(fp);
     memset(buf, 0, sizeof(buf));
@@ -239,7 +239,7 @@
 {
   int ret;
   Segment_OpenParams_t open_params;
-  Segment_Handle_t segment_handle;
+  Segment_Handle_t segment_handle = NULL;
 
   DVR_RETURN_IF_FALSE(location);
   DVR_RETURN_IF_FALSE(p_info);
@@ -274,7 +274,7 @@
 {
   int ret;
   Segment_OpenParams_t open_params;
-  Segment_Handle_t segment_handle;
+  Segment_Handle_t segment_handle = NULL;
 
   DVR_RETURN_IF_FALSE(location);
   DVR_RETURN_IF_FALSE(list);
@@ -322,7 +322,7 @@
 
   DVR_INFO("%s op[%d] location:%s, nb_segments:%d", __func__, op, location, nb_segments);
   memset(fpath, 0, sizeof(fpath));
-  sprintf(fpath, "%s.list", location);
+  snprintf(fpath, DVR_MAX_LOCATION_SIZE-5, "%s.list", location);
   fp = fopen(fpath, (op == SEGMENT_OP_ADD) ? "a+" : "w+");
   if (!fp) {
     DVR_INFO("failed to open list file, err:%d:%s", errno, strerror(errno));
diff --git a/src/dvr_wrapper.c b/src/dvr_wrapper.c
index e20c8e5..5a2223e 100644
--- a/src/dvr_wrapper.c
+++ b/src/dvr_wrapper.c
@@ -852,14 +852,10 @@
     DVR_PlaybackSegmentFlag_t flags)
 {
   DVR_WrapperPlaybackSegmentInfo_t *p_seg;
-  int error;
-
-  error = 0;
   p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
   if (!p_seg) {
-    error = DVR_FAILURE;
     DVR_WRAPPER_INFO("memory fail\n");
-    return error;
+    return DVR_FAILURE;
   }
 
   /*copy the original segment info*/
@@ -879,7 +875,7 @@
   list_add(p_seg, &ctx->segments);
   DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
 
-  error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
+  int error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
   if (error) {
     DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
   } else {
@@ -1689,8 +1685,6 @@
   DVR_RETURN_IF_FALSE(playback);
   DVR_RETURN_IF_FALSE(p_pids);
 
-  ctx_record = NULL;
-
   /*lock the recorder to avoid changing the recording segments*/
   ctx_record = ctx_getRecord(sn_timeshift_record);
 
@@ -1929,10 +1923,8 @@
         DVR_RecordSegmentInfo_t *segment_tmp = NULL;
         list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
         {
-            if (segment) {
-              list_del(&segment->head);
-              free(segment);
-            }
+          list_del(&segment->head);
+          free(segment);
         }
     }
 
@@ -2478,6 +2470,7 @@
 
   WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
 
+  memset((void*)&play_status,0,sizeof(DVR_PlaybackStatus_t));
   dvr_playback_get_status(ctx->playback.player, &play_status);
 
   ctx->playback.seg_status = play_status;
@@ -2539,7 +2532,7 @@
   DVR_RETURN_IF_FALSE(location);
 
   /*del the stats file*/
-  sprintf(fpath, "%s.stats", location);
+  snprintf(fpath, DVR_MAX_LOCATION_SIZE-6, "%s.stats", location);
   unlink(fpath);
 
   return dvr_segment_del_by_location(location);
@@ -2557,7 +2550,7 @@
     memset(p_info, 0, sizeof(p_info[0]));
 
   memset(fpath, 0, sizeof(fpath));
-  sprintf(fpath, "%s.stats", location);
+  snprintf(fpath, DVR_MAX_LOCATION_SIZE-6, "%s.stats", location);
 
   /*stats file exists*/
   if ((fp = fopen(fpath, "r"))) {
@@ -2668,10 +2661,8 @@
       DVR_RecordSegmentInfo_t *segment_tmp = NULL;
       list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
       {
-          if (segment) {
-            list_del(&segment->head);
-            free(segment);
-          }
+        list_del(&segment->head);
+        free(segment);
       }
     }
     free(p_ids);
@@ -2742,7 +2733,7 @@
   DVR_RETURN_IF_FALSE(location);
   DVR_RETURN_IF_FALSE(p_status);
 
-  sprintf(fpath, "%s.stats", location);
+  snprintf(fpath, DVR_MAX_LOCATION_SIZE-6, "%s.stats", location);
 
   /*stats file*/
   if ((fp = fopen(fpath, "w"))) {
@@ -2779,6 +2770,7 @@
       ctx->record.param_update.segment.nb_pids++;
     }
   }
+  memset((void*)&seg_info,0,sizeof(DVR_RecordSegmentInfo_t));
   error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
   {
     DVR_RecordSegmentInfo_t new_seg_info =
diff --git a/src/segment_dataout.c b/src/segment_dataout.c
index bad2928..801b254 100644
--- a/src/segment_dataout.c
+++ b/src/segment_dataout.c
@@ -80,14 +80,11 @@
 
 loff_t segment_dataout_tell_total_time(Segment_Handle_t handle)
 {
-  uint64_t pts = ULLONG_MAX;
   Segment_Context_t *p_ctx = (Segment_Context_t *)handle;
 
   DVR_RETURN_IF_FALSE(p_ctx);
 
-  pts = p_ctx->cur_time;
-
-  return pts;
+  return p_ctx->cur_time;
 }
 
 int segment_dataout_store_info(Segment_Handle_t handle, Segment_StoreInfo_t *p_info)