From: Damiano Galassi <damiog@gmail.com>
Date: Fri, 12 Jan 2018 19:54:08 +0000 (+0100)
Subject: libhb: fix a number of issues reported by clang.
X-Git-Tag: 1.1.0~79
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=696ebe518b38bb186e7f6692f91a3c799f3c838c;p=handbrake

libhb: fix a number of issues reported by clang.
---

diff --git a/libhb/batch.c b/libhb/batch.c
index 1ae70d9ec..050d1e0c8 100644
--- a/libhb/batch.c
+++ b/libhb/batch.c
@@ -53,6 +53,12 @@ hb_batch_t * hb_batch_init( hb_handle_t *h, char * path )
     {
         count++;
     }
+
+    if (count == 0)
+    {
+        return NULL;
+    }
+
     files = malloc(count * sizeof(char*));
 
     // Find all regular files
diff --git a/libhb/common.c b/libhb/common.c
index 301f121dd..80ccf9f17 100644
--- a/libhb/common.c
+++ b/libhb/common.c
@@ -874,6 +874,13 @@ int hb_audio_samplerate_find_closest(int samplerate, uint32_t codec)
     const hb_rate_t * rate, * prev, * next;
 
     rate = prev = next = hb_audio_samplerate_get_next_for_codec(NULL, codec);
+
+    if (rate == NULL)
+    {
+        // This codec doesn't support any samplerate
+        return 0;
+    }
+
     while (rate != NULL && next->rate < samplerate)
     {
         rate = hb_audio_samplerate_get_next_for_codec(rate, codec);
@@ -4605,6 +4612,7 @@ int hb_audio_add(const hb_job_t * job, const hb_audio_config_t * audiocfg)
     {
         /* This most likely means the client didn't call hb_audio_config_init
          * so bail. */
+        hb_audio_close(&audio);
         return 0;
     }
 
@@ -5196,7 +5204,12 @@ char * hb_strncat_dup( const char * s1, const char * s2, size_t n )
         strcpy( str, s1 );
     else
         strcpy( str, "" );
-    strncat( str, s2, n );
+
+    if (s2)
+    {
+        strncat( str, s2, n );
+    }
+
     return str;
 }
 
diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c
index 3f884b6c1..28d260596 100644
--- a/libhb/decavcodec.c
+++ b/libhb/decavcodec.c
@@ -1349,8 +1349,8 @@ static int decodeFrame( hb_work_object_t *w, packet_info_t * packet_info )
             reordered->pts          = packet_info->pts;
             reordered->scr_sequence = packet_info->scr_sequence;
             reordered->new_chap     = packet_info->new_chap;
+            reordered_hash_add(pv, reordered);
         }
-        reordered_hash_add(pv, reordered);
 
         // libav avcodec video decoder needs AVPacket flagged with
         // AV_PKT_FLAG_KEY for some codecs. For example, sequence of
diff --git a/libhb/deccc608sub.c b/libhb/deccc608sub.c
index 27b7e23b0..10d9926d9 100644
--- a/libhb/deccc608sub.c
+++ b/libhb/deccc608sub.c
@@ -1789,23 +1789,28 @@ static int decccInit( hb_work_object_t * w, hb_job_t * job )
                 {
                     retval = 1;
                 }
-                init_eia608(pv->cc608->data608);
+                else
+                {
+                    init_eia608(pv->cc608->data608);
+                }
             }
+
+            // When rendering subs, we need to push rollup subtitles out
+            // asap (instead of waiting for a completed line) so that we
+            // do not miss the frame that they should be rendered over.
+            pv->cc608->direct_rollup = w->subtitle->config.dest == RENDERSUB;
+        }
+
+        if (!retval)
+        {
+            // Generate generic SSA Script Info.
+            int height = job->title->geometry.height - job->crop[0] - job->crop[1];
+            int width = job->title->geometry.width - job->crop[2] - job->crop[3];
+            int safe_height = 0.8 * job->title->geometry.height;
+            hb_subtitle_add_ssa_header(w->subtitle, HB_FONT_MONO,
+                                       .08 * safe_height, width, height);
         }
     }
