dvbbasebin: fix test for proper use count balancing

usecount is unsigned, so too many "unuse" will wrap the counter
around and the >= 0 check will always be fine.

It would be much simpler to just make the counter signed, but
moving the checks where the decrements happen allow a mistake
to be detected earlier, and thus easier to debug.

Coverity 1139791
This commit is contained in:
Vincent Penquerc'h 2014-04-17 09:58:47 +01:00
parent 52981d5a4a
commit 8250a7d531

View file

@ -136,6 +136,21 @@ G_DEFINE_TYPE_EXTENDED (DvbBaseBin, dvb_base_bin, GST_TYPE_BIN,
dvb_base_bin_uri_handler_init));
static void
dvb_base_bin_ref_stream (DvbBaseBinStream * stream)
{
g_return_if_fail (stream != NULL);
stream->usecount++;
}
static void
dvb_base_bin_unref_stream (DvbBaseBinStream * stream)
{
g_return_if_fail (stream != NULL);
g_return_if_fail (stream->usecount > 0);
stream->usecount--;
}
static DvbBaseBinStream *
dvb_base_bin_add_stream (DvbBaseBin * dvbbasebin, guint16 pid)
{
@ -366,7 +381,7 @@ dvb_base_bin_init (DvbBaseBin * dvbbasebin)
i = 0;
while (initial_pids[i] >= 0) {
stream = dvb_base_bin_add_stream (dvbbasebin, (guint16) initial_pids[i]);
++stream->usecount;
dvb_base_bin_ref_stream (stream);
i++;
}
dvb_base_bin_rebuild_filter (dvbbasebin);
@ -649,8 +664,6 @@ foreach_stream_build_filter (gpointer key, gpointer value, gpointer user_data)
DvbBaseBinStream *stream = (DvbBaseBinStream *) value;
gchar *tmp, *pid;
g_assert (stream->usecount >= 0);
GST_DEBUG ("stream %d usecount %d", stream->pid, stream->usecount);
if (stream->usecount > 0) {
@ -699,7 +712,7 @@ dvb_base_bin_remove_pmt_streams (DvbBaseBin * dvbbasebin,
continue;
}
--stream->usecount;
dvb_base_bin_unref_stream (stream);
}
}
@ -718,7 +731,7 @@ dvb_base_bin_add_pmt_streams (DvbBaseBin * dvbbasebin, const GstMpegTsPMT * pmt)
stream = dvb_base_bin_get_stream (dvbbasebin, pmtstream->pid);
if (stream == NULL)
stream = dvb_base_bin_add_stream (dvbbasebin, pmtstream->pid);
++stream->usecount;
dvb_base_bin_ref_stream (stream);
}
}
@ -740,7 +753,7 @@ dvb_base_bin_activate_program (DvbBaseBin * dvbbasebin,
stream = dvb_base_bin_get_stream (dvbbasebin, program->pmt_pid);
if (stream == NULL)
stream = dvb_base_bin_add_stream (dvbbasebin, program->pmt_pid);
stream->usecount += 1;
dvb_base_bin_ref_stream (stream);
program->pmt_active = TRUE;
}
@ -749,13 +762,15 @@ dvb_base_bin_activate_program (DvbBaseBin * dvbbasebin,
old_pcr_pid = program->pcr_pid;
program->pcr_pid = program->pmt->pcr_pid;
if (old_pcr_pid != G_MAXUINT16 && old_pcr_pid != program->pcr_pid)
dvb_base_bin_get_stream (dvbbasebin, old_pcr_pid)->usecount--;
if (old_pcr_pid != G_MAXUINT16 && old_pcr_pid != program->pcr_pid) {
dvb_base_bin_unref_stream (dvb_base_bin_get_stream (dvbbasebin,
old_pcr_pid));
}
stream = dvb_base_bin_get_stream (dvbbasebin, program->pcr_pid);
if (stream == NULL)
stream = dvb_base_bin_add_stream (dvbbasebin, program->pcr_pid);
stream->usecount += 1;
dvb_base_bin_ref_stream (stream);
dvb_base_bin_add_pmt_streams (dvbbasebin, program->pmt);
dvbbasebin->pmtlist =
@ -774,12 +789,14 @@ dvb_base_bin_deactivate_program (DvbBaseBin * dvbbasebin,
DvbBaseBinStream *stream;
stream = dvb_base_bin_get_stream (dvbbasebin, program->pmt_pid);
if (stream != NULL)
stream->usecount -= 1;
if (stream != NULL) {
dvb_base_bin_unref_stream (stream);
}
stream = dvb_base_bin_get_stream (dvbbasebin, program->pcr_pid);
if (stream != NULL)
stream->usecount -= 1;
if (stream != NULL) {
dvb_base_bin_unref_stream (stream);
}
if (program->pmt) {
dvb_base_bin_remove_pmt_streams (dvbbasebin, program->pmt);
@ -851,14 +868,16 @@ dvb_base_bin_pat_info_cb (DvbBaseBin * dvbbasebin, GstMpegTsSection * section)
if (program->selected) {
/* PAT update */
if (old_pmt_pid != G_MAXUINT16 && old_pmt_pid != program->pmt_pid)
dvb_base_bin_get_stream (dvbbasebin, old_pmt_pid)->usecount -= 1;
if (old_pmt_pid != G_MAXUINT16 && old_pmt_pid != program->pmt_pid) {
dvb_base_bin_unref_stream (dvb_base_bin_get_stream (dvbbasebin,
old_pmt_pid));
}
stream = dvb_base_bin_get_stream (dvbbasebin, program->pmt_pid);
if (stream == NULL)
stream = dvb_base_bin_add_stream (dvbbasebin, program->pmt_pid);
stream->usecount += 1;
dvb_base_bin_ref_stream (stream);
rebuild_filter = TRUE;
}