-    if (!retval)
-    {
-        // Generate generic SSA Script Info.
-        int height = job->title->geometry.height - job->crop[0] - job->crop[1];
-        int width = job->title->geometry.width - job->crop[2] - job->crop[3];
-        int safe_height = 0.8 * job->title->geometry.height;
-        hb_subtitle_add_ssa_header(w->subtitle, HB_FONT_MONO,
-                                   .08 * safe_height, width, height);
-    }
-    // When rendering subs, we need to push rollup subtitles out
-    // asap (instead of waiting for a completed line) so that we
-    // do not miss the frame that they should be rendered over.
-    pv->cc608->direct_rollup = w->subtitle->config.dest == RENDERSUB;
     return retval;
 }
 
@@ -1842,9 +1847,13 @@ static int decccWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
 static void decccClose( hb_work_object_t * w )
 {
     hb_work_private_t * pv = w->private_data;
-    general_608_close( pv->cc608 );
-    free( pv->cc608->data608 );
-    free( pv->cc608 );
+
+    if (pv)
+    {
+        general_608_close( pv->cc608 );
+        free( pv->cc608->data608 );
+        free( pv->cc608 );
+    }
     free( w->private_data );
 }
 
diff --git a/libhb/decvobsub.c b/libhb/decvobsub.c
index 1ddb659ad..862c289da 100644
--- a/libhb/decvobsub.c
+++ b/libhb/decvobsub.c
@@ -468,7 +468,6 @@ static void resample( uint8_t * dst, uint8_t * src, int dst_w, int src_w )
     if( dst_w < src_w )
     {
         // sample down
-        err = 0;
         sum = 0;
         val = 0;
         cnt = 0;
@@ -495,7 +494,6 @@ static void resample( uint8_t * dst, uint8_t * src, int dst_w, int src_w )
     else
     {
         // sample up
-        err = 0;
         err = dst_w / 2;
         src_x = 0;
         for( dst_x = 0; dst_x < dst_w; dst_x++ )
diff --git a/libhb/encx264.c b/libhb/encx264.c
index 04ac22824..4adab5cc0 100644
--- a/libhb/encx264.c
+++ b/libhb/encx264.c
@@ -1055,7 +1055,8 @@ int apply_h264_level(const x264_api_t *api, x264_param_t *param,
             return -1;
         }
     }
-    else if(!strcasecmp(h264_level, hb_h264_level_names[0]))
+    else if(h264_level != NULL &&
+            !strcasecmp(h264_level, hb_h264_level_names[0]))
     {
         // "auto", do nothing
         return 0;
diff --git a/libhb/hb_json.c b/libhb/hb_json.c
index 672810efb..9e6a2cc1f 100644
--- a/libhb/hb_json.c
+++ b/libhb/hb_json.c
@@ -607,10 +607,6 @@ hb_dict_t* hb_job_to_dict( const hb_job_t * job )
     }
     else if (job->pts_to_start != 0 || job->pts_to_stop != 0)
     {
-        range_dict = json_pack_ex(&error, 0, "{s:o, s:o, s:o}",
-            "Type",  hb_value_string("time"),
-            "Start", hb_value_int(job->pts_to_start),
-            "End",   hb_value_int(job->pts_to_stop));
         range_dict = hb_dict_init();
         hb_dict_set(source_dict, "Type", hb_value_string("time"));
         if (job->pts_to_start > 0)
diff --git a/libhb/muxcommon.c b/libhb/muxcommon.c
index 196965bec..323173895 100644
--- a/libhb/muxcommon.c
+++ b/libhb/muxcommon.c
@@ -604,6 +604,24 @@ static int muxInit( hb_work_object_t * muxer, hb_job_t * job )
     int                i;
     hb_work_object_t * w;
 
+    /* Get a real muxer */
+    if( job->pass_id == HB_PASS_ENCODE || job->pass_id == HB_PASS_ENCODE_2ND )
+    {
+        switch( job->mux )
+        {
+            case HB_MUX_AV_MP4:
+            case HB_MUX_AV_MKV:
+                mux->m = hb_mux_avformat_init( job );
+                break;
+            default:
+                hb_error( "No muxer selected, exiting" );
+                free(mux);
+                *job->done_error = HB_ERROR_INIT;
+                *job->die = 1;
+                return -1;
+        }
+    }
+
     pv->list_work = hb_list_init();
 
     // The bit vectors must be allocated before hb_thread_init for the
@@ -623,21 +641,8 @@ static int muxInit( hb_work_object_t * muxer, hb_job_t * job )
     mux->interleave = 90000. * (double)job->vrate.den / job->vrate.num;
     mux->pts = mux->interleave;
 
-    /* Get a real muxer */
     if( job->pass_id == HB_PASS_ENCODE || job->pass_id == HB_PASS_ENCODE_2ND )
     {
-        switch( job->mux )
-        {
-        case HB_MUX_AV_MP4:
-        case HB_MUX_AV_MKV:
-            mux->m = hb_mux_avformat_init( job );
-            break;
-        default:
-            hb_error( "No muxer selected, exiting" );
-            *job->done_error = HB_ERROR_INIT;
-            *job->die = 1;
-            return -1;
-        }
         /* Create file, write headers */
         if( mux->m )
         {
diff --git a/libhb/param.c b/libhb/param.c
index 578b6a27a..764ea5820 100644
--- a/libhb/param.c
+++ b/libhb/param.c
@@ -1157,7 +1157,7 @@ int
 hb_validate_filter_preset(int filter_id, const char *preset, const char *tune,
                           const char *custom)
 {
-    if (preset == NULL && tune == NULL)
+    if (preset == NULL)
         return 1;
 
     int preset_count, tune_count;
diff --git a/libhb/platform/macosx/encca_aac.c b/libhb/platform/macosx/encca_aac.c
index 22c35bbd9..726bc710b 100644
--- a/libhb/platform/macosx/encca_aac.c
+++ b/libhb/platform/macosx/encca_aac.c
@@ -227,13 +227,13 @@ int encCoreAudioInit(hb_work_object_t *w, hb_job_t *job, enum AAC_MODE mode)
         // get available bitrates
         AudioValueRange *bitrates;
         ssize_t bitrateCounts;
-        err = AudioConverterGetPropertyInfo(pv->converter,
-                                            kAudioConverterApplicableEncodeBitRates,
-                                            &tmpsiz, NULL);
+        AudioConverterGetPropertyInfo(pv->converter,
+                                      kAudioConverterApplicableEncodeBitRates,
+                                      &tmpsiz, NULL);
         bitrates = malloc(tmpsiz);
-        err = AudioConverterGetProperty(pv->converter,
-                                        kAudioConverterApplicableEncodeBitRates,
-                                        &tmpsiz, bitrates);
+        AudioConverterGetProperty(pv->converter,
+                                  kAudioConverterApplicableEncodeBitRates,
+                                  &tmpsiz, bitrates);
         bitrateCounts = tmpsiz / sizeof(AudioValueRange);
 
         // set bitrate
diff --git a/libhb/preset.c b/libhb/preset.c
index 5840714ce..710499024 100644
--- a/libhb/preset.c
+++ b/libhb/preset.c
@@ -921,7 +921,6 @@ static void add_subtitle_for_lang(hb_value_array_t *list, hb_title_t *title,
                                   subtitle_behavior_t *behavior)
 {
     int t;
-    t = find_subtitle_track(title, lang, 0);
     for (t = find_subtitle_track(title, lang, 0);
          t >= 0;
          t = behavior->one ? -1 : find_subtitle_track(title, lang, t + 1))
@@ -3698,6 +3697,12 @@ int hb_presets_add_path(char * path)
     {
         count++;
     }
+
+    if (count == 0)
+    {
+        return -1;
+    }
+
     files = malloc(count * sizeof(char*));
 
     // Find all regular files
diff --git a/libhb/stream.c b/libhb/stream.c
index bfc5dbc04..326cb0e4b 100644
--- a/libhb/stream.c
+++ b/libhb/stream.c
@@ -813,6 +813,12 @@ static void prune_streams(hb_stream_t *d)
 hb_stream_t *
 hb_stream_open(hb_handle_t *h, char *path, hb_title_t *title, int scan)
 {
+    if (title == NULL)
+    {
+        hb_log("hb_stream_open: title is null");
+        return NULL;
+    }
+
     FILE *f = hb_fopen(path, "rb");
     if ( f == NULL )
     {
@@ -828,7 +834,7 @@ hb_stream_open(hb_handle_t *h, char *path, hb_title_t *title, int scan)
         return NULL;
     }
 
-    if( title && !( title->flags & HBTF_NO_IDR ) )
+    if (!(title->flags & HBTF_NO_IDR))
     {
         d->has_IDRs = 1;
     }
@@ -2899,10 +2905,10 @@ static int decode_PAT(const uint8_t *buf, hb_stream_t *stream)
                             }
                     }
 
-                    pos += 3 + section_len;
+//                    pos += 3 + section_len;
             }
 
-            tablepos = 0;
+//            tablepos = 0;
     }
     return 1;
 }